feat: filter respected by ingredient or by recipe can be obtained by asking API
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
ea4769063d
commit
3e0f027562
@ -1,33 +1,66 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { IngredientsClassesGateway } from "../gateways/ingredientsClasses.gateway";
|
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) => {
|
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)) {
|
try {
|
||||||
res.status(400).send('invalid parameter or no parameter')
|
const classes = await class_gw.getForIngredient(id);
|
||||||
return
|
|
||||||
|
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 {
|
try {
|
||||||
const classes = await class_gw.getForIngredient(Number(id))
|
const recipe = await recipe_gw.getById(id);
|
||||||
|
|
||||||
if (classes.length == 0) {
|
if (!recipe) {
|
||||||
res.status(404).send('not found')
|
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) {
|
} catch (error) {
|
||||||
const error_error = error as Error
|
const error_error = error as Error;
|
||||||
res.status(500).send(error_error.message)
|
res.status(500).send(error_error.message);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
export { IngredientsClassesController }
|
export { IngredientsClassesController };
|
||||||
|
Loading…
Reference in new issue