This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user