From 1ee84c3f615da4dc3ce32c7708a4b297eebcc181 Mon Sep 17 00:00:00 2001 From: Alessandre Laguierce Date: Mon, 2 Dec 2024 21:26:06 +0100 Subject: [PATCH] feat: add pieces for boxes --- back-end/src/controllers/box.controller.ts | 54 ++++++++++++++++++++++ back-end/src/routes/box.route.ts | 3 +- back-end/src/services/box.service.ts | 16 +++++++ back-end/src/templates/box.ejs | 20 ++++++-- 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/back-end/src/controllers/box.controller.ts b/back-end/src/controllers/box.controller.ts index aaa2b91..af4cc7c 100644 --- a/back-end/src/controllers/box.controller.ts +++ b/back-end/src/controllers/box.controller.ts @@ -1,5 +1,9 @@ import { RequestHandler } from "express"; +import { getPiece } from "../services/pieces.service"; import boxService from "../services/box.service"; +import { Either, eitherLeft, eitherRight, eitherFormatError, Pair, createPair } from '../utils/utils'; +import { Box } from '../types/box' +import { Piece } from '../types/piece' const boxByTitle: RequestHandler<{ title: string; }> = async (req, res) => { const boxEither = await boxService.getBox(req.params.title); @@ -31,6 +35,54 @@ const allBoxesFromDate: RequestHandler<{ date: string; }> = async (req, res) => res.render('boxes.ejs', { boxes }); }; +const boxAddPieces: RequestHandler<{ title: string; }> = async (req, res) => { + const boxEither: Either = await boxService.getBox(req.params.title); + + 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/${req.params.title}`); +}; + +const boxRemovePiece: RequestHandler<{ title: string; }> = async (req, res) => { + const boxEither: Either = await boxService.getBox(req.params.title); + + 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); @@ -46,6 +98,8 @@ const boxById: RequestHandler<{ id: string; }> = async (req, res) => { }; const boxController = { + boxAddPieces, + boxRemovePiece, boxByTitle, allBoxes, allBoxesFromDate, diff --git a/back-end/src/routes/box.route.ts b/back-end/src/routes/box.route.ts index 2df615e..b7cddfd 100644 --- a/back-end/src/routes/box.route.ts +++ b/back-end/src/routes/box.route.ts @@ -8,7 +8,8 @@ boxRouter.get("/", boxController.allBoxes); boxRouter.get("/from/:date", boxController.allBoxesFromDate); boxRouter.get("/:title", boxController.boxByTitle); - +boxRouter.post("/:title", boxController.boxAddPieces); +boxRouter.delete("/:title", boxController.boxRemovePiece); boxRouter.get("/byid/:id", boxController.boxById); export default boxRouter; diff --git a/back-end/src/services/box.service.ts b/back-end/src/services/box.service.ts index ce03d1b..caab528 100644 --- a/back-end/src/services/box.service.ts +++ b/back-end/src/services/box.service.ts @@ -25,6 +25,20 @@ async function getPiecesFromBox(id_box: number): Promise { const box: Box = { id: data.id_boite, @@ -81,9 +95,11 @@ const getAllBoxesFromDate = async (date: Date) => { } const boxService = { + addPiecesToBox, getBox, getAllBoxes, getAllBoxesFromDate, + removePieceFromBox }; export default boxService; diff --git a/back-end/src/templates/box.ejs b/back-end/src/templates/box.ejs index 5d13f83..d5ae056 100644 --- a/back-end/src/templates/box.ejs +++ b/back-end/src/templates/box.ejs @@ -7,8 +7,14 @@ <%= box.title %> - <%- include('partials/links.ejs') %> + @@ -23,10 +29,18 @@ <% box.pieces.forEach(function(pair) { %>
  • - <%=pair.first.id_piece%> @ <%=pair.second%> + n°<%=pair.first.id_piece%> en <%=pair.second%> exemplaires +

    Suppr

  • - <% }); %> + <% }); %> +
    + + + + + +