feat: add pieces for boxes

This commit is contained in:
Alessandre Laguierce 2024-12-02 21:26:06 +01:00
parent c349c05acf
commit 1ee84c3f61
4 changed files with 89 additions and 4 deletions

View File

@ -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<Box, string> = await boxService.getBox(req.params.title);
if (boxEither.hasRight) {
res.sendStatus(404);
return;
}
const piece: Either<Piece, string> = 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<Box, string> = await boxService.getBox(req.params.title);
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);
@ -46,6 +98,8 @@ const boxById: RequestHandler<{ id: string; }> = async (req, res) => {
};
const boxController = {
boxAddPieces,
boxRemovePiece,
boxByTitle,
allBoxes,
allBoxesFromDate,

View File

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

View File

@ -25,6 +25,20 @@ async function getPiecesFromBox(id_box: number): Promise<Array<Pair<Piece, numbe
return arr;
}
async function addPiecesToBox(id_box: number, id_piece: number, qty: number) {
const client = new_client();
await client.connect();
const res = await client.query(`INSERT INTO contenir (id_boite, id_piece, quantite_contenir) VALUES ($1, $2, $3);`, [ id_box, id_piece, qty ]);
await client.end();
}
async function removePieceFromBox(id_box: number, id_piece: number) {
const client = new_client();
await client.connect();
const res = await client.query(`DELETE FROM contenir WHERE id_boite = $1 AND id_piece = $2;`, [ id_box, id_piece ]);
await client.end();
}
async function db2box(data: DBBox): Promise<Box> {
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;

View File

@ -7,8 +7,14 @@
<title>
<%= box.title %>
</title>
<%- include('partials/links.ejs') %>
<script>
function deletePiece(id) {
fetch(window.location + "?id_piece=" + id, {
method: 'DELETE'
}).then(r => window.location.reload());
}
</script>
</head>
<body>
@ -23,10 +29,18 @@
<% box.pieces.forEach(function(pair) { %>
<li>
<a href="/pieces/<%=pair.first.id_piece%>">
<%=pair.first.id_piece%> @ <%=pair.second%>
n°<%=pair.first.id_piece%> en <%=pair.second%> exemplaires
</a>
<p><a href="javascript: deletePiece('<%=pair.first.id_piece%>')">Suppr</a></p>
</li>
<% }); %>
<% }); %>
<form>
<label for="numero_piece">Numero Piece</label>
<input type="number" id="numero_piece" name="numero_piece" />
<label for="quantite">Quantité</label>
<input type="number" id="quantite" name="quantite" />
<input type="submit" formmethod="post" value="Ajouter"/>
</form>
</pre>
</body>