96 lines
3.2 KiB
JavaScript
96 lines
3.2 KiB
JavaScript
const express = require('express');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
const db = require('../db');
|
|
const router = express.Router();
|
|
|
|
// GET /api/racks?roomId=
|
|
router.get('/', (req, res) => {
|
|
const { roomId } = req.query;
|
|
if (!roomId) return res.status(400).json({ error: 'roomId query param required' });
|
|
|
|
const racks = db.prepare(`
|
|
SELECT ra.*, COUNT(c.id) as component_count
|
|
FROM racks ra
|
|
LEFT JOIN components c ON c.rack_id = ra.id
|
|
WHERE ra.room_id = ?
|
|
GROUP BY ra.id
|
|
ORDER BY ra.name
|
|
`).all(roomId);
|
|
res.json(racks);
|
|
});
|
|
|
|
// GET /api/racks/:id
|
|
router.get('/:id', (req, res) => {
|
|
const rack = db.prepare('SELECT * FROM racks WHERE id = ?').get(req.params.id);
|
|
if (!rack) return res.status(404).json({ error: 'Rack not found' });
|
|
|
|
const components = db.prepare(`
|
|
SELECT * FROM components WHERE rack_id = ? ORDER BY position ASC NULLS LAST, name ASC
|
|
`).all(req.params.id);
|
|
|
|
// Attach ports to each component so the graphic view can show active ports
|
|
const componentsWithPorts = components.map(c => {
|
|
const ports = db.prepare('SELECT * FROM ports WHERE component_id = ? ORDER BY port_number').all(c.id);
|
|
return { ...c, ports };
|
|
});
|
|
|
|
let room = null, site = null;
|
|
if (rack.room_id) {
|
|
room = db.prepare('SELECT id, name, site_id FROM rooms WHERE id = ?').get(rack.room_id);
|
|
if (room) site = db.prepare('SELECT id, name FROM sites WHERE id = ?').get(room.site_id);
|
|
}
|
|
|
|
res.json({ ...rack, components: componentsWithPorts, room, site });
|
|
});
|
|
|
|
// POST /api/racks
|
|
router.post('/', (req, res) => {
|
|
const { room_id, name, total_units, manufacturer, model, notes } = req.body;
|
|
if (!name) return res.status(400).json({ error: 'name is required' });
|
|
|
|
if (room_id) {
|
|
const room = db.prepare('SELECT id FROM rooms WHERE id = ?').get(room_id);
|
|
if (!room) return res.status(404).json({ error: 'Room not found' });
|
|
}
|
|
|
|
const id = uuidv4();
|
|
db.prepare(`
|
|
INSERT INTO racks (id, room_id, name, total_units, manufacturer, model, notes)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
`).run(id, room_id || null, name, total_units || 42, manufacturer || null, model || null, notes || '');
|
|
|
|
res.status(201).json(db.prepare('SELECT * FROM racks WHERE id = ?').get(id));
|
|
});
|
|
|
|
// PUT /api/racks/:id
|
|
router.put('/:id', (req, res) => {
|
|
const rack = db.prepare('SELECT * FROM racks WHERE id = ?').get(req.params.id);
|
|
if (!rack) return res.status(404).json({ error: 'Rack not found' });
|
|
|
|
const { name, total_units, manufacturer, model, notes } = req.body;
|
|
db.prepare(`
|
|
UPDATE racks SET name = ?, total_units = ?, manufacturer = ?, model = ?, notes = ?,
|
|
updated_at = CURRENT_TIMESTAMP WHERE id = ?
|
|
`).run(
|
|
name ?? rack.name,
|
|
total_units ?? rack.total_units,
|
|
manufacturer ?? rack.manufacturer,
|
|
model ?? rack.model,
|
|
notes ?? rack.notes,
|
|
req.params.id
|
|
);
|
|
|
|
res.json(db.prepare('SELECT * FROM racks WHERE id = ?').get(req.params.id));
|
|
});
|
|
|
|
// DELETE /api/racks/:id
|
|
router.delete('/:id', (req, res) => {
|
|
const rack = db.prepare('SELECT id FROM racks WHERE id = ?').get(req.params.id);
|
|
if (!rack) return res.status(404).json({ error: 'Rack not found' });
|
|
|
|
db.prepare('DELETE FROM racks WHERE id = ?').run(req.params.id);
|
|
res.json({ ok: true });
|
|
});
|
|
|
|
module.exports = router;
|