Initial commit

This commit is contained in:
twotalesanimation
2026-06-11 10:46:09 +02:00
commit 81ad7e4ea9
223 changed files with 39530 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
// 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 });
}
}
+133
View File
@@ -0,0 +1,133 @@
// app/api/playlists/route.ts
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth-options";
import { prisma } from "@/lib/prisma";
type VideoWithCourses = {
videoCourses?: Array<{ courseId: string; exclusive: boolean }>
};
function mapRestrictedCourseIds(video: VideoWithCourses) {
if (!video?.videoCourses?.length) return [];
return video.videoCourses
.filter((assignment) => assignment.exclusive)
.map((assignment) => assignment.courseId);
}
function filterRestrictedVideos(videos: any[], userCourseIds: string[]) {
return videos
.filter((video) => {
const restrictedCourseIds = mapRestrictedCourseIds(video);
if (restrictedCourseIds.length === 0) return true;
return restrictedCourseIds.some((courseId: string) =>
userCourseIds.includes(courseId)
);
})
.map((video) => ({
...video,
restrictedCourseIds: mapRestrictedCourseIds(video),
}));
}
export async function GET(req: Request) {
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 },
include: {
enrollments: {
include: { course: true },
},
},
});
const courseIds = user?.enrollments.map((e) => e.courseId) ?? [];
// Get playlists directly assigned to enrolled courses
const coursesWithPlaylists = await prisma.course.findMany({
where: { id: { in: courseIds } },
include: {
playlists: {
include: {
videos: {
include: {
uploader: {
select: {
id: true,
name: true,
image: true,
},
},
videoCourses: true,
},
orderBy: { index: "asc" },
},
courses: {
include: { course: true },
},
},
orderBy: { sortOrder: "asc" },
},
},
orderBy: { title: "asc" },
});
// Also get playlists assigned to courses via CoursePlaylist mapping
const additionalPlaylists = await prisma.coursePlaylist.findMany({
where: { courseId: { in: courseIds } },
include: {
playlist: {
include: {
videos: {
include: {
uploader: {
select: {
id: true,
name: true,
image: true,
},
},
videoCourses: true,
},
orderBy: { index: "asc" },
},
courses: {
include: { course: true },
},
},
},
course: true,
},
});
// Merge results: add additional playlists to their respective courses
const playlistMap = new Map();
additionalPlaylists.forEach(({ course, playlist }) => {
if (!playlistMap.has(course.id)) {
playlistMap.set(course.id, []);
}
playlist.videos = filterRestrictedVideos(playlist.videos, courseIds);
playlistMap.get(course.id).push(playlist);
});
coursesWithPlaylists.forEach((course) => {
course.playlists.forEach((playlist) => {
playlist.videos = filterRestrictedVideos(playlist.videos, courseIds);
});
const additional = playlistMap.get(course.id) || [];
const existingIds = new Set(course.playlists.map((p) => p.id));
const newPlaylists = additional.filter((p: any) => !existingIds.has(p.id));
newPlaylists.forEach((playlist: any) => {
playlist.videos = filterRestrictedVideos(playlist.videos, courseIds);
});
course.playlists.push(...newPlaylists);
course.playlists.sort((a, b) => a.sortOrder - b.sortOrder);
});
return NextResponse.json({ subjects: coursesWithPlaylists });
}