From acff49b692d6fabb8bb2a3bf92e467a29f31edb6 Mon Sep 17 00:00:00 2001 From: Remi R Date: Thu, 16 Nov 2023 10:11:51 +0100 Subject: [PATCH] feat: divide old ingredients controller into new ingredient controller and ingredient gateway --- .../src/controllers/ingredients.controller.ts | 54 +++++++++---------- .../src/controllers/recipes.controller.ts | 25 ++++++++- API-Project/src/database/connection.ts | 28 +++++++++- .../src/gateways/ingredients.gateway.ts | 45 ++++++++++++++++ API-Project/src/types/recipes.ts | 34 ++++++++++-- 5 files changed, 151 insertions(+), 35 deletions(-) create mode 100644 API-Project/src/gateways/ingredients.gateway.ts diff --git a/API-Project/src/controllers/ingredients.controller.ts b/API-Project/src/controllers/ingredients.controller.ts index 0f0a5f5..16ea131 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -4,50 +4,48 @@ import { Exceptions } from "../utils/exception"; import { IIngredient, Ingredient } from "../types/ingredients"; import { pool } from "../database/connection"; import { Query, QueryResult } from "pg"; +import { IngredientsGateway } from "../gateways/ingredients.gateway"; const IngredientsController = Router() +const ingredient_gw = new IngredientsGateway() + /** To get all ingredients */ -IngredientsController.get('/', (req, res) => { - let ingredients:Ingredient[] = [] - pool.query({text:'SELECT * FROM Ingredients ORDER BY id'}, (error: Error, results: QueryResult) => { - if (error) { - throw(error); - } - for (let key in results.rows) { - let ingredient:Ingredient = new Ingredient(Number(results.rows[key].id), results.rows[key].name); - ingredients.push(ingredient); - } - res.status(200); - res.json(ingredients); - }) - return res; +IngredientsController.get('/', async (req, res) => { + try { + const ingredients = await ingredient_gw.getAll() + + res.status(200).json(ingredients) + } catch (error) { + const error_error = error as Error + res.status(500).send(error_error.message) + } }) /** To get one ingredient by id */ -IngredientsController.get('/:id', (req, res) => { +IngredientsController.get('/:id', async (req, res) => { const id = Number(req.params.id); if (!Number.isInteger(id)) { throw new Exceptions.BadRequestException('id invalid !'); } - pool.query({text:'SELECT * FROM Ingredients WHERE id =$1', - values: [id]}, (error: Error, results: QueryResult) => { - if (error) { - throw(error) - } - - if (results.rowCount == 0) { - throw new Exceptions.NotFoundException('no ingredient with this id'); - } + try { + const ingredient = await ingredient_gw.findOneById(id) - res.status(200) - res.json(results.rows) - }) + if (ingredient == null) { + res.status(404).send('not found') + } + else { + const ingredient_ingredient = ingredient as Ingredient - return res; + res.status(200).json(ingredient) + } + } catch (error) { + const error_error = error as Error + res.status(500).send(error_error.message) + } }) export { IngredientsController } \ No newline at end of file diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index 2d89476..1dccdc8 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -1,11 +1,32 @@ 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 { IngredientsController } from "./ingredients.controller"; +import { IIngredient } from "../types/ingredients"; const RecipesController = Router() -RecipesController.get('/', (req, res) => { - res.send("Liste des recettes"); +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; + } + + 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) { + + } + } + + res.json(result.rows) + }) return res; }) diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index 23bc6cf..fe3cb39 100644 --- a/API-Project/src/database/connection.ts +++ b/API-Project/src/database/connection.ts @@ -1,8 +1,34 @@ +import { Client } from "pg" + const Pool = require('pg').Pool + export const pool = new Pool({ user: 'rgregnault', host: 'localhost', database: 'leftovers', password: 'motdepasse', port: 5432, -}) \ No newline at end of file +}) + + +export class Connection { + public client:Client + clientIsConnected:boolean = false + + constructor() { + this.client = new Client({ + user: 'rgregnault', + host: 'localhost', + database: 'leftovers', + password: 'motdepasse', + port: 5432, + }) + } + + public async connect() { + if (!this.clientIsConnected) { + await this.client.connect() + this.clientIsConnected = true + } + } +} \ No newline at end of file diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts new file mode 100644 index 0000000..dca6feb --- /dev/null +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -0,0 +1,45 @@ +import { QueryResult } from "pg"; +import { Ingredient } from "../types/ingredients"; +import { Connection } from "../database/connection" + +export class IngredientsGateway { + connection:Connection + + constructor() { + this.connection = new Connection() + } + + async getAll() : Promise { + this.connection.connect() + + const res = await this.connection.client.query('SELECT * FROM Ingredients ORDER BY id') + + let ingredients:Ingredient[] = [] + + for (let key in res.rows) { + let ingredient:Ingredient = new Ingredient(Number(res.rows[key].id), res.rows[key].name); + ingredients.push(ingredient); + } + + return ingredients + } + + async findOneById(id: number) : Promise { + this.connection.connect() + + const query = { + text: 'SELECT * FROM Ingredients WHERE id =$1', + values: [id], + } + + const res = await this.connection.client.query(query) + + if (res.rowCount != 1) { + return null + } + + const ingredient = new Ingredient(Number(res.rows[0].id), String(res.rows[0].name)) + + return ingredient + } +} \ No newline at end of file diff --git a/API-Project/src/types/recipes.ts b/API-Project/src/types/recipes.ts index 3f93c4b..be9cfa8 100644 --- a/API-Project/src/types/recipes.ts +++ b/API-Project/src/types/recipes.ts @@ -1,10 +1,36 @@ import type { IIngredient } from "./ingredients" export interface IRecipe { - id: number, - name: string, - description: string, - time_to_cook: number, + readonly id: number, + readonly name: string, + readonly description: string, + readonly time_to_cook: number, ingredients: IIngredient[], steps: string[] +} + +export class Recipe implements IRecipe { + id: number + name: string + description: string + time_to_cook: number + ingredients: IIngredient[] + steps: string[] + + constructor(id: number, name: string, description: string, time_to_cook: number) { + this.id = id + this.name = name + this.description = description + this.time_to_cook = time_to_cook + this.ingredients = [] + this.steps = [] + } + + addStep(newStep: string) { + this.steps.push(newStep) + } + + addIngredient(newIngredient: IIngredient) { + this.ingredients.push(newIngredient) + } } \ No newline at end of file