feat: filter respected by ingredient or by recipe can be obtained by asking API
continuous-integration/drone/push Build is failing Details

WORK-RRE
Rémi REGNAULT 1 year ago
parent ea4769063d
commit 3e0f027562

@ -1,33 +1,66 @@
import { Router } from "express";
import { IngredientsClassesGateway } from "../gateways/ingredientsClasses.gateway";
import { RecipeGateway } from "../gateways/recipe.gateway";
import { IngredientsClasses } from "../types/ingredientsClasses";
const IngredientsClassesController = Router();
const IngredientsClassesController = Router()
const class_gw: IngredientsClassesGateway = new IngredientsClassesGateway();
const recipe_gw: RecipeGateway = new RecipeGateway();
const class_gw: IngredientsClassesGateway = new IngredientsClassesGateway()
function validateAndParseId(req: any, res: any): number | null {
const id = Number(req.params.id);
if (!Number.isInteger(id)) {
res.status(400).send('Invalid parameter or no parameter');
return null;
}
return id;
}
IngredientsClassesController.get('/ofingr/:id', async (req, res) => {
const id = Number(req.params.id);
const id = validateAndParseId(req, res);
if (id === null) return;
if (!Number.isInteger(id)) {
res.status(400).send('invalid parameter or no parameter')
return
try {
const classes = await class_gw.getForIngredient(id);
if (classes.length === 0) {
res.status(404).send('Not found');
} else {
res.status(200).json(classes);
}
} catch (error) {
const error_error = error as Error;
res.status(500).send(error_error.message);
}
});
IngredientsClassesController.get('/ofrecipe/:id', async (req, res) => {
const id = validateAndParseId(req, res);
if (id === null) return;
try {
const classes = await class_gw.getForIngredient(Number(id))
const recipe = await recipe_gw.getById(id);
if (classes.length == 0) {
res.status(404).send('not found')
if (!recipe) {
res.status(404).send('Recipe not found');
return;
}
else {
res.status(200).json(classes)
let final_classes: IngredientsClasses[] = Array.from(new Set(Object.values(IngredientsClasses)));
for (const ingredient of recipe.ingredients) {
const new_classes: IngredientsClasses[] = await class_gw.getForIngredient(ingredient.id);
final_classes = final_classes.filter(item => new_classes.includes(item));
}
res.status(200).json(final_classes);
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
const error_error = error as Error;
res.status(500).send(error_error.message);
}
})
});
export { IngredientsClassesController }
export { IngredientsClassesController };

@ -1,6 +1,14 @@
import { Connection } from "../database/connection"
import { IngredientsClasses } from "../types/ingredientsClasses"
function convertToEnum<T extends Record<string, string>>(enumObject: T, value: string): T[keyof T] | undefined {
const enumKeys = Object.keys(enumObject) as Array<keyof T>;
const enumValues = enumKeys.map(key => enumObject[key]);
const index = enumValues.indexOf(value as T[keyof T]);
return index !== -1 ? enumValues[index] as T[keyof T] : undefined;
}
export class IngredientsClassesGateway {
connection: Connection
@ -14,7 +22,7 @@ export class IngredientsClassesGateway {
let classes : IngredientsClasses[] = []
const query = {
text: 'SELECT name FROM Composed, IngredientsClasses WHERE idIngredient = $1 AND idClass = id',
text: 'SELECT name FROM IngredientsClassesAttribution, IngredientsClasses WHERE idIngredient = $1 AND idClass = id',
values: [id],
}
@ -24,12 +32,16 @@ export class IngredientsClassesGateway {
for (let row of res.rows) {
const classNameString: string = row.name;
const classNameEnum: IngredientsClasses = IngredientsClasses[classNameString as keyof typeof IngredientsClasses];
const classNameEnum: IngredientsClasses | undefined = convertToEnum(IngredientsClasses, classNameString);
if (classNameEnum !== undefined) {
classes.push(classNameEnum);
}
}
console.log(classes)
return classes
}

Loading…
Cancel
Save