diff --git a/back-end/src/controllers/piece.controller.ts b/back-end/src/controllers/piece.controller.ts index 65294c6..81c7411 100644 --- a/back-end/src/controllers/piece.controller.ts +++ b/back-end/src/controllers/piece.controller.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; import { new_client } from '../db/db_client'; import { Colour, Pattern, Shape, Piece, ComplexPiece } from '../types/piece'; -import { Either, eitherLeft, eitherRight } from '../utils/utils'; +import { Either, eitherLeft, eitherRight, eitherFormatError } from '../utils/utils'; const getDefaultColour = () => ({ id_colour: -1, name: 'unknown' }); const getDefaultPattern = () => ({ id_pattern: -1, name: 'unknown' }); @@ -9,146 +9,187 @@ const getDefaultShape = () => ({ id_shape: -1, name: 'unknown' }); const getDefaultPiece = () => ({ id_piece: -1, colour: getDefaultColour(), pattern: getDefaultPattern(), shape: getDefaultShape() }); async function getEntity(table: string, column: string, value: string | number, f: (o: Object) => T): Promise> { - const client = new_client(); - await client.connect(); - const res = await client.query(`SELECT * FROM ${client.escapeIdentifier(table)} WHERE $1=$2;`, [column, value]); - if (res.rows.length === 0) { - await client.end(); - return eitherRight('Not found in database.'); - } - const entity: T = f(res.rows[0]); + const client = new_client(); + await client.connect(); + const res = await client.query(`SELECT * FROM ${client.escapeIdentifier(table)} WHERE $1=$2;`, [column, value]); + if (res.rows.length === 0) { await client.end(); - return eitherLeft(entity); + return eitherRight('Not found in database.'); + } + const entity: T = f(res.rows[0]); + await client.end(); + return eitherLeft(entity); } async function getEntities(table: string, f: (o: Object) => T): Promise> { - const client = new_client(); - await client.connect(); - const res = await client.query(`SELECT * FROM ${client.escapeIdentifier(table)};`); - const arr: Array = new Array(); - for (let i = 0; i < res.rows.length; ++i) { - arr.push(f(res.rows[i])); - } - await client.end(); - return arr; + const client = new_client(); + await client.connect(); + const res = await client.query(`SELECT * FROM ${client.escapeIdentifier(table)};`); + const arr: Array = new Array(); + for (let i = 0; i < res.rows.length; ++i) { + arr.push(f(res.rows[i])); + } + await client.end(); + return arr; +} + +async function registerEntity(table: string, colName: string, name: string, f: (o: Object) => T): Promise { + 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]); + await client.end(); } const toColour: (o: Object) => Colour = o => { - return { id_colour: o['id_couleur'], name: o['nom_couleur'] }; + return { id_colour: o['id_couleur'], name: o['nom_couleur'] }; }; async function getColour(idOrName: number | string): Promise> { - return await getEntity('couleurs', typeof idOrName !== 'number' ? 'nom_couleur' : 'id_couleur', idOrName, toColour); + return await getEntity('couleurs', typeof idOrName !== 'number' ? 'nom_couleur' : 'id_couleur', idOrName, toColour); } async function getColours(): Promise> { - return await getEntities('couleurs', toColour); + return await getEntities('couleurs', toColour); } async function createColour(name: string): Promise { - return getDefaultColour(); + return await registerEntity('couleurs', 'nom_couleur', name, toColour); } const toPattern: (o: Object) => Pattern = o => { - return { id_pattern: o['id_motif'], name: o['nom_motif'] }; + return { id_pattern: o['id_motif'], name: o['nom_motif'] }; }; async function getPattern(idOrName: number | string): Promise> { - return await getEntity('motifs', typeof idOrName !== 'number' ? 'nom_motif' : 'id_motif', idOrName, toPattern); + return await getEntity('motifs', typeof idOrName !== 'number' ? 'nom_motif' : 'id_motif', idOrName, toPattern); } async function getPatterns(): Promise> { - return await getEntities('motifs', toPattern); + return await getEntities('motifs', toPattern); } async function createPattern(name: string): Promise { - return getDefaultPattern(); + return await registerEntity('motifs', 'nom_motif', name, toPattern); } const toShape: (o: Object) => Shape = o => { - return { id_shape: o['id_forme'], name: o['nom_forme'] }; + return { id_shape: o['id_forme'], name: o['nom_forme'] }; }; async function getShape(idOrName: number | string): Promise> { - return await getEntity('formes', typeof idOrName !== 'number' ? 'nom_forme' : 'id_forme', idOrName, toShape); + return await getEntity('formes', typeof idOrName !== 'number' ? 'nom_forme' : 'id_forme', idOrName, toShape); } async function getShapes(): Promise> { - return await getEntities('formes', toShape); + return await getEntities('formes', toShape); } async function createShape(name: string): Promise { - return getDefaultShape(); + return await registerEntity('formes', 'nom_forme', name, toShape); } async function getPiece(id: number): Promise { - return getDefaultPiece(); + return getDefaultPiece(); } async function getPieces(): Promise> { - return new Array(getDefaultPiece()); + return new Array(getDefaultPiece()); } -async function createPiece(colour: number, pattern: number, shape: number): Promise { - return getDefaultPiece(); +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 }; + await client.end(); + return result; } async function retrieveEntity(f: (id: number) => Promise>, req: Request, res: Response) { - const entity: Either = await f(parseInt(req.params.id)); - if (entity.hasRight) { - res.status(500).send(entity.right); - return; - } - res.send(entity.left); + const entity: Either = await f(parseInt(req.params.id)); + if (entity.hasRight) { + res.status(500).send(entity.right); + return; + } + res.send(entity.left); +} + +async function createEntity(f: (o: Object) => T, table: string, colName: string, req: Request, res: Response) { + 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 retrieveColour = async (req: Request, res: Response) => { - retrieveEntity(getColour, req, res); + retrieveEntity(getColour, req, res); }; const retrieveColours = async (req: Request, res: Response) => { - const colours: Array = await getColours(); - res.send(colours); + const colours: Array = await getColours(); + res.send(colours); }; const registerColour = (req: Request, res: Response) => { - + createEntity("couleurs", getColour, req, res); }; const retrievePattern = (req: Request, res: Response) => { - retrieveEntity(getPattern, req, res); + retrieveEntity(getPattern, req, res); }; const retrievePatterns = (req: Request, res: Response) => { - res.send(getPatterns()); + res.send(getPatterns()); }; const registerPattern = (req: Request, res: Response) => { - + createEntity("motifs", getPattern, req, res); }; const retrieveShape = (req: Request, res: Response) => { - retrieveEntity(getShape, req, res); + retrieveEntity(getShape, req, res); }; const retrieveShapes = (req: Request, res: Response) => { - res.send(getShapes()); + res.send(getShapes()); }; const registerShape = (req: Request, res: Response) => { - + createEntity("formes", getShape, req, res); }; const retrievePiece = (req: Request, res: Response) => { - retrieveEntity(getPiece, req, res); + retrieveEntity(getPiece, req, res); }; const retrievePieces = (req: Request, res: Response) => { - res.send(getPieces()); + res.send(getPieces()); }; const registerPiece = (req: Request, res: Response) => { - + const colour: Either = getColour(req.body.colour); + if (colour.hasRight) { + res.status(500).send(eitherFormatError(colour)); + return; + } + const pattern: Either = getPattern(req.body.pattern); + if (pattern.hasRight) { + res.status(500).send(eitherFormatError(pattern)); + return; + } + const shape: Either = getShape(req.body.shape); + if (shape.hasRight) { + res.status(500).send(eitherFormatError(shape)); + return; + } + res.send(createPiece(colour.left, pattern.left, shape.left)); }; export { retrieveColour, retrieveColours, registerColour, retrievePattern, retrievePatterns, registerPattern, retrieveShape, retrieveShapes, registerShape, retrievePieces, registerPiece }; diff --git a/back-end/src/utils/utils.ts b/back-end/src/utils/utils.ts index abbed21..3fa852e 100644 --- a/back-end/src/utils/utils.ts +++ b/back-end/src/utils/utils.ts @@ -4,6 +4,13 @@ type Either = { right: U }; +type Error = { + id: number, + message: string +}; + +type Result = Error | T; + function eitherLeft(left: T): Either { return { hasRight: false, left: left, right: undefined }; } @@ -12,4 +19,11 @@ function eitherRight(right: U): Either { return { hasRight: true, left: undefined, right: right }; } -export { Either, eitherLeft, eitherRight }; +function eitherFormatError(either: Either): Result { + if (either.hasRight) { + return { id: -1, message: either.right }; + } + return either.left; +} + +export { Either, eitherLeft, eitherRight, eitherFormatError };