feat(validation): 为机房、机柜和设备路由添加Joi验证中间件 feat(hooks): 创建useDesignTokens Hook集中管理主题配置 feat(models): 为耗材模型添加乐观锁version字段和updatedAt索引 feat(3d): 增强3D场景和设备模型视觉效果与交互 refactor: 移除数据备份相关功能并优化代码结构 fix: 修复前端安全日志和密码加密工具 chore: 更新依赖并添加axios和joi库 docs: 更新注释和文档说明 style: 改进代码格式和命名一致性
84 lines
2.2 KiB
JavaScript
84 lines
2.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const Room = require('../models/Room');
|
|
const Rack = require('../models/Rack');
|
|
const { validateBody } = require('../middleware/validation');
|
|
const { createRoomSchema, updateRoomSchema } = require('../validation/roomSchema');
|
|
|
|
// 获取所有机房
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const rooms = await Room.findAll({
|
|
include: Rack
|
|
});
|
|
res.json(rooms);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// 获取单个机房
|
|
router.get('/:roomId', async (req, res) => {
|
|
try {
|
|
const room = await Room.findByPk(req.params.roomId, {
|
|
include: Rack
|
|
});
|
|
if (!room) {
|
|
return res.status(404).json({ error: '机房不存在' });
|
|
}
|
|
res.json(room);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// 创建机房
|
|
router.post('/', async (req, res) => {
|
|
try {
|
|
const room = await Room.create(req.body);
|
|
res.status(201).json(room);
|
|
} catch (error) {
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新机房
|
|
router.put('/:roomId', validateBody(updateRoomSchema), async (req, res) => {
|
|
try {
|
|
const [updated] = await Room.update(req.body, {
|
|
where: { roomId: req.params.roomId }
|
|
});
|
|
if (updated) {
|
|
const updatedRoom = await Room.findByPk(req.params.roomId);
|
|
res.json(updatedRoom);
|
|
} else {
|
|
res.status(404).json({ error: '机房不存在' });
|
|
}
|
|
} catch (error) {
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除机房
|
|
router.delete('/:roomId', async (req, res) => {
|
|
try {
|
|
// 检查是否有机柜关联
|
|
const racks = await Rack.findAll({ where: { roomId: req.params.roomId } });
|
|
if (racks.length > 0) {
|
|
return res.status(400).json({ error: '该机房下有机柜,无法删除' });
|
|
}
|
|
|
|
const deleted = await Room.destroy({
|
|
where: { roomId: req.params.roomId }
|
|
});
|
|
if (deleted) {
|
|
res.status(204).json();
|
|
} else {
|
|
res.status(404).json({ error: '机房不存在' });
|
|
}
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router; |