From e96ff1621cd5933e7c1a077630d9537fa1b06ead Mon Sep 17 00:00:00 2001 From: Alessandre Laguierce Date: Tue, 3 Dec 2024 13:46:04 +0100 Subject: [PATCH] feat: add models per box --- back-end/src/services/box.service.ts | 16 ++++++++++++++++ back-end/src/templates/box.ejs | 8 ++++++++ back-end/src/types/box.ts | 4 +++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/back-end/src/services/box.service.ts b/back-end/src/services/box.service.ts index 8803ee6..b6d1a6d 100644 --- a/back-end/src/services/box.service.ts +++ b/back-end/src/services/box.service.ts @@ -1,8 +1,10 @@ import { new_client } from '../db/db_client'; import { Box } from '../types/box'; +import { Model } from '../types/model'; import { Piece } from '../types/piece'; import { getPiece } from './pieces.service'; import { Either, eitherLeft, eitherRight, Pair, createPair } from '../utils/utils'; +import modelService from './model.service'; type DBBox = { id_boite: number; @@ -33,11 +35,25 @@ async function removePieceFromBox(id_box: number, id_piece: number) { const res = await client.query(`DELETE FROM contenir WHERE id_boite = $1 AND id_piece = $2;`, [ id_box, id_piece ]); } +async function getModelsFromBox(id_box: number): Promise> { + const client = new_client(); + const res = await client.query(`SELECT * FROM modeles_faisables_boite($1);`, [ id_box ]); + const arr: Array = new Array(); + for (let i = 0; i < res.rows.length; ++i) { + const model: Either = await modelService.getModel(res.rows[i]['id_model']); + if (model.hasRight) + continue; + arr.push(model.left); + } + return arr; +} + async function db2box(data: DBBox): Promise { const box: Box = { id: data.id_boite, title: data.titre_boite, date: new Date(data.date_boite), + models: await getModelsFromBox(data.id_boite), pieces: await getPiecesFromBox(data.id_boite) }; diff --git a/back-end/src/templates/box.ejs b/back-end/src/templates/box.ejs index d5ae056..033346a 100644 --- a/back-end/src/templates/box.ejs +++ b/back-end/src/templates/box.ejs @@ -25,6 +25,14 @@ function deletePiece(id) {
 		id          : <%= box.id %>
 		date        : <%= box.date %>
+    modèles     :
+    <% box.models.forEach(function(m) { %>
+			
  • + + <%=m.id_name%> + +
  • + <% }); %> pièces : <% box.pieces.forEach(function(pair) { %>
  • diff --git a/back-end/src/types/box.ts b/back-end/src/types/box.ts index 75b5ca9..f332bc8 100644 --- a/back-end/src/types/box.ts +++ b/back-end/src/types/box.ts @@ -1,3 +1,4 @@ +import { Model } from './model'; import { Pair } from '../utils/utils'; import { Piece } from './piece'; @@ -5,7 +6,8 @@ type Box = { id: number; title: string; date: Date; - pieces: Array> + models: Array; + pieces: Array>; }; export { Box };