feat: add piece
This commit is contained in:
parent
b2b769da8f
commit
e3301bd831
@ -37,8 +37,9 @@ async function registerEntity<T>(table: string, colName: string, name: string, f
|
|||||||
const client = new_client();
|
const client = new_client();
|
||||||
await client.connect();
|
await client.connect();
|
||||||
const res = await client.query(`INSERT INTO ${client.escapeIdentifier(table)} (${client.escapeIdentifier(colName)}) VALUES ($1) RETURNING *;`, [name]);
|
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();
|
await client.end();
|
||||||
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
const toColour: (o: Object) => Colour = o => {
|
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);
|
return await registerEntity<Shape>('formes', 'nom_forme', name, toShape);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPiece(id: number): Promise<Piece> {
|
async function getPiece(id: number): Promise<Either<Piece, string>> {
|
||||||
return 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 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>> {
|
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> {
|
async function createPiece(colour: Colour, pattern: Pattern, shape: Shape): Promise<Piece> {
|
||||||
const client = new_client();
|
const client = new_client();
|
||||||
await client.connect();
|
await client.connect();
|
||||||
const res = await client.query(`INSERT INTO pieces () VALUES () RETURNING *;`);
|
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();
|
await client.end();
|
||||||
return result;
|
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) {
|
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.'));
|
res.status(400).send(eitherRight<T, string>('No field `name` in body.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const entity: Either<T, string> = await registerEntity(table, colName, req.body.name, f);
|
const entity: T = await registerEntity(table, colName, req.body.name, f);
|
||||||
if (entity.hasRight) {
|
res.send(entity);
|
||||||
res.status(500).send(entity.right);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
res.send(entity.left);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const retrieveColour = async (req: Request, res: Response) => {
|
const retrieveColour = async (req: Request, res: Response) => {
|
||||||
@ -138,7 +149,7 @@ const retrieveColours = async (req: Request, res: Response) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const registerColour = (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) => {
|
const retrievePattern = (req: Request, res: Response) => {
|
||||||
@ -150,7 +161,7 @@ const retrievePatterns = (req: Request, res: Response) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const registerPattern = (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) => {
|
const retrieveShape = (req: Request, res: Response) => {
|
||||||
@ -162,7 +173,7 @@ const retrieveShapes = (req: Request, res: Response) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const registerShape = (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) => {
|
const retrievePiece = (req: Request, res: Response) => {
|
||||||
@ -173,18 +184,18 @@ const retrievePieces = (req: Request, res: Response) => {
|
|||||||
res.send(getPieces());
|
res.send(getPieces());
|
||||||
};
|
};
|
||||||
|
|
||||||
const registerPiece = (req: Request, res: Response) => {
|
const registerPiece = async (req: Request, res: Response) => {
|
||||||
const colour: Either<Colour, string> = getColour(req.body.colour);
|
const colour: Either<Colour, string> = await getColour(req.body.colour);
|
||||||
if (colour.hasRight) {
|
if (colour.hasRight) {
|
||||||
res.status(500).send(eitherFormatError(colour));
|
res.status(500).send(eitherFormatError(colour));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const pattern: Either<Pattern, string> = getPattern(req.body.pattern);
|
const pattern: Either<Pattern, string> = await getPattern(req.body.pattern);
|
||||||
if (pattern.hasRight) {
|
if (pattern.hasRight) {
|
||||||
res.status(500).send(eitherFormatError(pattern));
|
res.status(500).send(eitherFormatError(pattern));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const shape: Either<Shape, string> = getShape(req.body.shape);
|
const shape: Either<Shape, string> = await getShape(req.body.shape);
|
||||||
if (shape.hasRight) {
|
if (shape.hasRight) {
|
||||||
res.status(500).send(eitherFormatError(shape));
|
res.status(500).send(eitherFormatError(shape));
|
||||||
return;
|
return;
|
||||||
@ -192,4 +203,4 @@ const registerPiece = (req: Request, res: Response) => {
|
|||||||
res.send(createPiece(colour.left, pattern.left, shape.left));
|
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 };
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import express from 'express';
|
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();
|
const router = express.Router();
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ router.get("/colour", retrieveColours);
|
|||||||
router.get("/colour/:id", retrieveColour);
|
router.get("/colour/:id", retrieveColour);
|
||||||
router.post("/colour/register", registerColour);
|
router.post("/colour/register", registerColour);
|
||||||
router.get("/pattern", retrievePatterns);
|
router.get("/pattern", retrievePatterns);
|
||||||
router.get("/colour/:id", retrievePattern);
|
router.get("/pattern/:id", retrievePattern);
|
||||||
router.post("/pattern/register", registerPattern);
|
router.post("/pattern/register", registerPattern);
|
||||||
router.get("/shape", retrieveShapes);
|
router.get("/shape", retrieveShapes);
|
||||||
router.get("/shape/:id", retrieveShape);
|
router.get("/shape/:id", retrieveShape);
|
||||||
|
@ -19,7 +19,7 @@ function eitherRight<T, U>(right: U): Either<T, U> {
|
|||||||
return { hasRight: true, left: undefined, right: right };
|
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) {
|
if (either.hasRight) {
|
||||||
return { id: -1, message: either.right };
|
return { id: -1, message: either.right };
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user