From f294250be61504e63d1899698729cafa0b762aa1 Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Thu, 16 Nov 2023 15:17:59 +0100 Subject: [PATCH] add gateway recipes --- .../src/controllers/recipes.controller.ts | 48 +++++++++++++------ API-Project/src/database/connection.ts | 8 ++-- .../src/gateways/ingredients.gateway.ts | 1 - API-Project/src/gateways/recipe.gateway.ts | 46 ++++++++++++++++++ 4 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 API-Project/src/gateways/recipe.gateway.ts diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index 1dccdc8..8c91a56 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -3,31 +3,49 @@ import { Router } from "express"; import { pool } from "../database/connection"; import { Query, QueryResult } from "pg"; import { IRecipe, 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"; const RecipesController = Router() +const recipes_gw = new RecipeGateway() + RecipesController.get('/', async (req, res) => { - let recipes:IRecipe[] = [] - await pool.query('SELECT * FROM Recipe ORDER BY id', async (error: Error, result: QueryResult) => { - if (error) { - throw error; - } + try { + const recipes = await recipes_gw.getAll() + + res.status(200).json(recipes) + } catch (error) { + const error_error = error as Error + res.status(500).send(error_error.message) + } +}) - for (let key in result.rows) { - const current = result.rows[key] - let recipe = new Recipe(current.id, current.name, current.description, current.time_to_cook); - const ingr_ids = current.ingredients - for (let ingr_id in ingr_ids) { - - } +RecipesController.get('/:id', async (req, res) => { + const id = Number(req.params.id); + + if (!Number.isInteger(id)) { + throw new Exceptions.BadRequestException('id invalid !'); + } + + try { + const recipe = await recipes_gw.getById(id) + + if (recipe == null) { + res.status(404).send('not found') } + else { + const ingredient_ingredient = recipe as Recipe - res.json(result.rows) - }) - return res; + res.status(200).json(recipe) + } + } catch (error) { + const error_error = error as Error + res.status(500).send(error_error.message) + } }) export { RecipesController } \ No newline at end of file diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index fe3cb39..c571205 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: 'rgregnault', - host: 'localhost', - database: 'leftovers', - password: 'motdepasse', + user: 'leftovers_admin', + host: 'postgresql-leftovers.alwaysdata.net', + database: 'leftovers_recipedb', + password: 'AdmPsswd', port: 5432, }) } diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts index dca6feb..4088bc2 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -1,4 +1,3 @@ -import { QueryResult } from "pg"; import { Ingredient } from "../types/ingredients"; import { Connection } from "../database/connection" diff --git a/API-Project/src/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts new file mode 100644 index 0000000..fc7989c --- /dev/null +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -0,0 +1,46 @@ +import { Ingredient } from "../types/ingredients"; +import { Recipe } from "../types/recipes" +import { Connection } from "../database/connection" + +export class RecipeGateway { + connection:Connection + + constructor() { + this.connection = new Connection() + } + + async getAll() : Promise { + this.connection.connect() + + const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id') + + 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); + recipes.push(recipe); + } + + return recipes + } + + async getById(id: number) : Promise{ + this.connection.connect() + + const query = { + text: 'SELECT * FROM Recipes WHERE id =$1', + values: [id], + } + + const res = await this.connection.client.query(query) + + if (res.rowCount != 1) { + return null + } + + const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, res.rows[0].time); + + return recipe; + } +} \ No newline at end of file