This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
const { Subject, Chapter } = require('../models');
|
||||
const { cacheUtils } = require('../config/redis');
|
||||
const { addDatabaseWriteJob } = require('../config/bullmq');
|
||||
|
||||
/**
|
||||
* Subject Controller - Quản lý môn học
|
||||
@@ -147,22 +146,20 @@ class SubjectController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new subject (async via BullMQ)
|
||||
* Create new subject
|
||||
*/
|
||||
async createSubject(req, res, next) {
|
||||
try {
|
||||
const subjectData = req.body;
|
||||
|
||||
// Add to job queue for async processing
|
||||
const job = await addDatabaseWriteJob('create', 'Subject', subjectData);
|
||||
const subject = await Subject.create(subjectData);
|
||||
|
||||
// Note: Cache will be invalidated by worker after successful creation
|
||||
await cacheUtils.deletePattern('subjects:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Subject creation job queued',
|
||||
jobId: job.id,
|
||||
data: subjectData,
|
||||
message: 'Subject created successfully',
|
||||
data: subject,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -170,25 +167,31 @@ class SubjectController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update subject (async via BullMQ)
|
||||
* Update subject
|
||||
*/
|
||||
async updateSubject(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const updates = req.body;
|
||||
|
||||
// Add to job queue for async processing
|
||||
const job = await addDatabaseWriteJob('update', 'Subject', {
|
||||
id,
|
||||
updates,
|
||||
});
|
||||
const subject = await Subject.findByPk(id);
|
||||
|
||||
// Note: Cache will be invalidated by worker after successful update
|
||||
if (!subject) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Subject not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.status(202).json({
|
||||
await subject.update(updates);
|
||||
|
||||
await cacheUtils.delete(`subject:${id}`);
|
||||
await cacheUtils.deletePattern('subjects:list:*');
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Subject update job queued',
|
||||
jobId: job.id,
|
||||
message: 'Subject updated successfully',
|
||||
data: subject,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -196,21 +199,29 @@ class SubjectController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete subject (async via BullMQ)
|
||||
* Delete subject
|
||||
*/
|
||||
async deleteSubject(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// Add to job queue for async processing
|
||||
const job = await addDatabaseWriteJob('delete', 'Subject', { id });
|
||||
const subject = await Subject.findByPk(id);
|
||||
|
||||
// Note: Cache will be invalidated by worker after successful deletion
|
||||
if (!subject) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Subject not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.status(202).json({
|
||||
await subject.destroy();
|
||||
|
||||
await cacheUtils.delete(`subject:${id}`);
|
||||
await cacheUtils.deletePattern('subjects:list:*');
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Subject deletion job queued',
|
||||
jobId: job.id,
|
||||
message: 'Subject deleted successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
|
||||
Reference in New Issue
Block a user