From ababfc7a333250613d4d2b420b1fd039772c3735 Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 10:26:15 +0100 Subject: [PATCH 01/11] feat: class connection will use from now the env variable --- API-Project/package-lock.json | 12 +++++++++++ API-Project/package.json | 7 +++---- .../src/controllers/ingredients.controller.ts | 5 +---- .../src/controllers/steps.controller.ts | 1 - API-Project/src/database/connection.ts | 20 +++++-------------- API-Project/src/gateways/steps.gateway.ts | 1 - API-Project/src/server.ts | 3 +++ 7 files changed, 24 insertions(+), 25 deletions(-) diff --git a/API-Project/package-lock.json b/API-Project/package-lock.json index 9ff34b9..0b56a9f 100644 --- a/API-Project/package-lock.json +++ b/API-Project/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@types/express": "^4.17.21", "@types/morgan": "^1.9.9", + "dotenv": "^16.3.1", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1", @@ -2237,6 +2238,17 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/API-Project/package.json b/API-Project/package.json index 9ed2661..1e01c17 100644 --- a/API-Project/package.json +++ b/API-Project/package.json @@ -8,12 +8,13 @@ "build": "tsup src/server.ts --format cjs --clean", "dev": "nodemon --watch src -e js,ts,json --exec \"ts-node src/server.ts\"", "test": "jest --passWithNoTests" - }, + }, "author": "", "license": "ISC", "dependencies": { "@types/express": "^4.17.21", "@types/morgan": "^1.9.9", + "dotenv": "^16.3.1", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1", @@ -23,11 +24,9 @@ "typescript": "^5.2.2" }, "devDependencies": { - - "@types/pg": "^8.10.9", "@types/jest": "^29.5.8", + "@types/pg": "^8.10.9", "jest": "^29.7.0", "ts-jest": "^29.1.1" - } } diff --git a/API-Project/src/controllers/ingredients.controller.ts b/API-Project/src/controllers/ingredients.controller.ts index 16ea131..4a99322 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -1,9 +1,6 @@ -import { Request, Response, NextFunction } from "express"; import { Router } from "express"; import { Exceptions } from "../utils/exception"; -import { IIngredient, Ingredient } from "../types/ingredients"; -import { pool } from "../database/connection"; -import { Query, QueryResult } from "pg"; +import { Ingredient } from "../types/ingredients"; import { IngredientsGateway } from "../gateways/ingredients.gateway"; diff --git a/API-Project/src/controllers/steps.controller.ts b/API-Project/src/controllers/steps.controller.ts index e2bc432..cf3612c 100644 --- a/API-Project/src/controllers/steps.controller.ts +++ b/API-Project/src/controllers/steps.controller.ts @@ -1,5 +1,4 @@ import { Router } from "express"; -import { Recipe } from "../types/recipes"; import { Exceptions } from "../utils/exception"; import { StepsGateway } from "../gateways/steps.gateway"; diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index 55c586b..2dadde3 100644 --- a/API-Project/src/database/connection.ts +++ b/API-Project/src/database/connection.ts @@ -1,15 +1,5 @@ import { Client } from "pg" -const Pool = require('pg').Pool - -export const pool = new Pool({ - user: 'rgregnault', - host: 'localhost', - database: 'leftovers', - password: 'motdepasse', - port: 5432, -}) - export class Connection { public client:Client @@ -17,11 +7,11 @@ export class Connection { constructor() { this.client = new Client({ - user: 'leftovers_appuser', - host: 'postgresql-leftovers.alwaysdata.net', - database: 'leftovers_recipedb', - password: 'UsrPsswd', - port: 5432, + user: process.env.DB_USERNAME, + host: process.env.DB_DBHOST, + database: process.env.DB_DBNAME, + password: process.env.DB_USERPASSWORD, + port: Number(process.env.DB_PORT), }) } diff --git a/API-Project/src/gateways/steps.gateway.ts b/API-Project/src/gateways/steps.gateway.ts index 0371b05..99897ab 100644 --- a/API-Project/src/gateways/steps.gateway.ts +++ b/API-Project/src/gateways/steps.gateway.ts @@ -1,4 +1,3 @@ -import { Recipe } from "../types/recipes" import { Connection } from "../database/connection" export class StepsGateway { diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 9da9eb0..99cb44d 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -1,4 +1,7 @@ import express from "express"; + +require('dotenv').config(); + import { IngredientsController } from "./controllers/ingredients.controller"; import { RecipesController } from "./controllers/recipes.controller"; import { StepsController } from "./controllers/steps.controller"; From fed6a2b7e37cf734841ee7fb8e4d736911c581be Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 10:45:21 +0100 Subject: [PATCH 02/11] fix: set up environment variables about database in ci/cd --- .drone.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.drone.yml b/.drone.yml index 9121e57..75bff91 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,6 +6,18 @@ trigger: event: - push +environment: + DB_USERNAME: + from_secret: DB_USERNAME + DB_DBHOST: + from_secret: DB_DBHOST + DB_DBNAME: + from_secret: DB_DBNAME + DB_USERPASSWORD: + from_secret: DB_USERPASSWORD + DB_PORT: + from_secret: DB_PORT + steps: - name: api-build image: node:latest From 0f94800d4fe9f6d5b30442a86bc2857569d27c2f Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 10:54:02 +0100 Subject: [PATCH 03/11] fix: environment variables for ci/cd --- .drone.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.drone.yml b/.drone.yml index 75bff91..8180e37 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,15 +8,15 @@ trigger: environment: DB_USERNAME: - from_secret: DB_USERNAME + from_secret: SECRET_DB_USERNAME DB_DBHOST: - from_secret: DB_DBHOST + from_secret: SECRET_DB_DBHOST DB_DBNAME: - from_secret: DB_DBNAME + from_secret: SECRET_DB_DBNAME DB_USERPASSWORD: - from_secret: DB_USERPASSWORD + from_secret: SECRET_DB_USERPASSWORD DB_PORT: - from_secret: DB_PORT + from_secret: SECRET_DB_PORT steps: - name: api-build From df73ce269ca1950ceb37b2a893e5f38de294e03c Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 10:58:53 +0100 Subject: [PATCH 04/11] fix: environment variables for ci/cd --- .drone.yml | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/.drone.yml b/.drone.yml index 8180e37..36b49aa 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,21 +6,20 @@ trigger: event: - push -environment: - DB_USERNAME: - from_secret: SECRET_DB_USERNAME - DB_DBHOST: - from_secret: SECRET_DB_DBHOST - DB_DBNAME: - from_secret: SECRET_DB_DBNAME - DB_USERPASSWORD: - from_secret: SECRET_DB_USERPASSWORD - DB_PORT: - from_secret: SECRET_DB_PORT - steps: - name: api-build image: node:latest + environment: + DB_USERNAME: + from_secret: SECRET_DB_USERNAME + DB_DBHOST: + from_secret: SECRET_DB_DBHOST + DB_DBNAME: + from_secret: SECRET_DB_DBNAME + DB_USERPASSWORD: + from_secret: SECRET_DB_USERPASSWORD + DB_PORT: + from_secret: SECRET_DB_PORT commands: - cd ./API-Project - npm install From 7ef32ec58be0c4d4df22a9097a03ba2d36b4c7cd Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 11:08:48 +0100 Subject: [PATCH 05/11] fix: environment variables for ci/cd --- .drone.yml | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/.drone.yml b/.drone.yml index 36b49aa..9c63a1b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -27,6 +27,17 @@ steps: - name: test image: node:latest + environment: + DB_USERNAME: + from_secret: SECRET_DB_USERNAME + DB_DBHOST: + from_secret: SECRET_DB_DBHOST + DB_DBNAME: + from_secret: SECRET_DB_DBNAME + DB_USERPASSWORD: + from_secret: SECRET_DB_USERPASSWORD + DB_PORT: + from_secret: SECRET_DB_PORT commands: - cd ./API-Project - npm run test @@ -34,6 +45,17 @@ steps: - name: docker-build-and-push image: plugins/docker + environment: + DB_USERNAME: + from_secret: SECRET_DB_USERNAME + DB_DBHOST: + from_secret: SECRET_DB_DBHOST + DB_DBNAME: + from_secret: SECRET_DB_DBNAME + DB_USERPASSWORD: + from_secret: SECRET_DB_USERPASSWORD + DB_PORT: + from_secret: SECRET_DB_PORT settings: dockerfile: API-Project/Dockerfile context: API-Project @@ -49,11 +71,21 @@ steps: - name: deploy-container image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: - IMAGENAME: hub.codefirst.iut.uca.fr/rayhan.hassou/leftovers_api:latest - CONTAINERNAME: left-over-container - COMMAND: create - OVERWRITE: true - ADMINS: louison.parant,remi.regnault,rayhan.hassou + IMAGENAME: hub.codefirst.iut.uca.fr/rayhan.hassou/leftovers_api:latest + CONTAINERNAME: left-over-container + COMMAND: create + OVERWRITE: true + ADMINS: louison.parant,remi.regnault,rayhan.hassou + DB_USERNAME: + from_secret: SECRET_DB_USERNAME + DB_DBHOST: + from_secret: SECRET_DB_DBHOST + DB_DBNAME: + from_secret: SECRET_DB_DBNAME + DB_USERPASSWORD: + from_secret: SECRET_DB_USERPASSWORD + DB_PORT: + from_secret: SECRET_DB_PORT depends_on: [ docker-build-and-push ] - name: code-analysis From b4b6338ad5bdc2c05535a007758ea104e18e0ca1 Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 14:37:52 +0100 Subject: [PATCH 06/11] remove useless import --- API-Project/src/gateways/recipe.gateway.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/API-Project/src/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts index ee8a4e5..df3b1c1 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -1,7 +1,5 @@ -import { Ingredient } from "../types/ingredients"; import { Recipe } from "../types/recipes" import { Connection } from "../database/connection" -import { Router } from "express"; import { StepsGateway } from "./steps.gateway"; import { IngredientsGateway } from "./ingredients.gateway"; From 53b6798e48683488b68e33e94d83ed9ea295751d Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 14:56:54 +0100 Subject: [PATCH 07/11] working on code smells --- API-Project/src/controllers/ingredients.controller.ts | 2 -- API-Project/src/controllers/recipes.controller.ts | 1 - API-Project/src/controllers/steps.controller.ts | 2 -- API-Project/src/gateways/ingredients.gateway.ts | 2 +- API-Project/src/gateways/recipe.gateway.ts | 8 ++++---- API-Project/src/gateways/steps.gateway.ts | 2 +- 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/API-Project/src/controllers/ingredients.controller.ts b/API-Project/src/controllers/ingredients.controller.ts index 4a99322..8889d5e 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -35,8 +35,6 @@ IngredientsController.get('/:id', async (req, res) => { res.status(404).send('not found') } else { - const ingredient_ingredient = ingredient as Ingredient - res.status(200).json(ingredient) } } catch (error) { diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index d636690..6c08fb1 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -33,7 +33,6 @@ RecipesController.get('/:id', async (req, res) => { res.status(404).send('not found') } else { - const ingredient_ingredient = recipe as Recipe res.status(200).json(recipe) } } catch (error) { diff --git a/API-Project/src/controllers/steps.controller.ts b/API-Project/src/controllers/steps.controller.ts index cf3612c..347896a 100644 --- a/API-Project/src/controllers/steps.controller.ts +++ b/API-Project/src/controllers/steps.controller.ts @@ -21,8 +21,6 @@ StepsController.get('/:id', async (req, res) => { res.status(404).send('not found') } else { - const steps_steps = steps as string[] - res.status(200).json(steps) } } catch (error) { diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts index fcdeb63..6e282d8 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -42,7 +42,7 @@ export class IngredientsGateway { return ingredient } - async findIngredientsForRecipe(id: Number): Promise { + async findIngredientsForRecipe(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 df3b1c1..cb9087d 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -19,7 +19,7 @@ export class RecipeGateway { const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id'); const steps: string[] = []; - let recipes:Recipe[] = [] + let recipes: Recipe[] = [] for (let key in res.rows) { const steps = await this.steps_gw.getForRecipes(Number(key)); @@ -33,7 +33,7 @@ export class RecipeGateway { return recipes } - async getById(id: Number) : Promise{ + async getById(id: number) : Promise{ this.connection.connect() const query = { @@ -47,8 +47,8 @@ export class RecipeGateway { return null } - const steps = await this.steps_gw.getForRecipes(id) - const ingredients = await this.ingredient_gw.findIngredientsForRecipe(id) + const steps = await this.steps_gw.getForRecipes(Number(id)) + const ingredients = await this.ingredient_gw.findIngredientsForRecipe(Number(id)) const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, diff --git a/API-Project/src/gateways/steps.gateway.ts b/API-Project/src/gateways/steps.gateway.ts index 99897ab..ddeae1b 100644 --- a/API-Project/src/gateways/steps.gateway.ts +++ b/API-Project/src/gateways/steps.gateway.ts @@ -8,7 +8,7 @@ export class StepsGateway { } - async getForRecipes(id: Number): Promise { + async getForRecipes(id: number): Promise { this.connection.connect(); const query = { From d5e4259bb1343452d51b511d3924948cae37c4ee Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 15:01:37 +0100 Subject: [PATCH 08/11] working on code smells --- API-Project/src/controllers/ingredients.controller.ts | 1 - API-Project/src/controllers/recipes.controller.ts | 1 - API-Project/src/utils/exception.ts | 9 +++++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/API-Project/src/controllers/ingredients.controller.ts b/API-Project/src/controllers/ingredients.controller.ts index 8889d5e..cf0b31c 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -1,6 +1,5 @@ import { Router } from "express"; import { Exceptions } from "../utils/exception"; -import { Ingredient } from "../types/ingredients"; import { IngredientsGateway } from "../gateways/ingredients.gateway"; diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index 6c08fb1..74639a8 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -1,5 +1,4 @@ import { Router } from "express"; -import { Recipe } from "../types/recipes"; import { Exceptions } from "../utils/exception"; import { RecipeGateway } from "../gateways/recipe.gateway"; diff --git a/API-Project/src/utils/exception.ts b/API-Project/src/utils/exception.ts index 8f9b1f3..05627f5 100644 --- a/API-Project/src/utils/exception.ts +++ b/API-Project/src/utils/exception.ts @@ -14,11 +14,12 @@ class Exception implements ApiException { constructor(readonly error: any, readonly status: number) {} } -/** - * Création d'une 404 - */ -export module Exceptions { + +export namespace Exceptions { + /** + * Création d'une 404 + */ export class NotFoundException extends Exception { constructor(error: any) { super(error, 404) From 78973cfcc99f5515cda92f17c43dcd70e0477ec5 Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 15:02:49 +0100 Subject: [PATCH 09/11] working on code smells --- API-Project/package-lock.json | 9 +++++++++ API-Project/package.json | 1 + API-Project/src/server.ts | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/API-Project/package-lock.json b/API-Project/package-lock.json index 0b56a9f..cebc1eb 100644 --- a/API-Project/package-lock.json +++ b/API-Project/package-lock.json @@ -13,6 +13,7 @@ "@types/morgan": "^1.9.9", "dotenv": "^16.3.1", "express": "^4.18.2", + "helmet": "^7.1.0", "morgan": "^1.10.0", "nodemon": "^3.0.1", "pg": "^8.11.3", @@ -2763,6 +2764,14 @@ "node": ">= 0.4" } }, + "node_modules/helmet": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/helmet/-/helmet-7.1.0.tgz", + "integrity": "sha512-g+HZqgfbpXdCkme/Cd/mZkV0aV3BZZZSugecH03kl38m/Kmdx8jKjBikpDj2cr+Iynv4KpYEviojNdTJActJAg==", + "engines": { + "node": ">=16.0.0" + } + }, "node_modules/html-escaper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", diff --git a/API-Project/package.json b/API-Project/package.json index 1e01c17..659fb81 100644 --- a/API-Project/package.json +++ b/API-Project/package.json @@ -16,6 +16,7 @@ "@types/morgan": "^1.9.9", "dotenv": "^16.3.1", "express": "^4.18.2", + "helmet": "^7.1.0", "morgan": "^1.10.0", "nodemon": "^3.0.1", "pg": "^8.11.3", diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 99cb44d..e850f26 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -6,7 +6,9 @@ import { IngredientsController } from "./controllers/ingredients.controller"; import { RecipesController } from "./controllers/recipes.controller"; import { StepsController } from "./controllers/steps.controller"; -const app = express(); +let helmet = require("helmet"); +let app = express(); +app.use(helmet.hidePoweredBy()); app.get('/', (req, res) => { res.send('Hello from express and typescript !'); From d84748f08ed22e78990cdc5b0f01f246ebf74a82 Mon Sep 17 00:00:00 2001 From: Remi REGNAULT Date: Wed, 29 Nov 2023 15:24:31 +0100 Subject: [PATCH 10/11] fix: the app was crashing when multiple connection where required. I replaced pg.Client by pg.Pool, it should enhanced the usability and durability --- API-Project/src/database/connection.ts | 14 +++++-------- .../src/gateways/ingredients.gateway.ts | 20 +++++++++++-------- API-Project/src/gateways/recipe.gateway.ts | 13 ++++++------ API-Project/src/gateways/steps.gateway.ts | 6 ++++-- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/API-Project/src/database/connection.ts b/API-Project/src/database/connection.ts index 2dadde3..7c702d4 100644 --- a/API-Project/src/database/connection.ts +++ b/API-Project/src/database/connection.ts @@ -1,12 +1,11 @@ -import { Client } from "pg" +import { Pool, PoolClient } from "pg" export class Connection { - public client:Client - clientIsConnected:boolean = false + private pool:Pool constructor() { - this.client = new Client({ + this.pool = new Pool({ user: process.env.DB_USERNAME, host: process.env.DB_DBHOST, database: process.env.DB_DBNAME, @@ -15,10 +14,7 @@ export class Connection { }) } - public async connect() { - if (!this.clientIsConnected) { - await this.client.connect() - this.clientIsConnected = true - } + public async getPoolClient() : Promise { + return await this.pool.connect() } } \ 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 6e282d8..22a8e06 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -9,9 +9,11 @@ export class IngredientsGateway { } async getAll() : Promise { - this.connection.connect() + const client = await this.connection.getPoolClient() - const res = await this.connection.client.query('SELECT * FROM Ingredients ORDER BY id') + const res = await client.query('SELECT * FROM Ingredients ORDER BY id') + + client.release() let ingredients:Ingredient[] = [] @@ -24,14 +26,16 @@ export class IngredientsGateway { } async findOneById(id: number) : Promise { - this.connection.connect() + const client = await this.connection.getPoolClient() const query = { text: 'SELECT * FROM Ingredients WHERE id =$1', values: [id], } - const res = await this.connection.client.query(query) + const res = await client.query(query) + + client.release() if (res.rowCount != 1) { return null @@ -43,15 +47,16 @@ export class IngredientsGateway { } async findIngredientsForRecipe(id: number): Promise { - this.connection.connect(); + const client = await this.connection.getPoolClient() 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) + const res = await client.query(query); + + client.release() if (res.rowCount === 0) { return null; @@ -62,7 +67,6 @@ export class IngredientsGateway { name: row.name, id: Number(row.id), // Conversion de l'identifiant en nombre })); - console.log(ingredients); return ingredients as Ingredient[]; } diff --git a/API-Project/src/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts index cb9087d..6e99f61 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -15,8 +15,9 @@ export class RecipeGateway { } async getAll() : Promise { - this.connection.connect() - const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id'); + const client = await this.connection.getPoolClient() + const res = await client.query('SELECT * FROM Recipes ORDER BY id'); + client.release() const steps: string[] = []; let recipes: Recipe[] = [] @@ -28,20 +29,20 @@ export class RecipeGateway { recipes.push(recipe); } - console.log(recipes); - return recipes } async getById(id: number) : Promise{ - this.connection.connect() + const client = await this.connection.getPoolClient() const query = { text: 'SELECT * FROM Recipes WHERE id =$1', values: [id], } - const res = await this.connection.client.query(query) + const res = await client.query(query) + + client.release() if (res.rowCount != 1) { return null diff --git a/API-Project/src/gateways/steps.gateway.ts b/API-Project/src/gateways/steps.gateway.ts index ddeae1b..36a065b 100644 --- a/API-Project/src/gateways/steps.gateway.ts +++ b/API-Project/src/gateways/steps.gateway.ts @@ -9,14 +9,16 @@ export class StepsGateway { async getForRecipes(id: number): Promise { - this.connection.connect(); + const client = await this.connection.getPoolClient() const query = { text: 'SELECT action FROM Steps WHERE idRecipe = $1 ORDER BY numstep', values: [id], }; - const res = await this.connection.client.query(query); + const res = await client.query(query); + + client.release() const steps = res.rows.map(row => row.action); From f9adc7d012d7d4f09ee6f5c9dc576b7f8906c05b Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Thu, 30 Nov 2023 09:08:05 +0100 Subject: [PATCH 11/11] fix some bugs --- API-Project/package-lock.json | 2 +- API-Project/src/gateways/ingredients.gateway.ts | 14 ++++++++------ API-Project/src/server.ts | 4 +++- API-Project/src/services | 0 4 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 API-Project/src/services diff --git a/API-Project/package-lock.json b/API-Project/package-lock.json index 90b4735..ffc5c3d 100644 --- a/API-Project/package-lock.json +++ b/API-Project/package-lock.json @@ -11,8 +11,8 @@ "dependencies": { "@types/express": "^4.17.21", "@types/morgan": "^1.9.9", - "dotenv": "^16.3.1", "cors": "^2.8.5", + "dotenv": "^16.3.1", "express": "^4.18.2", "helmet": "^7.1.0", "morgan": "^1.10.0", diff --git a/API-Project/src/gateways/ingredients.gateway.ts b/API-Project/src/gateways/ingredients.gateway.ts index e7bb812..76c6ba6 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -71,15 +71,16 @@ export class IngredientsGateway { } async getByLetter(letter: string): Promise { - this.connection.connect(); + const client = await this.connection.getPoolClient() const query = { text: 'SELECT * FROM Ingredients i WHERE LOWER(SUBSTRING(i.name, 1, 1)) = $1', values: [letter.toLowerCase()], }; - const res = await this.connection.client.query(query); - console.log(res) + const res = await client.query(query); + + client.release() if (res.rowCount === 0) { return null; @@ -96,15 +97,16 @@ export class IngredientsGateway { } async filter(prompt: string): Promise { - this.connection.connect(); + const client = await this.connection.getPoolClient() const query = { text: 'SELECT * FROM Ingredients WHERE LOWER(name) LIKE $1', values: [`%${prompt.toLowerCase()}%`], }; - const res = await this.connection.client.query(query); - console.log(res) + const res = await client.query(query); + + client.release() if (res.rowCount === 0) { return null; diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 7b3451f..69ca29e 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -1,10 +1,12 @@ -import express from "express"; require('dotenv').config(); +import express from "express"; import cors from "cors"; import { IngredientsController } from "./controllers/ingredients.controller"; import { RecipesController } from "./controllers/recipes.controller"; import { StepsController } from "./controllers/steps.controller"; + + let helmet = require("helmet"); let app = express(); app.use(helmet.hidePoweredBy()); diff --git a/API-Project/src/services b/API-Project/src/services deleted file mode 100644 index e69de29..0000000