update api chapterlesson
This commit is contained in:
parent
2fc0fb7a76
commit
5c2b5d55aa
11 changed files with 855 additions and 85 deletions
|
|
@ -165,7 +165,7 @@ export class ChaptersLessonService {
|
|||
|
||||
async reorderChapter(request: ReorderChapterRequest): Promise<ReorderChapterResponse> {
|
||||
try {
|
||||
const { token, course_id, chapter_id, sort_order } = request;
|
||||
const { token, course_id, chapter_id, sort_order: newSortOrder } = request;
|
||||
const decodedToken = jwt.verify(token, config.jwt.secret) as { id: number };
|
||||
await CoursesInstructorService.validateCourseStatus(course_id);
|
||||
const user = await prisma.user.findUnique({ where: { id: decodedToken.id } });
|
||||
|
|
@ -176,8 +176,54 @@ export class ChaptersLessonService {
|
|||
if (!courseInstructor) {
|
||||
throw new ForbiddenError('You are not permitted to reorder chapter');
|
||||
}
|
||||
const chapter = await prisma.chapter.update({ where: { id: chapter_id }, data: { sort_order } });
|
||||
return { code: 200, message: 'Chapter reordered successfully', data: [chapter as ChapterData] };
|
||||
|
||||
// Get current chapter to find its current sort_order
|
||||
const currentChapter = await prisma.chapter.findUnique({ where: { id: chapter_id } });
|
||||
if (!currentChapter) {
|
||||
throw new NotFoundError('Chapter not found');
|
||||
}
|
||||
const oldSortOrder = currentChapter.sort_order;
|
||||
|
||||
// If same position, no need to reorder
|
||||
if (oldSortOrder === newSortOrder) {
|
||||
const chapters = await prisma.chapter.findMany({
|
||||
where: { course_id },
|
||||
orderBy: { sort_order: 'asc' }
|
||||
});
|
||||
return { code: 200, message: 'Chapter reordered successfully', data: chapters as ChapterData[] };
|
||||
}
|
||||
|
||||
// Shift other chapters to make room for the insert
|
||||
if (oldSortOrder > newSortOrder) {
|
||||
// Moving up: shift chapters between newSortOrder and oldSortOrder-1 down by 1
|
||||
await prisma.chapter.updateMany({
|
||||
where: {
|
||||
course_id,
|
||||
sort_order: { gte: newSortOrder, lt: oldSortOrder }
|
||||
},
|
||||
data: { sort_order: { increment: 1 } }
|
||||
});
|
||||
} else {
|
||||
// Moving down: shift chapters between oldSortOrder+1 and newSortOrder up by 1
|
||||
await prisma.chapter.updateMany({
|
||||
where: {
|
||||
course_id,
|
||||
sort_order: { gt: oldSortOrder, lte: newSortOrder }
|
||||
},
|
||||
data: { sort_order: { decrement: 1 } }
|
||||
});
|
||||
}
|
||||
|
||||
// Update the target chapter to the new position
|
||||
await prisma.chapter.update({ where: { id: chapter_id }, data: { sort_order: newSortOrder } });
|
||||
|
||||
// Fetch all chapters with updated order
|
||||
const chapters = await prisma.chapter.findMany({
|
||||
where: { course_id },
|
||||
orderBy: { sort_order: 'asc' }
|
||||
});
|
||||
|
||||
return { code: 200, message: 'Chapter reordered successfully', data: chapters as ChapterData[] };
|
||||
} catch (error) {
|
||||
logger.error(`Error reordering chapter: ${error}`);
|
||||
throw error;
|
||||
|
|
@ -315,7 +361,7 @@ export class ChaptersLessonService {
|
|||
*/
|
||||
async reorderLessons(request: ReorderLessonsRequest): Promise<ReorderLessonsResponse> {
|
||||
try {
|
||||
const { token, course_id, chapter_id, lesson_ids } = request;
|
||||
const { token, course_id, chapter_id, lesson_id, sort_order: newSortOrder } = request;
|
||||
const decodedToken = jwt.verify(token, config.jwt.secret) as { id: number };
|
||||
await CoursesInstructorService.validateCourseStatus(course_id);
|
||||
|
||||
|
|
@ -331,17 +377,52 @@ export class ChaptersLessonService {
|
|||
throw new NotFoundError('Chapter not found');
|
||||
}
|
||||
|
||||
// Update sort_order for each lesson
|
||||
for (let i = 0; i < lesson_ids.length; i++) {
|
||||
await prisma.lesson.update({
|
||||
where: { id: lesson_ids[i] },
|
||||
data: { sort_order: i }
|
||||
// Get current lesson to find its current sort_order
|
||||
const currentLesson = await prisma.lesson.findUnique({ where: { id: lesson_id } });
|
||||
if (!currentLesson) {
|
||||
throw new NotFoundError('Lesson not found');
|
||||
}
|
||||
if (currentLesson.chapter_id !== chapter_id) {
|
||||
throw new NotFoundError('Lesson not found in this chapter');
|
||||
}
|
||||
const oldSortOrder = currentLesson.sort_order;
|
||||
|
||||
// If same position, no need to reorder
|
||||
if (oldSortOrder === newSortOrder) {
|
||||
const lessons = await prisma.lesson.findMany({
|
||||
where: { chapter_id },
|
||||
orderBy: { sort_order: 'asc' }
|
||||
});
|
||||
return { code: 200, message: 'Lessons reordered successfully', data: lessons as LessonData[] };
|
||||
}
|
||||
|
||||
// Shift other lessons to make room for the insert
|
||||
if (oldSortOrder > newSortOrder) {
|
||||
// Moving up: shift lessons between newSortOrder and oldSortOrder-1 down by 1
|
||||
await prisma.lesson.updateMany({
|
||||
where: {
|
||||
chapter_id,
|
||||
sort_order: { gte: newSortOrder, lt: oldSortOrder }
|
||||
},
|
||||
data: { sort_order: { increment: 1 } }
|
||||
});
|
||||
} else {
|
||||
// Moving down: shift lessons between oldSortOrder+1 and newSortOrder up by 1
|
||||
await prisma.lesson.updateMany({
|
||||
where: {
|
||||
chapter_id,
|
||||
sort_order: { gt: oldSortOrder, lte: newSortOrder }
|
||||
},
|
||||
data: { sort_order: { decrement: 1 } }
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch reordered lessons
|
||||
// Update the target lesson to the new position
|
||||
await prisma.lesson.update({ where: { id: lesson_id }, data: { sort_order: newSortOrder } });
|
||||
|
||||
// Fetch all lessons with updated order
|
||||
const lessons = await prisma.lesson.findMany({
|
||||
where: { chapter_id: chapter_id },
|
||||
where: { chapter_id },
|
||||
orderBy: { sort_order: 'asc' }
|
||||
});
|
||||
|
||||
|
|
@ -498,6 +579,7 @@ export class ChaptersLessonService {
|
|||
await CoursesInstructorService.validateCourseStatus(course_id);
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: decodedToken.id } });
|
||||
logger.info(`User: ${user}`);
|
||||
if (!user) throw new UnauthorizedError('Invalid token');
|
||||
|
||||
const courseInstructor = await CoursesInstructorService.validateCourseInstructor(token, course_id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue