From 8d8e205f9473bd6522bc2abfea0bdb56a856b200 Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Thu, 30 Nov 2023 08:45:26 +0100 Subject: [PATCH] add some changes --- API-Project/package-lock.json | 23 ++++++++ API-Project/package.json | 8 +-- .../src/controllers/ingredients.controller.ts | 59 +++++++++++++++---- .../src/gateways/ingredients.gateway.ts | 53 ++++++++++++++++- API-Project/src/server.ts | 14 +++-- API-Project/src/services | 0 6 files changed, 138 insertions(+), 19 deletions(-) create mode 100644 API-Project/src/services diff --git a/API-Project/package-lock.json b/API-Project/package-lock.json index 9ff34b9..b2f2f82 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", + "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1", @@ -20,6 +21,7 @@ "typescript": "^5.2.2" }, "devDependencies": { + "@types/cors": "^2.8.17", "@types/jest": "^29.5.8", "@types/pg": "^8.10.9", "jest": "^29.7.0", @@ -1244,6 +1246,15 @@ "@types/node": "*" } }, + "node_modules/@types/cors": { + "version": "2.8.17", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.17.tgz", + "integrity": "sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/express": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", @@ -2100,6 +2111,18 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/create-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz", diff --git a/API-Project/package.json b/API-Project/package.json index 9ed2661..da89284 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", + "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1", @@ -23,11 +24,10 @@ "typescript": "^5.2.2" }, "devDependencies": { - - "@types/pg": "^8.10.9", + "@types/cors": "^2.8.17", "@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..c08ab41 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -2,29 +2,58 @@ 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 { IngredientsGateway } from "../gateways/ingredients.gateway"; - const IngredientsController = Router() const ingredient_gw = new IngredientsGateway() -/** To get all ingredients */ -IngredientsController.get('/', async (req, res) => { + +IngredientsController.get('/filter/:prompt', async (req, res) => { + const letter = req.params.prompt; + + if (!letter) { + throw new Exceptions.BadRequestException('prompt is invalid!'); + } + try { - const ingredients = await ingredient_gw.getAll() + const ingredient = await ingredient_gw.filter(letter); - res.status(200).json(ingredients) + if (ingredient == null) { + res.status(404).send('Not found'); + } else { + res.status(200).json(ingredient); + } } catch (error) { const error_error = error as Error - res.status(500).send(error_error.message) + res.status(500).send(error_error.message); } -}) +}); + +IngredientsController.get('/letter/:letter', async (req, res) => { + const letter = req.params.letter; + + if (!letter) { + throw new Exceptions.BadRequestException('Letter is invalid!'); + } + + try { + const ingredient = await ingredient_gw.getByLetter(letter); + + if (ingredient == null) { + res.status(404).send('Not found'); + } else { + res.status(200).json(ingredient); + } + } catch (error) { + const error_error = error as Error + res.status(500).send(error_error.message); + } +}); /** To get one ingredient by id */ IngredientsController.get('/:id', async (req, res) => { + console const id = Number(req.params.id); if (!Number.isInteger(id)) { @@ -39,7 +68,6 @@ IngredientsController.get('/:id', async (req, res) => { } else { const ingredient_ingredient = ingredient as Ingredient - res.status(200).json(ingredient) } } catch (error) { @@ -48,4 +76,15 @@ IngredientsController.get('/:id', async (req, 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) + } +}) + export { IngredientsController } \ 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 fcdeb63..4856d4e 100644 --- a/API-Project/src/gateways/ingredients.gateway.ts +++ b/API-Project/src/gateways/ingredients.gateway.ts @@ -57,7 +57,6 @@ export class IngredientsGateway { return null; } - const ingredients = res.rows.map(row => ({ name: row.name, id: Number(row.id), // Conversion de l'identifiant en nombre @@ -67,4 +66,56 @@ export class IngredientsGateway { return ingredients as Ingredient[]; } + async getByLetter(letter: string): Promise { + this.connection.connect(); + + 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) + + if (res.rowCount === 0) { + return null; + } + + let ingredients: Ingredient[] = []; + + for (const row of res.rows) { + const ingredient: Ingredient = new Ingredient(Number(row.id), row.name); + ingredients.push(ingredient); + } + + return ingredients; + } + + async filter(prompt: string): Promise { + this.connection.connect(); + + 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) + + if (res.rowCount === 0) { + return null; + } + + let ingredients: Ingredient[] = []; + + for (const row of res.rows) { + const ingredient: Ingredient = new Ingredient(Number(row.id), row.name); + ingredients.push(ingredient); + } + + return ingredients; + } + + + } \ No newline at end of file diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 9da9eb0..08e5f35 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -1,18 +1,24 @@ 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"; const app = express(); +// Configuration du middleware CORS pour autoriser toutes les origines +app.use(cors({ + origin: '*', +})); + app.get('/', (req, res) => { - res.send('Hello from express and typescript !'); + res.json({ message: 'Hello from express and typescript!' }); }); app.use('/ingredients', IngredientsController); app.use('/recipes', RecipesController); -app.use('/steps', StepsController) +app.use('/steps', StepsController); -const port = process.env.PORT || 3000; +const port = process.env.PORT || 3000; -app.listen(port, () => console.log(`App listenning on PORT ${port}`)); \ No newline at end of file +app.listen(port, () => console.log(`App listening on PORT ${port}`)); diff --git a/API-Project/src/services b/API-Project/src/services new file mode 100644 index 0000000..e69de29