Files
twotalesanimation 81ad7e4ea9 Initial commit
2026-06-11 10:46:09 +02:00

60 lines
1.8 KiB
TypeScript

// app/api/playlists/[id]/route.ts
import { NextResponse } from "next/server";
import { prisma } from "../../../../lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "../../../../lib/auth-options";
export async function GET(req: Request, context: any) {
try {
// Unwrap params (Next may provide a Promise)
let params = context?.params;
if (typeof params?.then === "function") params = await params;
const id = params?.id;
if (!id) return NextResponse.json({ error: "Missing playlist id" }, { status: 400 });
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const user = await prisma.user.findUnique({
where: { email: session.user.email },
});
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const playlist = await prisma.playlist.findUnique({
where: { id },
include: {
videos: {
include: {
uploader: {
select: {
id: true,
name: true,
image: true,
},
},
},
orderBy: { index: "asc" },
},
course: true,
},
});
if (!playlist) return NextResponse.json({ error: "Not found" }, { status: 404 });
const enrolled = await prisma.enrollment.findUnique({
where: { userId_courseId: { userId: user.id, courseId: playlist.courseId } },
});
if (!enrolled) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
return NextResponse.json({ playlist });
} catch (err: any) {
console.error("GET /api/playlists/[id] error:", err);
return NextResponse.json({ error: "Server error" }, { status: 500 });
}
}