feat: add route multiples ingredients to find a recipe
continuous-integration/drone/push Build is failing Details

WORK-RHA
Rémi REGNAULT 1 year ago
parent ff12235c62
commit 8c399384c3

@ -40,4 +40,30 @@ RecipesController.get('/:id', async (req, res) => {
} }
}) })
RecipesController.get('/withingr/:ids', async (req, res) => {
let ids: number[] = [];
let raw_ids = String(req.params.ids).split(':')
for (let key in raw_ids) {
const test = Number(raw_ids[key])
if (Number.isNaN(test) || !Number.isInteger(test)) {
res.status(400).json('A parameter is not an integer')
}
ids.push(Number(test))
}
try {
const recipes = await recipes_gw.getIdsRecipesThatContainsIngredients(ids)
if (recipes.length == 0) {
res.status(404).json('no data found')
}
else {
res.status(200).json(recipes)
}
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
}
})
export { RecipesController } export { RecipesController }

@ -58,4 +58,34 @@ export class RecipeGateway {
return recipe; return recipe;
} }
async getIdsRecipesThatContainsIngredients(ids: number[]) : Promise<Recipe[]> {
let recipes: Recipe[] = []
const client = await this.connection.getPoolClient()
let query_list_text = '($1'
for (let count = 1; count < ids.length; count++) {
query_list_text = query_list_text + ', $' + String(count+1)
}
query_list_text = query_list_text + ')'
const query = {
text: 'SELECT idRecipe FROM Composed GROUP BY idRecipe HAVING COUNT(DISTINCT idIngredient) = COUNT(DISTINCT CASE WHEN idIngredient IN ' + query_list_text + ' THEN idIngredient END)',
values: ids
}
const res = await client.query(query)
client.release()
for(var key in res.rows) {
const recipe = await this.getById(Number(res.rows[key].idrecipe))
if (recipe != null) {
recipes.push(recipe)
}
}
return recipes
}
} }
Loading…
Cancel
Save