148 lines
3.9 KiB
JavaScript
148 lines
3.9 KiB
JavaScript
/**
|
|
* 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;
|