This commit is contained in:
@@ -311,7 +311,7 @@ class CategoryController {
|
||||
}
|
||||
|
||||
// Build query conditions
|
||||
const where = { id: id };
|
||||
const where = { category_id: id };
|
||||
if (is_active !== undefined) where.is_active = is_active === 'true';
|
||||
|
||||
// Query subjects
|
||||
@@ -349,6 +349,90 @@ class CategoryController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add subject to category (Create subject within category context)
|
||||
*/
|
||||
async addSubjectToCategory(req, res, next) {
|
||||
try {
|
||||
const { categoryId } = req.params;
|
||||
const subjectData = req.body;
|
||||
|
||||
// Check if category exists
|
||||
const category = await Categories.findByPk(categoryId);
|
||||
if (!category) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Category not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Create subject with category_id
|
||||
const subject = await Subject.create({
|
||||
...subjectData,
|
||||
category_id: categoryId,
|
||||
});
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.deletePattern('subjects:list:*');
|
||||
await cacheUtils.deletePattern(`category:${categoryId}:subjects:*`);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Subject added to category successfully',
|
||||
data: subject,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove subject from category (Delete subject within category context)
|
||||
*/
|
||||
async removeSubjectFromCategory(req, res, next) {
|
||||
try {
|
||||
const { categoryId, subjectId } = req.params;
|
||||
|
||||
// Check if category exists
|
||||
const category = await Categories.findByPk(categoryId);
|
||||
if (!category) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Category not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Find subject
|
||||
const subject = await Subject.findOne({
|
||||
where: {
|
||||
id: subjectId,
|
||||
category_id: categoryId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!subject) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Subject not found in this category',
|
||||
});
|
||||
}
|
||||
|
||||
await subject.destroy();
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.delete(`subject:${subjectId}`);
|
||||
await cacheUtils.deletePattern('subjects:list:*');
|
||||
await cacheUtils.deletePattern(`category:${categoryId}:subjects:*`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Subject removed from category successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new CategoryController();
|
||||
|
||||
Reference in New Issue
Block a user