This commit is contained in:
@@ -291,7 +291,7 @@ class CategoryController {
|
|||||||
// Generate cache key
|
// Generate cache key
|
||||||
const cacheKey = `category:${id}:subjects:${page}:${limit}:${is_active || 'all'}`;
|
const cacheKey = `category:${id}:subjects:${page}:${limit}:${is_active || 'all'}`;
|
||||||
|
|
||||||
// Try cache first
|
/* Try cache first
|
||||||
const cached = await cacheUtils.get(cacheKey);
|
const cached = await cacheUtils.get(cacheKey);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return res.json({
|
return res.json({
|
||||||
@@ -300,7 +300,7 @@ class CategoryController {
|
|||||||
cached: true,
|
cached: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
// Check if category exists
|
// Check if category exists
|
||||||
const category = await Categories.findByPk(id);
|
const category = await Categories.findByPk(id);
|
||||||
if (!category) {
|
if (!category) {
|
||||||
@@ -356,7 +356,14 @@ class CategoryController {
|
|||||||
async addSubjectToCategory(req, res, next) {
|
async addSubjectToCategory(req, res, next) {
|
||||||
try {
|
try {
|
||||||
const { categoryId } = req.params;
|
const { categoryId } = req.params;
|
||||||
const subjectData = req.body;
|
const { id } = req.body;
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Subject id is required',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Check if category exists
|
// Check if category exists
|
||||||
const category = await Categories.findByPk(categoryId);
|
const category = await Categories.findByPk(categoryId);
|
||||||
@@ -367,17 +374,23 @@ class CategoryController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create subject with category_id
|
// Find existing subject
|
||||||
const subject = await Subject.create({
|
const subject = await Subject.findByPk(id);
|
||||||
...subjectData,
|
if (!subject) {
|
||||||
category_id: categoryId,
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Subject not found',
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign category
|
||||||
|
await subject.update({ category_id: categoryId });
|
||||||
|
|
||||||
// Clear cache
|
// Clear cache
|
||||||
await cacheUtils.deletePattern('subjects:list:*');
|
await cacheUtils.deletePattern('subjects:list:*');
|
||||||
await cacheUtils.deletePattern(`category:${categoryId}:subjects:*`);
|
await cacheUtils.deletePattern(`category:${categoryId}:subjects:*`);
|
||||||
|
|
||||||
res.status(201).json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Subject added to category successfully',
|
message: 'Subject added to category successfully',
|
||||||
data: subject,
|
data: subject,
|
||||||
|
|||||||
@@ -66,9 +66,6 @@
|
|||||||
{
|
{
|
||||||
"grade": "010106",
|
"grade": "010106",
|
||||||
"vocab": [
|
"vocab": [
|
||||||
"Rosy",
|
|
||||||
"Tim",
|
|
||||||
"Billy",
|
|
||||||
"Mom",
|
"Mom",
|
||||||
"clean",
|
"clean",
|
||||||
"tidy",
|
"tidy",
|
||||||
|
|||||||
@@ -75,10 +75,7 @@
|
|||||||
"ao dai"
|
"ao dai"
|
||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": null
|
||||||
"They are dancers from Viet Nam.",
|
|
||||||
"They have fans."
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"grade": "010208",
|
"grade": "010208",
|
||||||
|
|||||||
@@ -91,9 +91,7 @@
|
|||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"A flower!",
|
"A flower!",
|
||||||
"A frog!",
|
"A frog!"
|
||||||
"It's a bird.",
|
|
||||||
"score a goal"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -79,8 +79,7 @@
|
|||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"This is...",
|
"This is...",
|
||||||
"These are...",
|
"These are..."
|
||||||
"Let's take care in the sun."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -69,8 +69,7 @@
|
|||||||
"vocab": [],
|
"vocab": [],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"I have ...",
|
"I have ..."
|
||||||
"Share with others."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,10 +3,7 @@
|
|||||||
"grade": "010001",
|
"grade": "010001",
|
||||||
"vocab": [
|
"vocab": [
|
||||||
"hello",
|
"hello",
|
||||||
"goodbye",
|
"goodbye"
|
||||||
"Rosy",
|
|
||||||
"Tim",
|
|
||||||
"Billy"
|
|
||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
@@ -18,17 +15,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"grade": "010002",
|
"grade": "010002",
|
||||||
"vocab": [
|
"vocab": null,
|
||||||
"hello",
|
|
||||||
"goodbye",
|
|
||||||
"Rosy",
|
|
||||||
"Tim",
|
|
||||||
"Billy"
|
|
||||||
],
|
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"What's your name?",
|
|
||||||
"I'm ...",
|
|
||||||
"Stand up",
|
"Stand up",
|
||||||
"Sit down",
|
"Sit down",
|
||||||
"Line up",
|
"Line up",
|
||||||
@@ -78,16 +67,10 @@
|
|||||||
"vocab": [
|
"vocab": [
|
||||||
"hello",
|
"hello",
|
||||||
"goodbye",
|
"goodbye",
|
||||||
"Rosy",
|
"Mom"
|
||||||
"Tim",
|
|
||||||
"Billy",
|
|
||||||
"Mom",
|
|
||||||
"Miss Bell"
|
|
||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": null
|
||||||
"It is good to say Hello and Goodbye."
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"grade": "010007",
|
"grade": "010007",
|
||||||
@@ -101,8 +84,7 @@
|
|||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"I'm from ...",
|
"I'm from ..."
|
||||||
"It's red and white."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,14 +4,10 @@
|
|||||||
"vocab": [
|
"vocab": [
|
||||||
"pink",
|
"pink",
|
||||||
"brown",
|
"brown",
|
||||||
"white",
|
"white"
|
||||||
"Hello",
|
|
||||||
"Nice to meet you"
|
|
||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"Hello.",
|
|
||||||
"Nice to meet you.",
|
|
||||||
"This is...",
|
"This is...",
|
||||||
"What color is it?",
|
"What color is it?",
|
||||||
"It's..."
|
"It's..."
|
||||||
|
|||||||
@@ -8,8 +8,7 @@
|
|||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"What's this?",
|
"What's this?"
|
||||||
"Point and say."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -159,8 +158,7 @@
|
|||||||
],
|
],
|
||||||
"phonics": null,
|
"phonics": null,
|
||||||
"grammar": [
|
"grammar": [
|
||||||
"Hello, I'm... the lion.",
|
"Hello, I'm... the lion."
|
||||||
"Nice to meet you."
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -21,9 +21,6 @@ router.get('/code/:code', categoryController.getCategoryByCode);
|
|||||||
// GET /api/categories/:id - Get category by ID
|
// GET /api/categories/:id - Get category by ID
|
||||||
router.get('/:id', categoryController.getCategoryById);
|
router.get('/:id', categoryController.getCategoryById);
|
||||||
|
|
||||||
// GET /api/categories/:id/subjects - Get subjects by category
|
|
||||||
router.get('/:id/subjects', categoryController.getSubjectsByCategory);
|
|
||||||
|
|
||||||
// POST /api/categories - Create new category
|
// POST /api/categories - Create new category
|
||||||
router.post('/', categoryController.createCategory);
|
router.post('/', categoryController.createCategory);
|
||||||
|
|
||||||
@@ -34,6 +31,9 @@ router.put('/:id', categoryController.updateCategory);
|
|||||||
router.delete('/:id', categoryController.deleteCategory);
|
router.delete('/:id', categoryController.deleteCategory);
|
||||||
|
|
||||||
// ============ Nested Subject Routes ============
|
// ============ Nested Subject Routes ============
|
||||||
|
// GET /api/categories/:id/subjects - Get subjects by category
|
||||||
|
router.get('/:id/subjects', categoryController.getSubjectsByCategory);
|
||||||
|
|
||||||
// POST /api/categories/:categoryId/subjects - Add subject to category
|
// POST /api/categories/:categoryId/subjects - Add subject to category
|
||||||
router.post('/:categoryId/subjects', categoryController.addSubjectToCategory);
|
router.post('/:categoryId/subjects', categoryController.addSubjectToCategory);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user