130 lines
5.6 KiB
TypeScript
130 lines
5.6 KiB
TypeScript
import { new_client } from '../db/db_client';
|
|
import { Colour, Pattern, Shape, Piece, ComplexPiece } from '../types/piece';
|
|
import { Either, eitherLeft, eitherRight, eitherFormatError, createPair } from '../utils/utils';
|
|
import { Pair } 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();
|
|
const res = await client.query(`SELECT * FROM $1 WHERE $2=$3;`, [table, column, value]);
|
|
if (res.rows.length === 0) {
|
|
return eitherRight<T, string>('Not found in database.');
|
|
}
|
|
const entity: T = f(res.rows[0]);
|
|
return eitherLeft<T, string>(entity);
|
|
}
|
|
|
|
async function getEntities<T>(table: string, f: (o: Object) => T): Promise<Array<T>> {
|
|
const client = new_client();
|
|
const res = await client.query(`SELECT * FROM $1;`, [table]);
|
|
const arr: Array<T> = new Array();
|
|
for (let i = 0; i < res.rows.length; ++i) {
|
|
arr.push(f(res.rows[i]));
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
async function registerEntity<T>(table: string, colName: string, name: string, f: (o: Object) => T): Promise<T> {
|
|
const client = new_client();
|
|
const res = await client.query(`INSERT INTO $1 ($2) VALUES ($3) RETURNING *;`, [table, colName, name]);
|
|
const entity: T = f(res.rows[0]);
|
|
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();
|
|
const res = await client.query(`SELECT * FROM pieces NATURAL JOIN colorer NATURAL JOIN couleurs NATURAL JOIN etre_forme NATURAL JOIN formes NATURAL JOIN avoir_motif NATURAL JOIN motifs 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();
|
|
const res = await client.query(`SELECT * FROM pieces NATURAL JOIN colorer NATURAL JOIN couleurs NATURAL JOIN etre_forme NATURAL JOIN formes NATURAL JOIN avoir_motif NATURAL JOIN motifs;`);
|
|
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();
|
|
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])
|
|
return result;
|
|
}
|
|
|
|
async function getBuyedPiecesFromMember(id_member: number): Promise<Array<Pair<Piece, number>>> {
|
|
const client = new_client();
|
|
const res = await client.query(`SELECT id_piece, quantite_acheter FROM acheter WHERE id_membre = $1;`, [id_member]);
|
|
|
|
const arr = 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_acheter']));
|
|
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
export { getBuyedPiecesFromMember, getEntity, getEntities, registerEntity, toColour, getColour, getColours, createColour, toPattern, getPattern, getPatterns, createPattern, toShape, getShape, getShapes, createShape, getPiece, getPieces, createPiece };
|