Sistemato attività
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { Request, Response, NextFunction, Router } from 'express';
|
||||
import { HttpError } from '../../errors';
|
||||
import * as notificheService from './notifiche.service';
|
||||
|
||||
function asyncHandler(
|
||||
handler: (req: Request, res: Response, next: NextFunction) => Promise<void>,
|
||||
) {
|
||||
return (req: Request, res: Response, next: NextFunction): void => {
|
||||
handler(req, res, next).catch(next);
|
||||
};
|
||||
}
|
||||
|
||||
function parseId(req: Request, next: NextFunction): number | undefined {
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id)) {
|
||||
next(new HttpError(400, "l'id deve essere un numero intero"));
|
||||
return undefined;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
export const notificheRouter = Router();
|
||||
|
||||
notificheRouter.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
res.status(200).json(await notificheService.listNotifiche(req.auth!));
|
||||
}),
|
||||
);
|
||||
|
||||
notificheRouter.get(
|
||||
'/non-lette/count',
|
||||
asyncHandler(async (req, res) => {
|
||||
res.status(200).json({ count: await notificheService.countNonLette(req.auth!) });
|
||||
}),
|
||||
);
|
||||
|
||||
notificheRouter.put(
|
||||
'/letta-tutte',
|
||||
asyncHandler(async (req, res) => {
|
||||
await notificheService.segnaTutteLette(req.auth!);
|
||||
res.status(204).end();
|
||||
}),
|
||||
);
|
||||
|
||||
notificheRouter.put(
|
||||
'/:id/letta',
|
||||
asyncHandler(async (req, res, next) => {
|
||||
const id = parseId(req, next);
|
||||
if (id === undefined) return;
|
||||
|
||||
await notificheService.segnaLetta(id, req.auth!);
|
||||
res.status(204).end();
|
||||
}),
|
||||
);
|
||||
|
||||
export default notificheRouter;
|
||||
Reference in New Issue
Block a user