Merge branch 'box_pieces'

This commit is contained in:
Nemo D'ACREMONT 2024-11-27 16:01:11 +01:00
commit db205b01e5
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
5 changed files with 162 additions and 120 deletions

View File

@ -1,126 +1,13 @@
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, eitherFormatError } from '../utils/utils';
import { getEntity, getEntities, registerEntity, toColour, getColour, getColours, createColour, toPattern, getPattern, getPatterns, createPattern, toShape, getShape, getShapes, createShape, getPiece, getPieces, createPiece } from '../services/pieces.service';
const getDefaultColour = () => ({ id_colour: -1, name: 'unknown' });
const getDefaultPattern = () => ({ id_pattern: -1, name: 'unknown' });
const getDefaultShape = () => ({ id_shape: -1, name: 'unknown' });
const getDefaultPiece = () => ({ id_piece: -1, colour: getDefaultColour(), pattern: getDefaultPattern(), shape: getDefaultShape() });
async function getEntity<T>(table: string, column: string, value: string | number, f: (o: Object) => T): Promise<Either<T, string>> {
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<T, string>('Not found in database.');
}
const entity: T = f(res.rows[0]);
await client.end();
return eitherLeft<T, string>(entity);
}
async function getEntities<T>(table: string, f: (o: Object) => T): Promise<Array<T>> {
const client = new_client();
await client.connect();
const res = await client.query(`SELECT * FROM ${client.escapeIdentifier(table)};`);
const arr: Array<T> = 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<T>(table: string, colName: string, name: string, f: (o: Object) => T): Promise<T> {
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 entity: T = f(res.rows[0]);
await client.end();
return entity;
}
const toColour: (o: Object) => Colour = o => {
return { id_colour: o['id_couleur'], name: o['nom_couleur'] };
};
async function getColour(idOrName: number | string): Promise<Either<Colour, string>> {
return await getEntity<Colour>('couleurs', typeof idOrName !== 'number' ? 'nom_couleur' : 'id_couleur', idOrName, toColour);
}
async function getColours(): Promise<Array<Colour>> {
return await getEntities<Colour>('couleurs', toColour);
}
async function createColour(name: string): Promise<Colour> {
return await registerEntity<Colour>('couleurs', 'nom_couleur', name, toColour);
}
const toPattern: (o: Object) => Pattern = o => {
return { id_pattern: o['id_motif'], name: o['nom_motif'] };
};
async function getPattern(idOrName: number | string): Promise<Either<Pattern, string>> {
return await getEntity<Pattern>('motifs', typeof idOrName !== 'number' ? 'nom_motif' : 'id_motif', idOrName, toPattern);
}
async function getPatterns(): Promise<Array<Pattern>> {
return await getEntities<Pattern>('motifs', toPattern);
}
async function createPattern(name: string): Promise<Pattern> {
return await registerEntity<Pattern>('motifs', 'nom_motif', name, toPattern);
}
const toShape: (o: Object) => Shape = o => {
return { id_shape: o['id_forme'], name: o['nom_forme'] };
};
async function getShape(idOrName: number | string): Promise<Either<Shape, string>> {
return await getEntity<Shape>('formes', typeof idOrName !== 'number' ? 'nom_forme' : 'id_forme', idOrName, toShape);
}
async function getShapes(): Promise<Array<Shape>> {
return await getEntities<Shape>('formes', toShape);
}
async function createShape(name: string): Promise<Shape> {
return await registerEntity<Shape>('formes', 'nom_forme', name, toShape);
}
async function getPiece(id: number): Promise<Either<Piece, string>> {
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<Piece, string>(result);
}
async function getPieces(): Promise<Array<Piece>> {
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<Piece> = 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<Piece> {
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.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;
}
async function retrieveEntity<T>(f: (id: number) => Promise<Either<T, string>>, req: Request, res: Response) {
const entity: Either<T, string> = await f(parseInt(req.params.id));
if (entity.hasRight) {

View File

@ -1,6 +1,8 @@
import { new_client } from '../db/db_client';
import { Box } from '../types/box';
import { Either, eitherLeft, eitherRight } from '../utils/utils';
import { Piece } from '../types/piece';
import { getPiece } from './pieces.service';
import { Either, eitherLeft, eitherRight, Pair, createPair } from '../utils/utils';
type DBBox = {
id_boite: number;
@ -8,11 +10,27 @@ type DBBox = {
date_boite: string;
};
function db2box(data: DBBox): Box {
async function getPiecesFromBox(id_box: number): Promise<Array<Pair<Piece, number>>> {
const client = new_client();
await client.connect();
const res = await client.query(`SELECT * FROM contenir WHERE id_boite = $1;`, [ id_box ]);
const arr: Array<Pair<Piece, number>> = new Array();
for (let i = 0; i < res.rows.length; ++i) {
const piece: Either<Piece, string> = await getPiece(res.rows[i]['id_piece']);
if (piece.hasRight)
continue;
arr.push(createPair(piece.left, res.rows[i]['quantite_contenir'] as number));
}
await client.end();
return arr;
}
async function db2box(data: DBBox): Promise<Box> {
const box: Box = {
id: data.id_boite,
title: data.titre_boite,
date: new Date(data.date_boite),
pieces: await getPiecesFromBox(data.id_boite)
};
return box;
@ -31,7 +49,7 @@ const getBox = async (idOrTitle: number | string): Promise<Either<Box, string>>
await client.end();
return eitherRight<Box, string>("Does not exist.");
}
const box = db2box(res.rows[0]);
const box = await db2box(res.rows[0]);
await client.end();
return eitherLeft<Box, string>(box);
@ -40,9 +58,11 @@ const getBox = async (idOrTitle: number | string): Promise<Either<Box, string>>
const getAllBoxes = async () => {
const client = new_client();
await client.connect();
const res = await client.query("SELECT * FROM boites");
await client.end();
return res.rows.map(db2box);
return await Promise.all(res.rows.map(async (el) => { return await db2box(el) }));
}
const getAllBoxesFromDate = async (date: Date) => {
@ -55,8 +75,9 @@ const getAllBoxesFromDate = async (date: Date) => {
const dateString = `${YYYY}-${MM}-${DD}`;
const res = await client.query("SELECT * FROM boites WHERE date_boite > $1", [dateString]);
await client.end();
return res.rows.map(db2box);
return await Promise.all(res.rows.map(async (el) => { return await db2box(el) }));
}
const boxService = {

View File

@ -0,0 +1,121 @@
import { new_client } from '../db/db_client';
import { Colour, Pattern, Shape, Piece, ComplexPiece } from '../types/piece';
import { Either, eitherLeft, eitherRight, eitherFormatError } from '../utils/utils';
async function getEntity<T>(table: string, column: string, value: string | number, f: (o: Object) => T): Promise<Either<T, string>> {
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<T, string>('Not found in database.');
}
const entity: T = f(res.rows[0]);
await client.end();
return eitherLeft<T, string>(entity);
}
async function getEntities<T>(table: string, f: (o: Object) => T): Promise<Array<T>> {
const client = new_client();
await client.connect();
const res = await client.query(`SELECT * FROM ${client.escapeIdentifier(table)};`);
const arr: Array<T> = 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<T>(table: string, colName: string, name: string, f: (o: Object) => T): Promise<T> {
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 entity: T = f(res.rows[0]);
await client.end();
return entity;
}
const toColour: (o: Object) => Colour = o => {
return { id_colour: o['id_couleur'], name: o['nom_couleur'] };
};
async function getColour(idOrName: number | string): Promise<Either<Colour, string>> {
return await getEntity<Colour>('couleurs', typeof idOrName !== 'number' ? 'nom_couleur' : 'id_couleur', idOrName, toColour);
}
async function getColours(): Promise<Array<Colour>> {
return await getEntities<Colour>('couleurs', toColour);
}
async function createColour(name: string): Promise<Colour> {
return await registerEntity<Colour>('couleurs', 'nom_couleur', name, toColour);
}
const toPattern: (o: Object) => Pattern = o => {
return { id_pattern: o['id_motif'], name: o['nom_motif'] };
};
async function getPattern(idOrName: number | string): Promise<Either<Pattern, string>> {
return await getEntity<Pattern>('motifs', typeof idOrName !== 'number' ? 'nom_motif' : 'id_motif', idOrName, toPattern);
}
async function getPatterns(): Promise<Array<Pattern>> {
return await getEntities<Pattern>('motifs', toPattern);
}
async function createPattern(name: string): Promise<Pattern> {
return await registerEntity<Pattern>('motifs', 'nom_motif', name, toPattern);
}
const toShape: (o: Object) => Shape = o => {
return { id_shape: o['id_forme'], name: o['nom_forme'] };
};
async function getShape(idOrName: number | string): Promise<Either<Shape, string>> {
return await getEntity<Shape>('formes', typeof idOrName !== 'number' ? 'nom_forme' : 'id_forme', idOrName, toShape);
}
async function getShapes(): Promise<Array<Shape>> {
return await getEntities<Shape>('formes', toShape);
}
async function createShape(name: string): Promise<Shape> {
return await registerEntity<Shape>('formes', 'nom_forme', name, toShape);
}
async function getPiece(id: number): Promise<Either<Piece, string>> {
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]);
if (!res.rows[0]) {
return eitherRight<Piece, string>("Nothing found");
}
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<Piece, string>(result);
}
async function getPieces(): Promise<Array<Piece>> {
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<Piece> = 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<Piece> {
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.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;
}
export { getEntity, getEntities, registerEntity, toColour, getColour, getColours, createColour, toPattern, getPattern, getPatterns, createPattern, toShape, getShape, getShapes, createShape, getPiece, getPieces, createPiece };

View File

@ -1,7 +1,11 @@
import { Pair } from '../utils/utils';
import { Piece } from './piece';
type Box = {
id: number;
title: string;
date: Date;
pieces: Array<Pair<Piece, number>>
};
export { Box };

View File

@ -4,6 +4,11 @@ type Either<T, U> = {
right: U
};
type Pair<T, U> = {
first: T,
second: U
};
type Error = {
id: number,
message: string
@ -26,4 +31,8 @@ function eitherFormatError<T, Error>(either: Either<T, string>): Result<T> {
return either.left;
}
export { Either, eitherLeft, eitherRight, eitherFormatError };
function createPair<T, U>(first: T, second: U): Pair<T, U> {
return { first: first, second: second };
}
export { Either, eitherLeft, eitherRight, eitherFormatError, Pair, createPair };