50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
|
|
import { Router, Request, Response, NextFunction } from 'express';
|
||
|
|
import * as vlanService from '../services/vlanService';
|
||
|
|
import { ok } from '../types/index';
|
||
|
|
|
||
|
|
export const vlansRouter = Router();
|
||
|
|
|
||
|
|
vlansRouter.get('/', async (_req: Request, res: Response, next: NextFunction) => {
|
||
|
|
try {
|
||
|
|
res.json(ok(await vlanService.listVlans()));
|
||
|
|
} catch (e) {
|
||
|
|
next(e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
vlansRouter.post('/', async (req: Request, res: Response, next: NextFunction) => {
|
||
|
|
try {
|
||
|
|
const { vlanId, name, description, color } = req.body as {
|
||
|
|
vlanId: number;
|
||
|
|
name: string;
|
||
|
|
description?: string;
|
||
|
|
color?: string;
|
||
|
|
};
|
||
|
|
res.status(201).json(ok(await vlanService.createVlan({ vlanId, name, description, color })));
|
||
|
|
} catch (e) {
|
||
|
|
next(e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
vlansRouter.put('/:id', async (req: Request, res: Response, next: NextFunction) => {
|
||
|
|
try {
|
||
|
|
const { name, description, color } = req.body as {
|
||
|
|
name?: string;
|
||
|
|
description?: string;
|
||
|
|
color?: string;
|
||
|
|
};
|
||
|
|
res.json(ok(await vlanService.updateVlan(req.params.id, { name, description, color })));
|
||
|
|
} catch (e) {
|
||
|
|
next(e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
vlansRouter.delete('/:id', async (req: Request, res: Response, next: NextFunction) => {
|
||
|
|
try {
|
||
|
|
await vlanService.deleteVlan(req.params.id);
|
||
|
|
res.json(ok(null));
|
||
|
|
} catch (e) {
|
||
|
|
next(e);
|
||
|
|
}
|
||
|
|
});
|