U
All checks were successful
Deploy to Production / deploy (push) Successful in 20s

This commit is contained in:
silverpro89
2026-02-27 20:10:15 +07:00
parent 6287a019e3
commit f96833a7e4
10 changed files with 37 additions and 58 deletions

View File

@@ -291,7 +291,7 @@ class CategoryController {
// Generate cache key
const cacheKey = `category:${id}:subjects:${page}:${limit}:${is_active || 'all'}`;
// Try cache first
/* Try cache first
const cached = await cacheUtils.get(cacheKey);
if (cached) {
return res.json({
@@ -300,7 +300,7 @@ class CategoryController {
cached: true,
});
}
*/
// Check if category exists
const category = await Categories.findByPk(id);
if (!category) {
@@ -356,7 +356,14 @@ class CategoryController {
async addSubjectToCategory(req, res, next) {
try {
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
const category = await Categories.findByPk(categoryId);
@@ -367,17 +374,23 @@ class CategoryController {
});
}
// Create subject with category_id
const subject = await Subject.create({
...subjectData,
category_id: categoryId,
// Find existing subject
const subject = await Subject.findByPk(id);
if (!subject) {
return res.status(404).json({
success: false,
message: 'Subject not found',
});
}
// Assign category
await subject.update({ category_id: categoryId });
// Clear cache
await cacheUtils.deletePattern('subjects:list:*');
await cacheUtils.deletePattern(`category:${categoryId}:subjects:*`);
res.status(201).json({
res.json({
success: true,
message: 'Subject added to category successfully',
data: subject,

View File

@@ -66,9 +66,6 @@
{
"grade": "010106",
"vocab": [
"Rosy",
"Tim",
"Billy",
"Mom",
"clean",
"tidy",

View File

@@ -75,10 +75,7 @@
"ao dai"
],
"phonics": null,
"grammar": [
"They are dancers from Viet Nam.",
"They have fans."
]
"grammar": null
},
{
"grade": "010208",

View File

@@ -91,9 +91,7 @@
"phonics": null,
"grammar": [
"A flower!",
"A frog!",
"It's a bird.",
"score a goal"
"A frog!"
]
},
{

View File

@@ -79,8 +79,7 @@
"phonics": null,
"grammar": [
"This is...",
"These are...",
"Let's take care in the sun."
"These are..."
]
},
{

View File

@@ -69,8 +69,7 @@
"vocab": [],
"phonics": null,
"grammar": [
"I have ...",
"Share with others."
"I have ..."
]
},
{

View File

@@ -3,10 +3,7 @@
"grade": "010001",
"vocab": [
"hello",
"goodbye",
"Rosy",
"Tim",
"Billy"
"goodbye"
],
"phonics": null,
"grammar": [
@@ -18,17 +15,9 @@
},
{
"grade": "010002",
"vocab": [
"hello",
"goodbye",
"Rosy",
"Tim",
"Billy"
],
"vocab": null,
"phonics": null,
"grammar": [
"What's your name?",
"I'm ...",
"Stand up",
"Sit down",
"Line up",
@@ -78,16 +67,10 @@
"vocab": [
"hello",
"goodbye",
"Rosy",
"Tim",
"Billy",
"Mom",
"Miss Bell"
"Mom"
],
"phonics": null,
"grammar": [
"It is good to say Hello and Goodbye."
]
"grammar": null
},
{
"grade": "010007",
@@ -101,8 +84,7 @@
],
"phonics": null,
"grammar": [
"I'm from ...",
"It's red and white."
"I'm from ..."
]
},
{

View File

@@ -4,14 +4,10 @@
"vocab": [
"pink",
"brown",
"white",
"Hello",
"Nice to meet you"
"white"
],
"phonics": null,
"grammar": [
"Hello.",
"Nice to meet you.",
"This is...",
"What color is it?",
"It's..."

View File

@@ -8,8 +8,7 @@
],
"phonics": null,
"grammar": [
"What's this?",
"Point and say."
"What's this?"
]
},
{
@@ -159,8 +158,7 @@
],
"phonics": null,
"grammar": [
"Hello, I'm... the lion.",
"Nice to meet you."
"Hello, I'm... the lion."
]
}
]

View File

@@ -21,9 +21,6 @@ router.get('/code/:code', categoryController.getCategoryByCode);
// GET /api/categories/:id - Get category by ID
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
router.post('/', categoryController.createCategory);
@@ -34,6 +31,9 @@ router.put('/:id', categoryController.updateCategory);
router.delete('/:id', categoryController.deleteCategory);
// ============ 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
router.post('/:categoryId/subjects', categoryController.addSubjectToCategory);