fix: delete piece from box
This commit is contained in:
parent
1577817111
commit
101551b4a3
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user