初始化IDC设备管理系统项目
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const DeviceField = require('../models/DeviceField');
|
||||
|
||||
// 获取所有字段配置
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const fields = await DeviceField.findAll({
|
||||
order: [['order', 'ASC']]
|
||||
});
|
||||
res.json(fields);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取单个字段配置
|
||||
router.get('/:fieldId', async (req, res) => {
|
||||
try {
|
||||
const field = await DeviceField.findByPk(req.params.fieldId);
|
||||
if (!field) {
|
||||
return res.status(404).json({ error: '字段不存在' });
|
||||
}
|
||||
res.json(field);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 创建字段配置
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const field = await DeviceField.create(req.body);
|
||||
res.status(201).json(field);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 更新字段配置
|
||||
router.put('/:fieldId', async (req, res) => {
|
||||
try {
|
||||
const [updated] = await DeviceField.update(req.body, {
|
||||
where: { fieldId: req.params.fieldId }
|
||||
});
|
||||
if (updated) {
|
||||
const updatedField = await DeviceField.findByPk(req.params.fieldId);
|
||||
res.json(updatedField);
|
||||
} else {
|
||||
res.status(404).json({ error: '字段不存在' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 删除字段配置
|
||||
router.delete('/:fieldId', async (req, res) => {
|
||||
try {
|
||||
const deleted = await DeviceField.destroy({
|
||||
where: { fieldId: req.params.fieldId }
|
||||
});
|
||||
if (deleted) {
|
||||
res.status(204).json();
|
||||
} else {
|
||||
res.status(404).json({ error: '字段不存在' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,124 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Device = require('../models/Device');
|
||||
const Rack = require('../models/Rack');
|
||||
|
||||
// 获取所有设备
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const devices = await Device.findAll({
|
||||
include: [
|
||||
{ model: Rack }
|
||||
]
|
||||
});
|
||||
res.json(devices);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取单个设备
|
||||
router.get('/:deviceId', async (req, res) => {
|
||||
try {
|
||||
const device = await Device.findByPk(req.params.deviceId, {
|
||||
include: [
|
||||
{ model: Rack }
|
||||
]
|
||||
});
|
||||
if (!device) {
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
res.json(device);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 创建设备
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const device = await Device.create(req.body);
|
||||
|
||||
// 更新机柜当前功率
|
||||
const rack = await Rack.findByPk(req.body.rackId);
|
||||
if (rack) {
|
||||
await rack.update({
|
||||
currentPower: rack.currentPower + req.body.powerConsumption
|
||||
});
|
||||
}
|
||||
|
||||
res.status(201).json(device);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 更新设备
|
||||
router.put('/:deviceId', async (req, res) => {
|
||||
try {
|
||||
// 获取旧设备信息以更新功率
|
||||
const oldDevice = await Device.findByPk(req.params.deviceId);
|
||||
if (!oldDevice) {
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
|
||||
const [updated] = await Device.update(req.body, {
|
||||
where: { deviceId: req.params.deviceId }
|
||||
});
|
||||
|
||||
if (updated) {
|
||||
// 更新机柜当前功率
|
||||
const rack = await Rack.findByPk(oldDevice.rackId);
|
||||
if (rack) {
|
||||
const powerDiff = req.body.powerConsumption - oldDevice.powerConsumption;
|
||||
await rack.update({
|
||||
currentPower: rack.currentPower + powerDiff
|
||||
});
|
||||
}
|
||||
|
||||
const updatedDevice = await Device.findByPk(req.params.deviceId, {
|
||||
include: [
|
||||
{ model: Rack }
|
||||
]
|
||||
});
|
||||
res.json(updatedDevice);
|
||||
} else {
|
||||
res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 删除设备
|
||||
router.delete('/:deviceId', async (req, res) => {
|
||||
try {
|
||||
// 获取设备信息以更新功率
|
||||
const device = await Device.findByPk(req.params.deviceId);
|
||||
if (!device) {
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
|
||||
const deleted = await Device.destroy({
|
||||
where: { deviceId: req.params.deviceId }
|
||||
});
|
||||
|
||||
if (deleted) {
|
||||
// 更新机柜当前功率
|
||||
const rack = await Rack.findByPk(device.rackId);
|
||||
if (rack) {
|
||||
await rack.update({
|
||||
currentPower: Math.max(0, rack.currentPower - device.powerConsumption)
|
||||
});
|
||||
}
|
||||
|
||||
res.status(204).json();
|
||||
} else {
|
||||
res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,94 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Rack = require('../models/Rack');
|
||||
const Device = require('../models/Device');
|
||||
const Room = require('../models/Room');
|
||||
|
||||
// 获取所有机柜
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const racks = await Rack.findAll({
|
||||
include: [
|
||||
{ model: Room },
|
||||
{ model: Device }
|
||||
]
|
||||
});
|
||||
res.json(racks);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取单个机柜
|
||||
router.get('/:rackId', async (req, res) => {
|
||||
try {
|
||||
const rack = await Rack.findByPk(req.params.rackId, {
|
||||
include: [
|
||||
{ model: Room },
|
||||
{ model: Device }
|
||||
]
|
||||
});
|
||||
if (!rack) {
|
||||
return res.status(404).json({ error: '机柜不存在' });
|
||||
}
|
||||
res.json(rack);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 创建机柜
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const rack = await Rack.create(req.body);
|
||||
res.status(201).json(rack);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 更新机柜
|
||||
router.put('/:rackId', async (req, res) => {
|
||||
try {
|
||||
const [updated] = await Rack.update(req.body, {
|
||||
where: { rackId: req.params.rackId }
|
||||
});
|
||||
if (updated) {
|
||||
const updatedRack = await Rack.findByPk(req.params.rackId, {
|
||||
include: [
|
||||
{ model: Room },
|
||||
{ model: Device }
|
||||
]
|
||||
});
|
||||
res.json(updatedRack);
|
||||
} else {
|
||||
res.status(404).json({ error: '机柜不存在' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 删除机柜
|
||||
router.delete('/:rackId', async (req, res) => {
|
||||
try {
|
||||
// 检查是否有设备关联
|
||||
const devices = await Device.findAll({ where: { rackId: req.params.rackId } });
|
||||
if (devices.length > 0) {
|
||||
return res.status(400).json({ error: '该机柜下有设备,无法删除' });
|
||||
}
|
||||
|
||||
const deleted = await Rack.destroy({
|
||||
where: { rackId: req.params.rackId }
|
||||
});
|
||||
if (deleted) {
|
||||
res.status(204).json();
|
||||
} else {
|
||||
res.status(404).json({ error: '机柜不存在' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,82 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Room = require('../models/Room');
|
||||
const Rack = require('../models/Rack');
|
||||
|
||||
// 获取所有机房
|
||||
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', 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;
|
||||
Reference in New Issue
Block a user