From d9c9830ec5289b82df6a66fe93b4c27c980b41bf Mon Sep 17 00:00:00 2001 From: Alessandre Laguierce Date: Wed, 4 Dec 2024 14:15:15 +0100 Subject: [PATCH] fix: fix fix of fix --- back-end/src/controllers/box.controller.ts | 35 +++++++++++++++++++++- back-end/src/routes/box.route.ts | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/back-end/src/controllers/box.controller.ts b/back-end/src/controllers/box.controller.ts index 922f176..aaaf913 100644 --- a/back-end/src/controllers/box.controller.ts +++ b/back-end/src/controllers/box.controller.ts @@ -38,7 +38,7 @@ const allBoxesFromDate: RequestHandler<{ date: string; }> = async (req, res) => res.render('boxes.ejs', { boxes }); }; -const boxAddPieces: RequestHandler<{ title: string|number; }> = async (req, res) => { +const boxAddPieces: RequestHandler<{ title: string; }> = async (req, res) => { const boxEither: Either = await boxService.getBox(req.params.title); if (boxEither.hasRight) { @@ -62,6 +62,34 @@ const boxAddPieces: RequestHandler<{ title: string|number; }> = async (req, res) res.redirect(`/boxes/${req.params.title}`); }; +const boxAddPiecesById: RequestHandler<{ id_box: string; }> = async (req, res) => { + if (isNaN(parseInt(req.params.id_box))) { + res.sendStatus(400); + return; + } + const boxEither: Either = await boxService.getBox(parseInt(req.params.id_box)); + + if (boxEither.hasRight) { + res.sendStatus(404); + return; + } + + const piece: Either = await getPiece(req.body.numero_piece); + if (piece.hasRight) { + res.status(404).send(eitherFormatError(piece)); + return; + } + + if (boxEither.left.pieces.map(p => p.first.id_piece).includes(piece.left.id_piece)) { + res.status(401).send({}); + return; + } + + await boxService.addPiecesToBox(boxEither.left.id, piece.left.id_piece, req.body.quantite); + + res.redirect(`/boxes/byid/${req.params.id_box}`); +}; + const boxRemovePiece: RequestHandler<{ title: string; }> = async (req, res) => { const boxEither: Either = await boxService.getBox(req.params.title); @@ -87,6 +115,10 @@ const boxRemovePiece: RequestHandler<{ title: string; }> = async (req, res) => { }; const boxRemovePieceById: RequestHandler<{ id_box: string; }> = async (req, res) => { + if (isNaN(parseInt(req.params.id_box))) { + res.sendStatus(400); + return; + } const boxEither: Either = await boxService.getBox(parseInt(req.params.id_box)); if (boxEither.hasRight) { @@ -150,6 +182,7 @@ const registerBox: RequestHandler = async (req, res, next) => { const boxController = { boxAddPieces, + boxAddPiecesById, boxRemovePiece, boxByTitle, allBoxes, diff --git a/back-end/src/routes/box.route.ts b/back-end/src/routes/box.route.ts index d39ad27..6948cbc 100644 --- a/back-end/src/routes/box.route.ts +++ b/back-end/src/routes/box.route.ts @@ -14,7 +14,7 @@ boxRouter.post("/:title", boxController.boxAddPieces); boxRouter.delete("/byid/:id_box", boxController.boxRemovePieceById); -boxRouter.post("/byid/:id_box", boxController.boxAddPieces); +boxRouter.post("/byid/:id_box", boxController.boxAddPiecesById); boxRouter.delete("/:title", boxController.boxRemovePiece); boxRouter.get("/byid/:id", boxController.boxById);