This commit is contained in:
@@ -349,6 +349,108 @@ class SubjectController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add chapter to subject (Create chapter within subject context)
|
||||
*/
|
||||
async addChapterToSubject(req, res, next) {
|
||||
try {
|
||||
const { subjectId } = req.params;
|
||||
const chapterData = req.body;
|
||||
|
||||
// Check if subject exists
|
||||
const subject = await Subject.findByPk(subjectId);
|
||||
if (!subject) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Subject not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if (!chapterData.chapter_number || !chapterData.chapter_title) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'chapter_number and chapter_title are required',
|
||||
});
|
||||
}
|
||||
|
||||
// Create chapter with subject_id
|
||||
const chapter = await Chapter.create({
|
||||
...chapterData,
|
||||
subject_id: subjectId,
|
||||
});
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.deletePattern('chapters:*');
|
||||
await cacheUtils.deletePattern(`subject:${subjectId}:chapters:*`);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Chapter added to subject successfully',
|
||||
data: chapter,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove chapter from subject (Delete chapter within subject context)
|
||||
*/
|
||||
async removeChapterFromSubject(req, res, next) {
|
||||
try {
|
||||
const { subjectId, chapterId } = req.params;
|
||||
|
||||
// Check if subject exists
|
||||
const subject = await Subject.findByPk(subjectId);
|
||||
if (!subject) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Subject not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Find chapter
|
||||
const chapter = await Chapter.findOne({
|
||||
where: {
|
||||
id: chapterId,
|
||||
subject_id: subjectId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!chapter) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Chapter not found in this subject',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if chapter has lessons
|
||||
const Lesson = require('../models').Lesson;
|
||||
const lessonsCount = await Lesson.count({ where: { chapter_id: chapterId } });
|
||||
if (lessonsCount > 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: `Cannot delete chapter. It has ${lessonsCount} lesson(s). Delete lessons first.`,
|
||||
});
|
||||
}
|
||||
|
||||
await chapter.destroy();
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.deletePattern('chapters:*');
|
||||
await cacheUtils.deletePattern(`chapter:${chapterId}*`);
|
||||
await cacheUtils.deletePattern(`subject:${subjectId}:chapters:*`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Chapter removed from subject successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SubjectController();
|
||||
|
||||
Reference in New Issue
Block a user