Merge pull request 'route for recipes are completed' (#7) from WORK-RHA into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #7
pull/9/head
Rayhân HASSOU 1 year ago
commit 1dfc3040ad

@ -34,7 +34,6 @@ RecipesController.get('/:id', async (req, res) => {
}
else {
const ingredient_ingredient = recipe as Recipe
res.status(200).json(recipe)
}
} catch (error) {

@ -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,
})
}

@ -41,4 +41,30 @@ export class IngredientsGateway {
return ingredient
}
async findIngredientsForRecipe(id: Number): Promise<any> {
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[];
}
}

@ -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<Recipe[]> {
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<Recipe | null>{
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;
}
}

@ -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) {

Loading…
Cancel
Save