From e3301bd831542243e3226122b4cb9e9d0dd4c1b4 Mon Sep 17 00:00:00 2001 From: Alessandre Laguierce Date: Wed, 27 Nov 2024 14:45:37 +0100 Subject: [PATCH] feat: add piece --- back-end/src/controllers/piece.controller.ts | 51 ++++++++++++-------- back-end/src/routes/piece.route.ts | 4 +- back-end/src/types/piece.ts | 24 ++++----- back-end/src/utils/utils.ts | 2 +- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/back-end/src/controllers/piece.controller.ts b/back-end/src/controllers/piece.controller.ts index 81c7411..4223f7c 100644 --- a/back-end/src/controllers/piece.controller.ts +++ b/back-end/src/controllers/piece.controller.ts @@ -37,8 +37,9 @@ async function registerEntity(table: string, colName: string, name: string, f const client = new_client(); await client.connect(); const res = await client.query(`INSERT INTO ${client.escapeIdentifier(table)} (${client.escapeIdentifier(colName)}) VALUES ($1) RETURNING *;`, [name]); - const T entity = f(res.rows[0]); + const entity: T = f(res.rows[0]); await client.end(); + return entity; } const toColour: (o: Object) => Colour = o => { @@ -89,19 +90,33 @@ async function createShape(name: string): Promise { return await registerEntity('formes', 'nom_forme', name, toShape); } -async function getPiece(id: number): Promise { - return getDefaultPiece(); +async function getPiece(id: number): Promise> { + const client = new_client(); + await client.connect(); + const res = await client.query(`SELECT * FROM pieces NATURAL JOIN colorer NATURAL JOIN etre_forme NATURAL JOIN avoir_motif WHERE id_piece = $1`, [id]); + const result: Piece = { id_piece: res.rows[0]['id_piece'], colour: toColour(res.rows[0]), pattern: toPattern(res.rows[0]), shape: toShape(res.rows[0]) }; + return eitherLeft(result); } async function getPieces(): Promise> { - return new Array(getDefaultPiece()); + const client = new_client(); + await client.connect(); + const res = await client.query(`SELECT * FROM pieces NATURAL JOIN colorer NATURAL JOIN etre_forme NATURAL JOIN avoir_motif;`); + const arr: Array = new Array(); + for (let i = 0; i < res.rows.length; ++i) { + arr.push({ id_piece: res.rows[i]['id_piece'], colour: toColour(res.rows[i]), pattern: toPattern(res.rows[i]), shape: toShape(res.rows[i]) }); + } + return arr; } async function createPiece(colour: Colour, pattern: Pattern, shape: Shape): Promise { const client = new_client(); await client.connect(); const res = await client.query(`INSERT INTO pieces () VALUES () RETURNING *;`); - const result: Piece = { id_piece: res.rows[0].id_piece, colour: colour, pattern: pattern, shape: shape }; + const result: Piece = { id_piece: res.rows[0]['id_piece'], colour: colour, pattern: pattern, shape: shape }; + await client.query("INSERT INTO colorer (id_piece, id_couleur) VALUES ($1, $2);", [result.id_piece, colour.id_colour]); + await client.query("INSERT INTO etre_forme (id_forme, id_piece) VALUES ($1, $2);", [shape.id_shape, result.id_piece]); + await client.query("INSERT INTO avoir_motif (id_piece, id_motif) VALUES ($1, $2);", [result.id_piece, pattern.id_pattern]) await client.end(); return result; } @@ -116,16 +131,12 @@ async function retrieveEntity(f: (id: number) => Promise>, } async function createEntity(f: (o: Object) => T, table: string, colName: string, req: Request, res: Response) { - if (!req | !req.body | !req.body.name) { + if (!req || !req.body || !req.body.name) { res.status(400).send(eitherRight('No field `name` in body.')); return; } - const entity: Either = await registerEntity(table, colName, req.body.name, f); - if (entity.hasRight) { - res.status(500).send(entity.right); - return; - } - res.send(entity.left); + const entity: T = await registerEntity(table, colName, req.body.name, f); + res.send(entity); } const retrieveColour = async (req: Request, res: Response) => { @@ -138,7 +149,7 @@ const retrieveColours = async (req: Request, res: Response) => { }; const registerColour = (req: Request, res: Response) => { - createEntity("couleurs", getColour, req, res); + createEntity(toColour, "couleurs", "nom_couleur", req, res); }; const retrievePattern = (req: Request, res: Response) => { @@ -150,7 +161,7 @@ const retrievePatterns = (req: Request, res: Response) => { }; const registerPattern = (req: Request, res: Response) => { - createEntity("motifs", getPattern, req, res); + createEntity(toPattern, "motifs", "nom_motif", req, res); }; const retrieveShape = (req: Request, res: Response) => { @@ -162,7 +173,7 @@ const retrieveShapes = (req: Request, res: Response) => { }; const registerShape = (req: Request, res: Response) => { - createEntity("formes", getShape, req, res); + createEntity(toShape, "formes", "nom_forme", req, res); }; const retrievePiece = (req: Request, res: Response) => { @@ -173,18 +184,18 @@ const retrievePieces = (req: Request, res: Response) => { res.send(getPieces()); }; -const registerPiece = (req: Request, res: Response) => { - const colour: Either = getColour(req.body.colour); +const registerPiece = async (req: Request, res: Response) => { + const colour: Either = await getColour(req.body.colour); if (colour.hasRight) { res.status(500).send(eitherFormatError(colour)); return; } - const pattern: Either = getPattern(req.body.pattern); + const pattern: Either = await getPattern(req.body.pattern); if (pattern.hasRight) { res.status(500).send(eitherFormatError(pattern)); return; } - const shape: Either = getShape(req.body.shape); + const shape: Either = await getShape(req.body.shape); if (shape.hasRight) { res.status(500).send(eitherFormatError(shape)); return; @@ -192,4 +203,4 @@ const registerPiece = (req: Request, res: Response) => { res.send(createPiece(colour.left, pattern.left, shape.left)); }; -export { retrieveColour, retrieveColours, registerColour, retrievePattern, retrievePatterns, registerPattern, retrieveShape, retrieveShapes, registerShape, retrievePieces, registerPiece }; +export { retrieveColour, retrieveColours, registerColour, retrievePattern, retrievePatterns, registerPattern, retrieveShape, retrieveShapes, registerShape, retrievePiece, retrievePieces, registerPiece }; diff --git a/back-end/src/routes/piece.route.ts b/back-end/src/routes/piece.route.ts index 1cd550f..9679516 100644 --- a/back-end/src/routes/piece.route.ts +++ b/back-end/src/routes/piece.route.ts @@ -1,5 +1,5 @@ import express from 'express'; -import { retrieveColour, retrieveColours, registerColour, retrievePatterns, registerPattern, retrieveShapes, registerShape, retrievePieces, registerPiece } from '../controllers/piece.controller'; +import { retrieveColour, retrieveColours, registerColour, retrievePattern, retrievePatterns, registerPattern, retrieveShape, retrieveShapes, registerShape, retrievePiece, retrievePieces, registerPiece } from '../controllers/piece.controller'; const router = express.Router(); @@ -7,7 +7,7 @@ router.get("/colour", retrieveColours); router.get("/colour/:id", retrieveColour); router.post("/colour/register", registerColour); router.get("/pattern", retrievePatterns); -router.get("/colour/:id", retrievePattern); +router.get("/pattern/:id", retrievePattern); router.post("/pattern/register", registerPattern); router.get("/shape", retrieveShapes); router.get("/shape/:id", retrieveShape); diff --git a/back-end/src/types/piece.ts b/back-end/src/types/piece.ts index dc79ad5..aea0880 100644 --- a/back-end/src/types/piece.ts +++ b/back-end/src/types/piece.ts @@ -1,28 +1,28 @@ type Colour = { - id_colour: number, - name: string + id_colour: number, + name: string }; type Pattern = { - id_pattern: number, - name: string + id_pattern: number, + name: string }; type Shape = { - id_shape: number, - name: string + id_shape: number, + name: string }; type Piece = { - id_piece: number, - colour: Colour, - pattern: Pattern, - shape: Shape + id_piece: number, + colour: Colour, + pattern: Pattern, + shape: Shape }; type ComplexPiece = { - id_complex: number, - subpieces: Array + id_complex: number, + subpieces: Array }; export { Colour, Pattern, Shape, Piece, ComplexPiece }; diff --git a/back-end/src/utils/utils.ts b/back-end/src/utils/utils.ts index 2a66b0b..067a770 100644 --- a/back-end/src/utils/utils.ts +++ b/back-end/src/utils/utils.ts @@ -19,7 +19,7 @@ function eitherRight(right: U): Either { return { hasRight: true, left: undefined, right: right }; } -function eitherFormatError(either: Either): Result { +function eitherFormatError(either: Either): Result { if (either.hasRight) { return { id: -1, message: either.right }; }