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

@@ -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();