Skip to content

Commit

Permalink
optimized the next and prev video call
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-flick committed Oct 8, 2024
1 parent 8bf41df commit 25843e8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 119 deletions.
24 changes: 19 additions & 5 deletions src/app/courses/[courseId]/[...moduleId]/page.tsx
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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 (
<CourseView
rest={rest}
course={course}
nextContent={nextContent}
prevContent={prevContent}
nextContent={nextContentUrl}
prevContent={prevContentUrl}
courseContent={courseContent}
fullCourseContent={fullCourseContent}
searchParams={searchParams}
Expand Down
131 changes: 17 additions & 114 deletions src/db/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,133 +133,36 @@ export async function getCourse(courseId: number) {
return courses;
}

export const getNextVideo = async (currentVideoId: number, currentVideoWeek: number, currentVideoCourse: number) => {
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<
Expand Down

0 comments on commit 25843e8

Please sign in to comment.