update
This commit is contained in:
270
controllers/academicYearController.js
Normal file
270
controllers/academicYearController.js
Normal file
@@ -0,0 +1,270 @@
|
||||
const { AcademicYear } = require('../models');
|
||||
const { cacheUtils } = require('../config/redis');
|
||||
const { addDatabaseWriteJob } = require('../config/bullmq');
|
||||
|
||||
/**
|
||||
* Academic Year Controller - Quản lý năm học
|
||||
*/
|
||||
class AcademicYearController {
|
||||
/**
|
||||
* Get all academic years with pagination and caching
|
||||
*/
|
||||
async getAllAcademicYears(req, res, next) {
|
||||
try {
|
||||
const { page = 1, limit = 20, is_current } = req.query;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
// Generate cache key
|
||||
const cacheKey = `academic_years:list:${page}:${limit}:${is_current || 'all'}`;
|
||||
|
||||
// Try to get from cache first
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Build query conditions
|
||||
const where = {};
|
||||
if (is_current !== undefined) where.is_current = is_current === 'true';
|
||||
|
||||
// Query from database (through ProxySQL)
|
||||
const { count, rows } = await AcademicYear.findAndCountAll({
|
||||
where,
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
order: [['start_date', 'DESC']],
|
||||
});
|
||||
|
||||
const result = {
|
||||
academicYears: rows,
|
||||
pagination: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
totalPages: Math.ceil(count / limit),
|
||||
},
|
||||
};
|
||||
|
||||
// Cache the result
|
||||
await cacheUtils.set(cacheKey, result, 3600); // Cache for 1 hour
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get academic year by ID with caching
|
||||
*/
|
||||
async getAcademicYearById(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const cacheKey = `academic_year:${id}`;
|
||||
|
||||
// Try cache first
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Query from database
|
||||
const academicYear = await AcademicYear.findByPk(id);
|
||||
|
||||
if (!academicYear) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Academic year not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Cache the result
|
||||
await cacheUtils.set(cacheKey, academicYear, 7200); // Cache for 2 hours
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: academicYear,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current academic year with caching
|
||||
*/
|
||||
async getCurrentAcademicYear(req, res, next) {
|
||||
try {
|
||||
const cacheKey = 'academic_year:current';
|
||||
|
||||
// Try cache first
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Query from database
|
||||
const academicYear = await AcademicYear.findOne({
|
||||
where: { is_current: true },
|
||||
});
|
||||
|
||||
if (!academicYear) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'No current academic year found',
|
||||
});
|
||||
}
|
||||
|
||||
// Cache the result
|
||||
await cacheUtils.set(cacheKey, academicYear, 3600); // Cache for 1 hour
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: academicYear,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new academic year (async via BullMQ)
|
||||
*/
|
||||
async createAcademicYear(req, res, next) {
|
||||
try {
|
||||
const academicYearData = req.body;
|
||||
|
||||
// Add to job queue for async processing
|
||||
const job = await addDatabaseWriteJob('create', 'AcademicYear', academicYearData);
|
||||
|
||||
// Invalidate related caches
|
||||
await cacheUtils.deletePattern('academic_years:list:*');
|
||||
await cacheUtils.delete('academic_year:current');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Academic year creation job queued',
|
||||
jobId: job.id,
|
||||
data: academicYearData,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update academic year (async via BullMQ)
|
||||
*/
|
||||
async updateAcademicYear(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const updates = req.body;
|
||||
|
||||
// Add to job queue for async processing
|
||||
const job = await addDatabaseWriteJob('update', 'AcademicYear', {
|
||||
id,
|
||||
updates,
|
||||
});
|
||||
|
||||
// Invalidate related caches
|
||||
await cacheUtils.delete(`academic_year:${id}`);
|
||||
await cacheUtils.deletePattern('academic_years:list:*');
|
||||
await cacheUtils.delete('academic_year:current');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Academic year update job queued',
|
||||
jobId: job.id,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete academic year (async via BullMQ)
|
||||
*/
|
||||
async deleteAcademicYear(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// Add to job queue for async processing
|
||||
const job = await addDatabaseWriteJob('delete', 'AcademicYear', { id });
|
||||
|
||||
// Invalidate related caches
|
||||
await cacheUtils.delete(`academic_year:${id}`);
|
||||
await cacheUtils.deletePattern('academic_years:list:*');
|
||||
await cacheUtils.delete('academic_year:current');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Academic year deletion job queued',
|
||||
jobId: job.id,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set academic year as current
|
||||
*/
|
||||
async setCurrentAcademicYear(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// Add to job queue to update current year
|
||||
const job = await addDatabaseWriteJob('update', 'AcademicYear', {
|
||||
id,
|
||||
updates: { is_current: true },
|
||||
// Will also set all other years to is_current: false
|
||||
});
|
||||
|
||||
// Invalidate all academic year caches
|
||||
await cacheUtils.deletePattern('academic_year*');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Set current academic year job queued',
|
||||
jobId: job.id,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get academic year datatypes
|
||||
*/
|
||||
async getAcademicYearDatatypes(req, res, next) {
|
||||
try {
|
||||
const datatypes = AcademicYear.rawAttributes;
|
||||
res.json({
|
||||
success: true,
|
||||
data: datatypes,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new AcademicYearController();
|
||||
Reference in New Issue
Block a user