adding ports fixed
This commit is contained in:
@@ -172,12 +172,26 @@ router.get('/:id/ports', (req, res) => {
|
||||
|
||||
// POST /api/components/:id/ports
|
||||
router.post('/:id/ports', (req, res) => {
|
||||
const component = db.prepare('SELECT id FROM components WHERE id = ?').get(req.params.id);
|
||||
const component = db.prepare('SELECT id, type, port_count FROM components WHERE id = ?').get(req.params.id);
|
||||
if (!component) return res.status(404).json({ error: 'Component not found' });
|
||||
|
||||
const { port_number, label, port_type, connected_to_port_id, notes } = req.body;
|
||||
if (port_number == null) return res.status(400).json({ error: 'port_number is required' });
|
||||
|
||||
// Patch-panel specific: no duplicate port numbers, enforce port_count capacity
|
||||
if (component.type === 'patch_panel') {
|
||||
const existing = db.prepare('SELECT id FROM ports WHERE component_id = ? AND port_number = ?').get(req.params.id, port_number);
|
||||
if (existing) return res.status(409).json({ error: `Port ${port_number} already exists on this patch panel.` });
|
||||
|
||||
if (component.port_count != null) {
|
||||
const count = db.prepare('SELECT COUNT(*) as n FROM ports WHERE component_id = ?').get(req.params.id).n;
|
||||
if (count >= component.port_count)
|
||||
return res.status(409).json({ error: `Patch panel is full (${component.port_count} ports maximum).` });
|
||||
if (port_number < 1 || port_number > component.port_count)
|
||||
return res.status(400).json({ error: `Port number must be between 1 and ${component.port_count}.` });
|
||||
}
|
||||
}
|
||||
|
||||
const id = uuidv4();
|
||||
db.prepare(`
|
||||
INSERT INTO ports (id, component_id, port_number, label, port_type, connected_to_port_id, notes)
|
||||
|
||||
Reference in New Issue
Block a user