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

232 lines
4.9 KiB
JavaScript

const { Room, School } = require('../models');
const { cacheUtils } = require('../config/redis');
/**
* 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 createRoom(req, res, next) {
try {
const roomData = req.body;
const room = await Room.create(roomData);
await cacheUtils.deletePattern('rooms:list:*');
res.status(201).json({
success: true,
message: 'Room created successfully',
data: room,
});
} catch (error) {
next(error);
}
}
/**
* Update room
*/
async updateRoom(req, res, next) {
try {
const { id } = req.params;
const updates = req.body;
const room = await Room.findByPk(id);
if (!room) {
return res.status(404).json({
success: false,
message: 'Room not found',
});
}
await room.update(updates);
await cacheUtils.delete(`room:${id}`);
await cacheUtils.deletePattern('rooms:list:*');
res.json({
success: true,
message: 'Room updated successfully',
data: room,
});
} catch (error) {
next(error);
}
}
/**
* Delete room
*/
async deleteRoom(req, res, next) {
try {
const { id } = req.params;
const room = await Room.findByPk(id);
if (!room) {
return res.status(404).json({
success: false,
message: 'Room not found',
});
}
await room.destroy();
await cacheUtils.delete(`room:${id}`);
await cacheUtils.deletePattern('rooms:list:*');
res.json({
success: true,
message: 'Room deleted successfully',
});
} 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();