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

98 lines
2.7 KiB
TypeScript
Raw Normal View History

import { Router, Request, Response, NextFunction } from 'express';
import * as mapService from '../services/mapService';
import { ok } from '../types/index';
import type { NodeType } from '../lib/constants';
export const serviceMapRouter = Router();
serviceMapRouter.get('/', async (_req: Request, res: Response, next: NextFunction) => {
try {
res.json(ok(await mapService.listMaps()));
} catch (e) {
next(e);
}
});
serviceMapRouter.post('/', async (req: Request, res: Response, next: NextFunction) => {
try {
const { name, description } = req.body as { name: string; description?: string };
res.status(201).json(ok(await mapService.createMap({ name, description })));
} catch (e) {
next(e);
}
});
serviceMapRouter.get('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
res.json(ok(await mapService.getMap(req.params.id)));
} catch (e) {
next(e);
}
});
serviceMapRouter.put('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
const { name, description } = req.body as { name?: string; description?: string };
res.json(ok(await mapService.updateMap(req.params.id, { name, description })));
} catch (e) {
next(e);
}
});
serviceMapRouter.delete('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
await mapService.deleteMap(req.params.id);
res.json(ok(null));
} catch (e) {
next(e);
}
});
serviceMapRouter.post('/:id/nodes', async (req: Request, res: Response, next: NextFunction) => {
try {
const { label, nodeType, positionX, positionY, metadata, color, icon, moduleId } = req.body as {
label: string;
nodeType: NodeType;
positionX: number;
positionY: number;
metadata?: string;
color?: string;
icon?: string;
moduleId?: string;
};
res.status(201).json(
ok(await mapService.addNode(req.params.id, { label, nodeType, positionX, positionY, metadata, color, icon, moduleId }))
);
} catch (e) {
next(e);
}
});
serviceMapRouter.post('/:id/populate', async (req: Request, res: Response, next: NextFunction) => {
try {
res.json(ok(await mapService.populateFromRack(req.params.id)));
} catch (e) {
next(e);
}
});
serviceMapRouter.post('/:id/edges', async (req: Request, res: Response, next: NextFunction) => {
try {
const { sourceId, targetId, label, edgeType, animated, metadata } = req.body as {
sourceId: string;
targetId: string;
label?: string;
edgeType?: string;
animated?: boolean;
metadata?: string;
};
res.status(201).json(
ok(await mapService.addEdge(req.params.id, { sourceId, targetId, label, edgeType, animated, metadata }))
);
} catch (e) {
next(e);
}
});
export { mapService };