/** * 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;