This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
const { StudentDetail, UserProfile, UsersAuth, Class } = require('../models');
|
||||
const { cacheUtils } = require('../config/redis');
|
||||
const { addDatabaseWriteJob } = require('../config/bullmq');
|
||||
|
||||
/**
|
||||
* Student Controller - Quản lý học sinh
|
||||
@@ -120,21 +119,20 @@ class StudentController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new student (async via BullMQ)
|
||||
* Create new student
|
||||
*/
|
||||
async createStudent(req, res, next) {
|
||||
try {
|
||||
const studentData = req.body;
|
||||
|
||||
const job = await addDatabaseWriteJob('create', 'StudentDetail', studentData);
|
||||
const newStudent = await StudentDetail.create(studentData);
|
||||
|
||||
await cacheUtils.deletePattern('students:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Student creation job queued',
|
||||
jobId: job.id,
|
||||
data: studentData,
|
||||
message: 'Student created successfully',
|
||||
data: newStudent,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -142,25 +140,30 @@ class StudentController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update student (async via BullMQ)
|
||||
* Update student
|
||||
*/
|
||||
async updateStudent(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const updates = req.body;
|
||||
|
||||
const job = await addDatabaseWriteJob('update', 'StudentDetail', {
|
||||
id,
|
||||
updates,
|
||||
});
|
||||
const student = await StudentDetail.findByPk(id);
|
||||
if (!student) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Student not found',
|
||||
});
|
||||
}
|
||||
|
||||
await student.update(updates);
|
||||
|
||||
await cacheUtils.delete(`student:${id}`);
|
||||
await cacheUtils.deletePattern('students:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Student update job queued',
|
||||
jobId: job.id,
|
||||
message: 'Student updated successfully',
|
||||
data: student,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -174,18 +177,23 @@ class StudentController {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const job = await addDatabaseWriteJob('update', 'StudentDetail', {
|
||||
id,
|
||||
updates: { status: 'dropped' },
|
||||
});
|
||||
const student = await StudentDetail.findByPk(id);
|
||||
if (!student) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Student not found',
|
||||
});
|
||||
}
|
||||
|
||||
await student.update({ status: 'dropped' });
|
||||
|
||||
await cacheUtils.delete(`student:${id}`);
|
||||
await cacheUtils.deletePattern('students:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Student deletion job queued',
|
||||
jobId: job.id,
|
||||
message: 'Student status updated to dropped',
|
||||
data: student,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
|
||||
Reference in New Issue
Block a user