fix: delete piece from box

This commit is contained in:
Martin Eyben 2024-12-04 12:41:43 +00:00
parent 1577817111
commit 101551b4a3
2 changed files with 26 additions and 0 deletions

View File

@ -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<Box, string> = 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<Piece, string> = 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;

View File

@ -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);