update
This commit is contained in:
219
controllers/roomController.js
Normal file
219
controllers/roomController.js
Normal file
@@ -0,0 +1,219 @@
|
||||
const { Room, School } = require('../models');
|
||||
const { cacheUtils } = require('../config/redis');
|
||||
const { addDatabaseWriteJob } = require('../config/bullmq');
|
||||
|
||||
/**
|
||||
* Room Controller - Quản lý phòng học
|
||||
*/
|
||||
class RoomController {
|
||||
/**
|
||||
* Get all rooms with pagination and caching
|
||||
*/
|
||||
async getAllRooms(req, res, next) {
|
||||
try {
|
||||
const { page = 1, limit = 50, school_id, room_type } = req.query;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const cacheKey = `rooms:list:${page}:${limit}:${school_id || 'all'}:${room_type || 'all'}`;
|
||||
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
const where = {};
|
||||
if (school_id) where.school_id = school_id;
|
||||
if (room_type) where.room_type = room_type;
|
||||
|
||||
const { count, rows } = await Room.findAndCountAll({
|
||||
where,
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
order: [['school_id', 'ASC'], ['floor', 'ASC'], ['room_code', 'ASC']],
|
||||
});
|
||||
|
||||
const result = {
|
||||
rooms: rows,
|
||||
pagination: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
totalPages: Math.ceil(count / limit),
|
||||
},
|
||||
};
|
||||
|
||||
await cacheUtils.set(cacheKey, result, 3600);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get room by ID
|
||||
*/
|
||||
async getRoomById(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const cacheKey = `room:${id}`;
|
||||
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
const room = await Room.findByPk(id);
|
||||
|
||||
if (!room) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Room not found',
|
||||
});
|
||||
}
|
||||
|
||||
await cacheUtils.set(cacheKey, room, 7200);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: room,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new room (async via BullMQ)
|
||||
*/
|
||||
async createRoom(req, res, next) {
|
||||
try {
|
||||
const roomData = req.body;
|
||||
|
||||
const job = await addDatabaseWriteJob('create', 'Room', roomData);
|
||||
|
||||
await cacheUtils.deletePattern('rooms:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Room creation job queued',
|
||||
jobId: job.id,
|
||||
data: roomData,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update room (async via BullMQ)
|
||||
*/
|
||||
async updateRoom(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const updates = req.body;
|
||||
|
||||
const job = await addDatabaseWriteJob('update', 'Room', {
|
||||
id,
|
||||
updates,
|
||||
});
|
||||
|
||||
await cacheUtils.delete(`room:${id}`);
|
||||
await cacheUtils.deletePattern('rooms:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Room update job queued',
|
||||
jobId: job.id,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete room (async via BullMQ)
|
||||
*/
|
||||
async deleteRoom(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const job = await addDatabaseWriteJob('delete', 'Room', { id });
|
||||
|
||||
await cacheUtils.delete(`room:${id}`);
|
||||
await cacheUtils.deletePattern('rooms:list:*');
|
||||
|
||||
res.status(202).json({
|
||||
success: true,
|
||||
message: 'Room deletion job queued',
|
||||
jobId: job.id,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rooms by school
|
||||
*/
|
||||
async getRoomsBySchool(req, res, next) {
|
||||
try {
|
||||
const { school_id } = req.params;
|
||||
const cacheKey = `rooms:school:${school_id}`;
|
||||
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
const rooms = await Room.findAll({
|
||||
where: { school_id },
|
||||
order: [['floor', 'ASC'], ['room_code', 'ASC']],
|
||||
});
|
||||
|
||||
await cacheUtils.set(cacheKey, rooms, 7200);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: rooms,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get room datatypes
|
||||
*/
|
||||
async getRoomDatatypes(req, res, next) {
|
||||
try {
|
||||
const datatypes = Room.rawAttributes;
|
||||
res.json({
|
||||
success: true,
|
||||
data: datatypes,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new RoomController();
|
||||
Reference in New Issue
Block a user