Files
rack-planner/server/routes/racks.ts

86 lines
2.5 KiB
TypeScript

import { Router, Request, Response, NextFunction } from 'express';
import * as rackService from '../services/rackService';
import * as moduleService from '../services/moduleService';
import { ok } from '../types/index';
import type { ModuleType, PortType } from '../lib/constants';
export const racksRouter = Router();
racksRouter.get('/', async (_req: Request, res: Response, next: NextFunction) => {
try {
res.json(ok(await rackService.listRacks()));
} catch (e) {
next(e);
}
});
racksRouter.post('/', async (req: Request, res: Response, next: NextFunction) => {
try {
const { name, totalU, location, displayOrder } = req.body as {
name: string;
totalU?: number;
location?: string;
displayOrder?: number;
};
res.status(201).json(ok(await rackService.createRack({ name, totalU, location, displayOrder })));
} catch (e) {
next(e);
}
});
racksRouter.get('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
res.json(ok(await rackService.getRack(req.params.id)));
} catch (e) {
next(e);
}
});
racksRouter.put('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
const { name, totalU, location, displayOrder } = req.body as {
name?: string;
totalU?: number;
location?: string;
displayOrder?: number;
};
res.json(ok(await rackService.updateRack(req.params.id, { name, totalU, location, displayOrder })));
} catch (e) {
next(e);
}
});
racksRouter.delete('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
await rackService.deleteRack(req.params.id);
res.json(ok(null));
} catch (e) {
next(e);
}
});
racksRouter.post('/:id/modules', async (req: Request, res: Response, next: NextFunction) => {
try {
const { name, type, uPosition, uSize, manufacturer, model, ipAddress, notes, portCount, portType, sfpCount, wanCount } =
req.body as {
name: string;
type: ModuleType;
uPosition: number;
uSize?: number;
manufacturer?: string;
model?: string;
ipAddress?: string;
notes?: string;
portCount?: number;
portType?: PortType;
sfpCount?: number;
wanCount?: number;
};
res.status(201).json(
ok(await moduleService.createModule(req.params.id, { name, type, uPosition, uSize, manufacturer, model, ipAddress, notes, portCount, portType, sfpCount, wanCount }))
);
} catch (e) {
next(e);
}
});