update
All checks were successful
Deploy to Production / deploy (push) Successful in 20s

This commit is contained in:
Ken
2026-02-27 09:38:39 +07:00
parent 9af45a7875
commit 6287a019e3
16 changed files with 2032 additions and 18 deletions

View File

@@ -330,6 +330,98 @@ class ChapterController {
next(error);
}
}
/**
* Add lesson to chapter (Create lesson within chapter context)
*/
async addLessonToChapter(req, res, next) {
try {
const { chapterId } = req.params;
const lessonData = req.body;
// Check if chapter exists
const chapter = await Chapter.findByPk(chapterId);
if (!chapter) {
return res.status(404).json({
success: false,
message: 'Chapter not found',
});
}
// Validate required fields
if (!lessonData.lesson_number || !lessonData.lesson_title) {
return res.status(400).json({
success: false,
message: 'lesson_number and lesson_title are required',
});
}
// Create lesson with chapter_id
const lesson = await Lesson.create({
...lessonData,
chapter_id: chapterId,
});
// Clear cache
await cacheUtils.deletePattern('lessons:*');
await cacheUtils.deletePattern(`chapter:${chapterId}:lessons:*`);
res.status(201).json({
success: true,
message: 'Lesson added to chapter successfully',
data: lesson,
});
} catch (error) {
next(error);
}
}
/**
* Remove lesson from chapter (Delete lesson within chapter context)
*/
async removeLessonFromChapter(req, res, next) {
try {
const { chapterId, lessonId } = req.params;
// Check if chapter exists
const chapter = await Chapter.findByPk(chapterId);
if (!chapter) {
return res.status(404).json({
success: false,
message: 'Chapter not found',
});
}
// Find lesson
const lesson = await Lesson.findOne({
where: {
id: lessonId,
chapter_id: chapterId,
},
});
if (!lesson) {
return res.status(404).json({
success: false,
message: 'Lesson not found in this chapter',
});
}
await lesson.destroy();
// Clear cache
await cacheUtils.deletePattern('lessons:*');
await cacheUtils.deletePattern(`lesson:${lessonId}*`);
await cacheUtils.deletePattern(`chapter:${chapterId}:lessons:*`);
res.json({
success: true,
message: 'Lesson removed from chapter successfully',
});
} catch (error) {
next(error);
}
}
}
module.exports = new ChapterController();