From 32c249e7b87525bf1b306b6d9a43ab87c6422660 Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Mon, 20 Nov 2023 15:02:37 +0100 Subject: [PATCH] route for recipes are completed --- .../src/controllers/recipes.controller.ts | 1 - API-Project/src/database/connection.ts | 4 +-- .../src/gateways/ingredients.gateway.ts | 26 ++++++++++++++++++ API-Project/src/gateways/recipe.gateway.ts | 27 ++++++++++++------- API-Project/src/types/recipes.ts | 8 +++--- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index f3f54bc..d636690 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -34,7 +34,6 @@ RecipesController.get('/:id', async (req, res) => { } else { const ingredient_ingredient = recipe as Recipe - res.status(200).json(recipe) } } catch (error) { diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index c571205..55c586b 100644 --- a/API-Project/src/database/connection.ts +++ b/API-Project/src/database/connection.ts @@ -17,10 +17,10 @@ export class Connection { constructor() { this.client = new Client({ - user: 'leftovers_admin', + user: 'leftovers_appuser', host: 'postgresql-leftovers.alwaysdata.net', database: 'leftovers_recipedb', - password: 'AdmPsswd', + password: 'UsrPsswd', port: 5432, }) } diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts index 503bb91..fcdeb63 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -41,4 +41,30 @@ export class IngredientsGateway { return ingredient } + + async findIngredientsForRecipe(id: Number): Promise { + this.connection.connect(); + + const query = { + text: 'SELECT i.name, i.id FROM Ingredients i, Composed c WHERE c.idRecipe =$1 AND i.id = c.idIngredient', + values: [id], + }; + + const res = await this.connection.client.query(query); + console.log(res) + + if (res.rowCount === 0) { + return null; + } + + + const ingredients = res.rows.map(row => ({ + name: row.name, + id: Number(row.id), // Conversion de l'identifiant en nombre + })); + console.log(ingredients); + + return ingredients as Ingredient[]; + } + } \ No newline at end of file diff --git a/API-Project/src/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts index f0de3bd..ee8a4e5 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -3,26 +3,30 @@ import { Recipe } from "../types/recipes" import { Connection } from "../database/connection" import { Router } from "express"; import { StepsGateway } from "./steps.gateway"; +import { IngredientsGateway } from "./ingredients.gateway"; export class RecipeGateway { connection:Connection + steps_gw:StepsGateway + ingredient_gw:IngredientsGateway constructor() { this.connection = new Connection() + this.steps_gw = new StepsGateway() + this.ingredient_gw = new IngredientsGateway() } async getAll() : Promise { this.connection.connect() - const steps_gw = new StepsGateway() - const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id'); const steps: string[] = []; let recipes:Recipe[] = [] for (let key in res.rows) { - const steps = await steps_gw.getForRecipes(Number(key)); - let recipe:Recipe = new Recipe(Number(res.rows[key].id), res.rows[key].name, res.rows[key].description, res.rows[key].time, steps); + const steps = await this.steps_gw.getForRecipes(Number(key)); + const ingredients = await this.ingredient_gw.findIngredientsForRecipe(Number(key)) + let recipe:Recipe = new Recipe(Number(res.rows[key].id), res.rows[key].name, res.rows[key].description, res.rows[key].time, steps, ingredients); recipes.push(recipe); } @@ -33,7 +37,6 @@ export class RecipeGateway { async getById(id: Number) : Promise{ this.connection.connect() - const steps_gw = new StepsGateway() const query = { text: 'SELECT * FROM Recipes WHERE id =$1', @@ -46,11 +49,15 @@ export class RecipeGateway { return null } - const steps = await steps_gw.getForRecipes(id) - console.log(steps); - const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, res.rows[0].time, steps); - console.log(recipe); - + const steps = await this.steps_gw.getForRecipes(id) + const ingredients = await this.ingredient_gw.findIngredientsForRecipe(id) + const recipe = new Recipe(Number(res.rows[0].id), + res.rows[0].name, + res.rows[0].description, + Number(res.rows[0].time), + steps, ingredients); + console.log(ingredients); + return recipe; } } \ No newline at end of file diff --git a/API-Project/src/types/recipes.ts b/API-Project/src/types/recipes.ts index 42e3071..1661444 100644 --- a/API-Project/src/types/recipes.ts +++ b/API-Project/src/types/recipes.ts @@ -1,4 +1,4 @@ -import type { IIngredient } from "./ingredients" +import type { IIngredient, Ingredient } from "./ingredients" export interface IRecipe { readonly id: number, @@ -17,13 +17,13 @@ export class Recipe implements IRecipe { ingredients: IIngredient[] steps: string[] - constructor(id: number, name: string, description: string, time_to_cook: number, steps: string[]) { + constructor(id: number, name: string, description: string, time_to_cook: number, steps: string[], ingredients: Ingredient[]) { this.id = id this.name = name this.description = description this.time_to_cook = time_to_cook - this.ingredients = [] - this.steps = steps; + this.ingredients = ingredients + this.steps = steps } addStep(newStep: string) {