feat: add piece

This commit is contained in:
Alessandre Laguierce 2024-11-27 14:45:37 +01:00
parent b2b769da8f
commit e3301bd831
4 changed files with 46 additions and 35 deletions

View File

@ -37,8 +37,9 @@ async function registerEntity<T>(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<Shape> {
return await registerEntity<Shape>('formes', 'nom_forme', name, toShape);
}
async function getPiece(id: number): Promise<Piece> {
return getDefaultPiece();
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>> {
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<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 };
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<T>(f: (id: number) => Promise<Either<T, string>>,
}
async function createEntity<T>(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<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 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<Colour>("couleurs", getColour, req, res);
createEntity<Colour>(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<Pattern>("motifs", getPattern, req, res);
createEntity<Pattern>(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<Shape>("formes", getShape, req, res);
createEntity<Shape>(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<Colour, string> = getColour(req.body.colour);
const registerPiece = async (req: Request, res: Response) => {
const colour: Either<Colour, string> = await getColour(req.body.colour);
if (colour.hasRight) {
res.status(500).send(eitherFormatError(colour));
return;
}
const pattern: Either<Pattern, string> = getPattern(req.body.pattern);
const pattern: Either<Pattern, string> = await getPattern(req.body.pattern);
if (pattern.hasRight) {
res.status(500).send(eitherFormatError(pattern));
return;
}
const shape: Either<Shape, string> = getShape(req.body.shape);
const shape: Either<Shape, string> = 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 };

View File

@ -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);

View File

@ -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<Piece>
id_complex: number,
subpieces: Array<Piece>
};
export { Colour, Pattern, Shape, Piece, ComplexPiece };

View File

@ -19,7 +19,7 @@ function eitherRight<T, U>(right: U): Either<T, U> {
return { hasRight: true, left: undefined, right: right };
}
function eitherFormatError<T, string>(either: Either<T, string>): Result<T> {
function eitherFormatError<T, Error>(either: Either<T, string>): Result<T> {
if (either.hasRight) {
return { id: -1, message: either.right };
}