feat: add pieces
This commit is contained in:
parent
afe50c6091
commit
54e113d8db
@ -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' });
|
||||
@ -33,6 +33,14 @@ async function getEntities<T>(table: string, f: (o: Object) => T): Promise<Array
|
||||
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 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'] };
|
||||
};
|
||||
@ -46,7 +54,7 @@ async function getColours(): Promise<Array<Colour>> {
|
||||
}
|
||||
|
||||
async function createColour(name: string): Promise<Colour> {
|
||||
return getDefaultColour();
|
||||
return await registerEntity<Colour>('couleurs', 'nom_couleur', name, toColour);
|
||||
}
|
||||
|
||||
const toPattern: (o: Object) => Pattern = o => {
|
||||
@ -62,7 +70,7 @@ async function getPatterns(): Promise<Array<Pattern>> {
|
||||
}
|
||||
|
||||
async function createPattern(name: string): Promise<Pattern> {
|
||||
return getDefaultPattern();
|
||||
return await registerEntity<Pattern>('motifs', 'nom_motif', name, toPattern);
|
||||
}
|
||||
|
||||
const toShape: (o: Object) => Shape = o => {
|
||||
@ -78,7 +86,7 @@ async function getShapes(): Promise<Array<Shape>> {
|
||||
}
|
||||
|
||||
async function createShape(name: string): Promise<Shape> {
|
||||
return getDefaultShape();
|
||||
return await registerEntity<Shape>('formes', 'nom_forme', name, toShape);
|
||||
}
|
||||
|
||||
async function getPiece(id: number): Promise<Piece> {
|
||||
@ -89,8 +97,13 @@ async function getPieces(): Promise<Array<Piece>> {
|
||||
return new Array(getDefaultPiece());
|
||||
}
|
||||
|
||||
async function createPiece(colour: number, pattern: number, shape: number): Promise<Piece> {
|
||||
return getDefaultPiece();
|
||||
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.end();
|
||||
return result;
|
||||
}
|
||||
|
||||
async function retrieveEntity<T>(f: (id: number) => Promise<Either<T, string>>, req: Request, res: Response) {
|
||||
@ -102,6 +115,19 @@ async function retrieveEntity<T>(f: (id: number) => Promise<Either<T, string>>,
|
||||
res.send(entity.left);
|
||||
}
|
||||
|
||||
async function createEntity<T>(f: (o: Object) => T, table: string, colName: string, req: Request, res: Response) {
|
||||
if (!req | !req.body | !req.body.name) {
|
||||
res.status(400).send(eitherRight<T, string>('No field `name` in body.'));
|
||||
return;
|
||||
}
|
||||
const entity: Either<T, string> = 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<Colour>(getColour, req, res);
|
||||
};
|
||||
@ -112,7 +138,7 @@ const retrieveColours = async (req: Request, res: Response) => {
|
||||
};
|
||||
|
||||
const registerColour = (req: Request, res: Response) => {
|
||||
|
||||
createEntity<Colour>("couleurs", getColour, req, res);
|
||||
};
|
||||
|
||||
const retrievePattern = (req: Request, res: Response) => {
|
||||
@ -124,7 +150,7 @@ const retrievePatterns = (req: Request, res: Response) => {
|
||||
};
|
||||
|
||||
const registerPattern = (req: Request, res: Response) => {
|
||||
|
||||
createEntity<Pattern>("motifs", getPattern, req, res);
|
||||
};
|
||||
|
||||
const retrieveShape = (req: Request, res: Response) => {
|
||||
@ -136,7 +162,7 @@ const retrieveShapes = (req: Request, res: Response) => {
|
||||
};
|
||||
|
||||
const registerShape = (req: Request, res: Response) => {
|
||||
|
||||
createEntity<Shape>("formes", getShape, req, res);
|
||||
};
|
||||
|
||||
const retrievePiece = (req: Request, res: Response) => {
|
||||
@ -148,7 +174,22 @@ const retrievePieces = (req: Request, res: Response) => {
|
||||
};
|
||||
|
||||
const registerPiece = (req: Request, res: Response) => {
|
||||
|
||||
const colour: Either<Colour, string> = getColour(req.body.colour);
|
||||
if (colour.hasRight) {
|
||||
res.status(500).send(eitherFormatError(colour));
|
||||
return;
|
||||
}
|
||||
const pattern: Either<Pattern, string> = getPattern(req.body.pattern);
|
||||
if (pattern.hasRight) {
|
||||
res.status(500).send(eitherFormatError(pattern));
|
||||
return;
|
||||
}
|
||||
const shape: Either<Shape, string> = 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 };
|
||||
|
@ -4,6 +4,13 @@ type Either<T, U> = {
|
||||
right: U
|
||||
};
|
||||
|
||||
type Error = {
|
||||
id: number,
|
||||
message: string
|
||||
};
|
||||
|
||||
type Result<T> = Error | T;
|
||||
|
||||
function eitherLeft<T, U>(left: T): Either<T, U> {
|
||||
return { hasRight: false, left: left, right: undefined };
|
||||
}
|
||||
@ -12,4 +19,11 @@ function eitherRight<T, U>(right: U): Either<T, U> {
|
||||
return { hasRight: true, left: undefined, right: right };
|
||||
}
|
||||
|
||||
export { Either, eitherLeft, eitherRight };
|
||||
function eitherFormatError<T, string>(either: Either<T, string>): Result<T> {
|
||||
if (either.hasRight) {
|
||||
return { id: -1, message: either.right };
|
||||
}
|
||||
return either.left;
|
||||
}
|
||||
|
||||
export { Either, eitherLeft, eitherRight, eitherFormatError };
|
||||
|
Loading…
x
Reference in New Issue
Block a user