Files
sena_db_api_layer/controllers/academicYearController.js
silverpro89 3791b7cae1
All checks were successful
Deploy to Production / deploy (push) Successful in 21s
update
2026-01-28 11:21:21 +07:00

295 lines
7.0 KiB
JavaScript

const { AcademicYear } = require('../models');
const { cacheUtils } = require('../config/redis');
/**
* 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 createAcademicYear(req, res, next) {
try {
const academicYearData = req.body;
// Create academic year directly
const academicYear = await AcademicYear.create(academicYearData);
// Invalidate related caches
await cacheUtils.deletePattern('academic_years:list:*');
await cacheUtils.delete('academic_year:current');
res.status(201).json({
success: true,
message: 'Academic year created successfully',
data: academicYear,
});
} catch (error) {
next(error);
}
}
/**
* Update academic year
*/
async updateAcademicYear(req, res, next) {
try {
const { id } = req.params;
const updates = req.body;
const academicYear = await AcademicYear.findByPk(id);
if (!academicYear) {
return res.status(404).json({
success: false,
message: 'Academic year not found',
});
}
// Update academic year directly
await academicYear.update(updates);
// Invalidate related caches
await cacheUtils.delete(`academic_year:${id}`);
await cacheUtils.deletePattern('academic_years:list:*');
await cacheUtils.delete('academic_year:current');
res.json({
success: true,
message: 'Academic year updated successfully',
data: academicYear,
});
} catch (error) {
next(error);
}
}
/**
* Delete academic year
*/
async deleteAcademicYear(req, res, next) {
try {
const { id } = req.params;
const academicYear = await AcademicYear.findByPk(id);
if (!academicYear) {
return res.status(404).json({
success: false,
message: 'Academic year not found',
});
}
// Delete academic year directly
await academicYear.destroy();
// Invalidate related caches
await cacheUtils.delete(`academic_year:${id}`);
await cacheUtils.deletePattern('academic_years:list:*');
await cacheUtils.delete('academic_year:current');
res.json({
success: true,
message: 'Academic year deleted successfully',
});
} catch (error) {
next(error);
}
}
/**
* Set academic year as current
*/
async setCurrentAcademicYear(req, res, next) {
try {
const { id } = req.params;
const academicYear = await AcademicYear.findByPk(id);
if (!academicYear) {
return res.status(404).json({
success: false,
message: 'Academic year not found',
});
}
// Set all other years to not current
await AcademicYear.update(
{ is_current: false },
{ where: {} }
);
// Set this year as current
await academicYear.update({ is_current: true });
// Invalidate all academic year caches
await cacheUtils.deletePattern('academic_year*');
res.json({
success: true,
message: 'Academic year set as current successfully',
data: academicYear,
});
} 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();