From 7d042d5a171899cd68efaae36af502433f165a1c Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Mon, 20 Nov 2023 10:34:36 +0100 Subject: [PATCH] now the routes for recipes also returns the steps --- .../src/controllers/recipes.controller.ts | 7 +--- .../src/controllers/steps.controller.ts | 35 +++++++++++++++++++ .../src/gateways/ingredients.gateway.ts | 2 +- API-Project/src/gateways/recipe.gateway.ts | 24 +++++++++---- API-Project/src/gateways/steps.gateway.ts | 26 ++++++++++++++ API-Project/src/server.ts | 2 ++ API-Project/src/types/recipes.ts | 4 +-- 7 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 API-Project/src/controllers/steps.controller.ts create mode 100644 API-Project/src/gateways/steps.gateway.ts diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index 8c91a56..f3f54bc 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -1,11 +1,6 @@ -import { Request, Response, NextFunction } from "express"; import { Router } from "express"; -import { pool } from "../database/connection"; -import { Query, QueryResult } from "pg"; -import { IRecipe, Recipe } from "../types/recipes"; +import { Recipe } from "../types/recipes"; import { Exceptions } from "../utils/exception"; -import { IngredientsController } from "./ingredients.controller"; -import { IIngredient } from "../types/ingredients"; import { RecipeGateway } from "../gateways/recipe.gateway"; diff --git a/API-Project/src/controllers/steps.controller.ts b/API-Project/src/controllers/steps.controller.ts new file mode 100644 index 0000000..e2bc432 --- /dev/null +++ b/API-Project/src/controllers/steps.controller.ts @@ -0,0 +1,35 @@ +import { Router } from "express"; +import { Recipe } from "../types/recipes"; +import { Exceptions } from "../utils/exception"; +import { StepsGateway } from "../gateways/steps.gateway"; + + +const StepsController = Router() +const steps_gw = new StepsGateway() + + +StepsController.get('/:id', async (req, res) => { + const id = String(req.params.id); + + if (!Number.isInteger(id)) { + throw new Exceptions.BadRequestException('id invalid !'); + } + + try { + const steps = await steps_gw.getForRecipes(Number(id)) + + if (steps == null) { + res.status(404).send('not found') + } + else { + const steps_steps = steps as string[] + + res.status(200).json(steps) + } + } catch (error) { + const error_error = error as Error + res.status(500).send(error_error.message) + } +}) + +export { StepsController } \ No newline at end of file diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts index 4088bc2..503bb91 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -23,7 +23,7 @@ export class IngredientsGateway { return ingredients } - async findOneById(id: number) : Promise { + async findOneById(id: number) : Promise { this.connection.connect() const query = { diff --git a/API-Project/src/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts index fc7989c..f0de3bd 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -1,6 +1,8 @@ import { Ingredient } from "../types/ingredients"; import { Recipe } from "../types/recipes" import { Connection } from "../database/connection" +import { Router } from "express"; +import { StepsGateway } from "./steps.gateway"; export class RecipeGateway { connection:Connection @@ -11,22 +13,27 @@ export class RecipeGateway { 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 res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id'); + + const steps: string[] = []; let recipes:Recipe[] = [] - console.log(res.rows); for (let key in res.rows) { - let recipe:Recipe = new Recipe(Number(res.rows[key].id), res.rows[key].name, res.rows[key].description, res.rows[key].time); + 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); recipes.push(recipe); } + console.log(recipes); + return recipes } - async getById(id: number) : Promise{ + async getById(id: Number) : Promise{ this.connection.connect() + const steps_gw = new StepsGateway() const query = { text: 'SELECT * FROM Recipes WHERE id =$1', @@ -39,8 +46,11 @@ export class RecipeGateway { return null } - const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, res.rows[0].time); - + 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); + return recipe; } } \ No newline at end of file diff --git a/API-Project/src/gateways/steps.gateway.ts b/API-Project/src/gateways/steps.gateway.ts new file mode 100644 index 0000000..0371b05 --- /dev/null +++ b/API-Project/src/gateways/steps.gateway.ts @@ -0,0 +1,26 @@ +import { Recipe } from "../types/recipes" +import { Connection } from "../database/connection" + +export class StepsGateway { + connection:Connection + + constructor() { + this.connection = new Connection() + } + + + async getForRecipes(id: Number): Promise { + this.connection.connect(); + + const query = { + text: 'SELECT action FROM Steps WHERE idRecipe = $1 ORDER BY numstep', + values: [id], + }; + + const res = await this.connection.client.query(query); + + const steps = res.rows.map(row => row.action); + + return steps as string[]; + } +} \ No newline at end of file diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 71d8c43..9da9eb0 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -1,6 +1,7 @@ import express from "express"; import { IngredientsController } from "./controllers/ingredients.controller"; import { RecipesController } from "./controllers/recipes.controller"; +import { StepsController } from "./controllers/steps.controller"; const app = express(); @@ -10,6 +11,7 @@ app.get('/', (req, res) => { app.use('/ingredients', IngredientsController); app.use('/recipes', RecipesController); +app.use('/steps', StepsController) const port = process.env.PORT || 3000; diff --git a/API-Project/src/types/recipes.ts b/API-Project/src/types/recipes.ts index be9cfa8..42e3071 100644 --- a/API-Project/src/types/recipes.ts +++ b/API-Project/src/types/recipes.ts @@ -17,13 +17,13 @@ export class Recipe implements IRecipe { ingredients: IIngredient[] steps: string[] - constructor(id: number, name: string, description: string, time_to_cook: number) { + constructor(id: number, name: string, description: string, time_to_cook: number, steps: string[]) { this.id = id this.name = name this.description = description this.time_to_cook = time_to_cook this.ingredients = [] - this.steps = [] + this.steps = steps; } addStep(newStep: string) { -- 2.36.3