From 101551b4a3126216aa728dfca1b8a14cb3a78a3b Mon Sep 17 00:00:00 2001 From: Martin Eyben Date: Wed, 4 Dec 2024 12:41:43 +0000 Subject: [PATCH] fix: delete piece from box --- back-end/src/controllers/box.controller.ts | 25 ++++++++++++++++++++++ back-end/src/routes/box.route.ts | 1 + 2 files changed, 26 insertions(+) diff --git a/back-end/src/controllers/box.controller.ts b/back-end/src/controllers/box.controller.ts index fb770f8..ab75e5a 100644 --- a/back-end/src/controllers/box.controller.ts +++ b/back-end/src/controllers/box.controller.ts @@ -86,6 +86,30 @@ const boxRemovePiece: RequestHandler<{ title: string; }> = async (req, res) => { res.status(200).send(); }; +const boxRemovePieceById: RequestHandler<{ id_box: string; }> = async (req, res) => { + const boxEither: Either = await boxService.getBox(parseInt(req.params.id_box)); + + if (boxEither.hasRight) { + res.sendStatus(404); + return; + } + + const id: any = req.query.id_piece; + const piece: Either = await getPiece(id as number); + 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.removePieceFromBox(boxEither.left.id, piece.left.id_piece); + + res.status(200).send(); +}; + const boxById: RequestHandler<{ id: string; }> = async (req, res) => { const id = parseInt(req.params.id); const boxEither = await boxService.getBox(id); @@ -132,6 +156,7 @@ const boxController = { allBoxesFromDate, boxById, registerBox, + boxRemovePieceById }; export default boxController; diff --git a/back-end/src/routes/box.route.ts b/back-end/src/routes/box.route.ts index 9f9edb2..d99633e 100644 --- a/back-end/src/routes/box.route.ts +++ b/back-end/src/routes/box.route.ts @@ -13,6 +13,7 @@ boxRouter.get("/:title", boxController.boxByTitle); boxRouter.post("/:title", boxController.boxAddPieces); +boxRouter.delete("/byid/:id_box", boxController.boxRemovePieceById); boxRouter.delete("/:title", boxController.boxRemovePiece); boxRouter.get("/byid/:id", boxController.boxById);