First commit
This commit is contained in:
186
server/routes/devices.js
Normal file
186
server/routes/devices.js
Normal file
@@ -0,0 +1,186 @@
|
||||
/**
|
||||
* Devices Routes
|
||||
* All routes related to device management
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const db = require('../db');
|
||||
const config = require('../config');
|
||||
|
||||
/**
|
||||
* GET /api/device-types
|
||||
* Get all device types
|
||||
*/
|
||||
router.get('/types', (req, res, next) => {
|
||||
try {
|
||||
const types = db.getAllDeviceTypes();
|
||||
res.json(types);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/devices
|
||||
* Get all devices for a project
|
||||
*/
|
||||
router.get('/', (req, res, next) => {
|
||||
try {
|
||||
const projectId = req.query.projectId || 1;
|
||||
const devices = db.getAllDevices(projectId);
|
||||
res.json(devices);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/devices/:id/used-ports
|
||||
* Get used ports for a device
|
||||
*/
|
||||
router.get('/:id/used-ports', (req, res, next) => {
|
||||
try {
|
||||
const ports = db.getUsedPorts(req.params.id);
|
||||
res.json(ports);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/devices
|
||||
* Create a new device
|
||||
*/
|
||||
router.post('/', (req, res, next) => {
|
||||
try {
|
||||
const { deviceTypeId, rackId, position, name } = req.body;
|
||||
|
||||
// Validation
|
||||
if (!deviceTypeId) {
|
||||
return res.status(400).json({ error: 'Device type ID is required' });
|
||||
}
|
||||
if (!rackId) {
|
||||
return res.status(400).json({ error: 'Rack ID is required' });
|
||||
}
|
||||
if (typeof position !== 'number' || position < 1 || position > config.rack.slots) {
|
||||
return res.status(400).json({
|
||||
error: `Position must be between 1 and ${config.rack.slots}`
|
||||
});
|
||||
}
|
||||
if (!name || name.trim().length === 0) {
|
||||
return res.status(400).json({ error: 'Device name is required' });
|
||||
}
|
||||
|
||||
// Get project_id from the rack
|
||||
const rack = db.db.prepare('SELECT project_id FROM racks WHERE id = ?').get(rackId);
|
||||
if (!rack) {
|
||||
return res.status(404).json({ error: 'Rack not found' });
|
||||
}
|
||||
|
||||
const device = db.createDevice(deviceTypeId, rackId, rack.project_id, position, name.trim());
|
||||
res.status(201).json(device);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/devices/:id/rack
|
||||
* Update device rack and position
|
||||
*/
|
||||
router.put('/:id/rack', (req, res, next) => {
|
||||
try {
|
||||
const { rackId, position } = req.body;
|
||||
|
||||
if (!rackId) {
|
||||
return res.status(400).json({ error: 'Rack ID is required' });
|
||||
}
|
||||
if (typeof position !== 'number' || position < 1 || position > config.rack.slots) {
|
||||
return res.status(400).json({
|
||||
error: `Position must be between 1 and ${config.rack.slots}`
|
||||
});
|
||||
}
|
||||
|
||||
db.updateDeviceRack(req.params.id, rackId, position);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/devices/:id/logical-position
|
||||
* Update device logical view position
|
||||
*/
|
||||
router.put('/:id/logical-position', (req, res, next) => {
|
||||
try {
|
||||
const { x, y } = req.body;
|
||||
|
||||
if (typeof x !== 'number' || typeof y !== 'number') {
|
||||
return res.status(400).json({ error: 'Valid x and y coordinates are required' });
|
||||
}
|
||||
|
||||
db.updateDeviceLogicalPosition(req.params.id, x, y);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/devices/:id/name
|
||||
* Update device name
|
||||
*/
|
||||
router.put('/:id/name', (req, res, next) => {
|
||||
try {
|
||||
const { name } = req.body;
|
||||
|
||||
if (!name || name.trim().length === 0) {
|
||||
return res.status(400).json({ error: 'Device name is required' });
|
||||
}
|
||||
|
||||
db.updateDeviceName(req.params.id, name.trim());
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/devices/:id/rack-units
|
||||
* Update device rack units (form factor)
|
||||
*/
|
||||
router.put('/:id/rack-units', (req, res, next) => {
|
||||
try {
|
||||
const { rackUnits } = req.body;
|
||||
const min = config.device.minRackUnits || 1;
|
||||
const max = config.device.maxRackUnits || config.rack.slots;
|
||||
|
||||
if (typeof rackUnits !== 'number' || rackUnits < min || rackUnits > max) {
|
||||
return res.status(400).json({
|
||||
error: `Rack units must be between ${min} and ${max}`
|
||||
});
|
||||
}
|
||||
|
||||
db.updateDeviceRackUnits(req.params.id, rackUnits);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/devices/:id
|
||||
* Delete a device
|
||||
*/
|
||||
router.delete('/:id', (req, res, next) => {
|
||||
try {
|
||||
db.deleteDevice(req.params.id);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user