diff --git a/src/app/courses/[courseId]/[...moduleId]/page.tsx b/src/app/courses/[courseId]/[...moduleId]/page.tsx index d4ef96781..1a6c83ddc 100644 --- a/src/app/courses/[courseId]/[...moduleId]/page.tsx +++ b/src/app/courses/[courseId]/[...moduleId]/page.tsx @@ -1,6 +1,6 @@ import { QueryParams } from '@/actions/types'; import { CourseView } from '@/components/CourseView'; -import { getCourse, getFullCourseContent, getNextVideo, getPrevVideo } from '@/db/course'; +import { getCourse, getCourseAllVideos, getFullCourseContent } from '@/db/course'; import findContentById from '@/lib/find-content-by-id'; export default async function Course({ @@ -20,15 +20,29 @@ export default async function Course({ fullCourseContent, rest.map((x) => parseInt(x, 10)), ); - const nextContent = await getNextVideo(Number(rest[rest.length - 1]), (rest.length>1? Number(rest[rest.length-2]):0), Number(courseId)); - const prevContent = await getPrevVideo(Number(rest[rest.length - 1]), (rest.length>1? Number(rest[rest.length-2]):0), Number(courseId)); + + const CurrentCourseAllVideos = await getCourseAllVideos(Number(courseId)); + const nextContent = CurrentCourseAllVideos.find((video: any) => { + return video.id > Number(rest[rest.length - 1]); + }); + const prevContent = (() => { + for (let i = CurrentCourseAllVideos.length - 1; i >= 0; i--) { + if (CurrentCourseAllVideos[i].id < Number(rest[rest.length - 1])) { + return CurrentCourseAllVideos[i]; + } + } + return null; + })(); + + const nextContentUrl = (nextContent ? `/courses/${courseId}/${nextContent.parentId}/${nextContent.id}` : null); + const prevContentUrl = (prevContent ? `/courses/${courseId}/${prevContent.parentId}/${prevContent.id}` : null); return ( { - if (!currentVideoId || !currentVideoWeek || !currentVideoCourse) { +export const getCourseAllVideos = async (currentCourse: number) => { + if (!currentCourse) { return null; } - const value = await cache.get('getNextVideo', [currentVideoId.toString()]); + const value = await cache.get('getCourseAllVideos', [String(currentCourse)]); if (value) { return value; } - const latestContent = await db.content.findFirst({ - orderBy: [ - { - id: 'asc', - }, - ], - where: { - parentId: { - equals: currentVideoWeek, - }, - id: { - gt: currentVideoId, - }, - type: "video", + const allVideos = await db.content.findMany({ + orderBy: { + id: 'asc', }, - }); - - if (!latestContent) { - const latestWeek = await db.courseContent.findMany({ - where: { - courseId: currentVideoCourse, - contentId: { - gt: currentVideoWeek, - }, - }, - orderBy: { - contentId: 'asc', - }, - take: 1, - }); - if (latestWeek && latestWeek.length===0) { - return null; - } - const latestContent2 = await db.content.findFirst({ - orderBy: [ - { - id: 'asc', - }, - ], - where: { - parentId: latestWeek[0].contentId, - type: "video", - }, - }); - if (!latestContent2) { - return null; - } - const latestcontenturl = `/courses/${currentVideoCourse}/${latestWeek[0].contentId}/${latestContent2?.id}`; - cache.set('getNextVideo', [currentVideoId.toString()], latestcontenturl); - return latestcontenturl; - } - const latestcontenturl = `/courses/${currentVideoCourse}/${currentVideoWeek}/${latestContent.id}`; - cache.set('getNextVideo', [currentVideoId.toString()], latestcontenturl); - return latestcontenturl; -}; - -export const getPrevVideo = async (currentVideoId: number, currentVideoWeek: number, currentVideoCourse: number) => { - if (!currentVideoId || !currentVideoWeek || !currentVideoCourse) { - return null; - } - const value = await cache.get('getPrevVideo', [currentVideoId.toString()]); - if (value) { - return value; - } - const latestContent = await db.content.findFirst({ - orderBy: [ - { - id: 'desc', - }, - ], where: { - parentId: { - equals: currentVideoWeek, - }, - id: { - lt: currentVideoId, + parent: { + courses: { + some: { + courseId: currentCourse + } + }, }, type: "video", - }, + } }); - if (!latestContent) { - const latestWeek = await db.courseContent.findMany({ - where: { - courseId: currentVideoCourse, - contentId: { - lt: currentVideoWeek, - }, - }, - orderBy: { - contentId: 'desc', - }, - take: 1, - }); - if (latestWeek && latestWeek.length===0) { - return null; - } - const latestContent2 = await db.content.findFirst({ - orderBy: [ - { - id: 'desc', - }, - ], - where: { - parentId: latestWeek[0].contentId, - type: "video" - }, - }); - if (!latestContent2) { - return null; - } - const latestcontenturl = `/courses/${currentVideoCourse}/${latestWeek[0].contentId}/${latestContent2?.id}`; - cache.set('getPrevVideo', [currentVideoId.toString()], latestcontenturl); - return latestcontenturl; + if (!allVideos || allVideos.length<1) { + return null; } - const latestcontenturl = `/courses/${currentVideoCourse}/${currentVideoWeek}/${latestContent.id}`; - cache.set('getPrevVideo', [currentVideoId.toString()], latestcontenturl); - return latestcontenturl; + cache.set('getCourseAllVideos', [String(currentCourse)], allVideos); + return allVideos; }; async function getAllContent(): Promise<