First commit
This commit is contained in:
147
server/routes/connections.js
Normal file
147
server/routes/connections.js
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* Connections Routes
|
||||
* All routes related to connection management
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const db = require('../db');
|
||||
|
||||
/**
|
||||
* GET /api/connections
|
||||
* Get all connections for a project
|
||||
*/
|
||||
router.get('/', (req, res, next) => {
|
||||
try {
|
||||
const projectId = req.query.projectId || 1;
|
||||
const connections = db.getAllConnections(projectId);
|
||||
res.json(connections);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/connections
|
||||
* Create a new connection
|
||||
*/
|
||||
router.post('/', (req, res, next) => {
|
||||
try {
|
||||
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = req.body;
|
||||
|
||||
// Validation
|
||||
if (!sourceDeviceId) {
|
||||
return res.status(400).json({ error: 'Source device ID is required' });
|
||||
}
|
||||
if (typeof sourcePort !== 'number' || sourcePort < 1) {
|
||||
return res.status(400).json({ error: 'Valid source port number is required' });
|
||||
}
|
||||
if (!targetDeviceId) {
|
||||
return res.status(400).json({ error: 'Target device ID is required' });
|
||||
}
|
||||
if (typeof targetPort !== 'number' || targetPort < 1) {
|
||||
return res.status(400).json({ error: 'Valid target port number is required' });
|
||||
}
|
||||
if (sourceDeviceId === targetDeviceId) {
|
||||
return res.status(400).json({ error: 'Cannot connect device to itself' });
|
||||
}
|
||||
|
||||
const connection = db.createConnection(
|
||||
sourceDeviceId,
|
||||
sourcePort,
|
||||
targetDeviceId,
|
||||
targetPort
|
||||
);
|
||||
res.status(201).json(connection);
|
||||
} catch (err) {
|
||||
// Handle unique constraint violations (port already in use)
|
||||
if (err.message && err.message.includes('UNIQUE constraint')) {
|
||||
return res.status(400).json({ error: 'One or both ports are already in use' });
|
||||
}
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/connections/:id
|
||||
* Update a connection
|
||||
*/
|
||||
router.put('/:id', (req, res, next) => {
|
||||
try {
|
||||
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = req.body;
|
||||
|
||||
// Validation
|
||||
if (!sourceDeviceId) {
|
||||
return res.status(400).json({ error: 'Source device ID is required' });
|
||||
}
|
||||
if (typeof sourcePort !== 'number' || sourcePort < 1) {
|
||||
return res.status(400).json({ error: 'Valid source port number is required' });
|
||||
}
|
||||
if (!targetDeviceId) {
|
||||
return res.status(400).json({ error: 'Target device ID is required' });
|
||||
}
|
||||
if (typeof targetPort !== 'number' || targetPort < 1) {
|
||||
return res.status(400).json({ error: 'Valid target port number is required' });
|
||||
}
|
||||
if (sourceDeviceId === targetDeviceId) {
|
||||
return res.status(400).json({ error: 'Cannot connect device to itself' });
|
||||
}
|
||||
|
||||
db.updateConnection(
|
||||
req.params.id,
|
||||
sourceDeviceId,
|
||||
sourcePort,
|
||||
targetDeviceId,
|
||||
targetPort
|
||||
);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
if (err.message && err.message.includes('UNIQUE constraint')) {
|
||||
return res.status(400).json({ error: 'One or both ports are already in use' });
|
||||
}
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/connections/:id/waypoints
|
||||
* Update connection waypoints
|
||||
*/
|
||||
router.put('/:id/waypoints', (req, res, next) => {
|
||||
try {
|
||||
const { waypoints, view } = req.body;
|
||||
|
||||
if (!waypoints || !Array.isArray(waypoints)) {
|
||||
return res.status(400).json({ error: 'Waypoints must be an array' });
|
||||
}
|
||||
|
||||
// Validate waypoint structure
|
||||
for (const waypoint of waypoints) {
|
||||
if (typeof waypoint.x !== 'number' || typeof waypoint.y !== 'number') {
|
||||
return res.status(400).json({
|
||||
error: 'Each waypoint must have valid x and y coordinates'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
db.updateConnectionWaypoints(req.params.id, waypoints, view);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/connections/:id
|
||||
* Delete a connection
|
||||
*/
|
||||
router.delete('/:id', (req, res, next) => {
|
||||
try {
|
||||
db.deleteConnection(req.params.id);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
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;
|
||||
90
server/routes/projects.js
Normal file
90
server/routes/projects.js
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Projects Routes
|
||||
* All routes related to project management
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const db = require('../db');
|
||||
|
||||
/**
|
||||
* GET /api/projects
|
||||
* Get all projects
|
||||
*/
|
||||
router.get('/', (req, res, next) => {
|
||||
try {
|
||||
const projects = db.getAllProjects();
|
||||
res.json(projects);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/projects/:id
|
||||
* Get a specific project
|
||||
*/
|
||||
router.get('/:id', (req, res, next) => {
|
||||
try {
|
||||
const project = db.getProject(req.params.id);
|
||||
if (!project) {
|
||||
return res.status(404).json({ error: 'Project not found' });
|
||||
}
|
||||
res.json(project);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/projects
|
||||
* Create a new project
|
||||
*/
|
||||
router.post('/', (req, res, next) => {
|
||||
try {
|
||||
const { name, description } = req.body;
|
||||
|
||||
if (!name || name.trim().length === 0) {
|
||||
return res.status(400).json({ error: 'Project name is required' });
|
||||
}
|
||||
|
||||
const project = db.createProject(name.trim(), description || '');
|
||||
res.status(201).json(project);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/projects/:id
|
||||
* Update a project
|
||||
*/
|
||||
router.put('/:id', (req, res, next) => {
|
||||
try {
|
||||
const { name, description } = req.body;
|
||||
|
||||
if (!name || name.trim().length === 0) {
|
||||
return res.status(400).json({ error: 'Project name is required' });
|
||||
}
|
||||
|
||||
db.updateProject(req.params.id, name.trim(), description || '');
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/projects/:id
|
||||
* Delete a project
|
||||
*/
|
||||
router.delete('/:id', (req, res, next) => {
|
||||
try {
|
||||
db.deleteProject(req.params.id);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
113
server/routes/racks.js
Normal file
113
server/routes/racks.js
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Racks Routes
|
||||
* All routes related to rack management
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const db = require('../db');
|
||||
|
||||
/**
|
||||
* GET /api/racks
|
||||
* Get all racks for a project
|
||||
*/
|
||||
router.get('/', (req, res, next) => {
|
||||
try {
|
||||
const projectId = req.query.projectId || 1;
|
||||
const racks = db.getAllRacks(projectId);
|
||||
res.json(racks);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/racks/next-name
|
||||
* Get next available rack name for a prefix
|
||||
*/
|
||||
router.get('/next-name', (req, res, next) => {
|
||||
try {
|
||||
const projectId = req.query.projectId || 1;
|
||||
const prefix = req.query.prefix || 'RACK';
|
||||
const name = db.getNextRackName(projectId, prefix);
|
||||
res.json({ name });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/racks
|
||||
* Create a new rack
|
||||
*/
|
||||
router.post('/', (req, res, next) => {
|
||||
try {
|
||||
const { projectId, name, x, y } = req.body;
|
||||
|
||||
// Validation
|
||||
if (!name || name.trim().length === 0) {
|
||||
return res.status(400).json({ error: 'Rack name is required' });
|
||||
}
|
||||
if (typeof x !== 'number' || typeof y !== 'number') {
|
||||
return res.status(400).json({ error: 'Valid x and y coordinates are required' });
|
||||
}
|
||||
|
||||
const rack = db.createRack(projectId || 1, name.trim(), x, y);
|
||||
res.status(201).json(rack);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/racks/:id/position
|
||||
* Update rack position
|
||||
*/
|
||||
router.put('/:id/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.updateRackPosition(req.params.id, x, y);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* PUT /api/racks/:id/name
|
||||
* Update rack 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: 'Rack name is required' });
|
||||
}
|
||||
|
||||
db.updateRackName(req.params.id, name.trim());
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/racks/:id
|
||||
* Delete a rack
|
||||
*/
|
||||
router.delete('/:id', (req, res, next) => {
|
||||
try {
|
||||
db.deleteRack(req.params.id);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user