diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index a716874..de57061 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -66,4 +66,26 @@ RecipesController.get('/withingr/:ids', async (req, res) => { } }) +RecipesController.get('/getcommentsdictionary/:id', async (req, res) => { + const id: number = Number(req.params.id) + if (Number.isNaN(id) || !Number.isInteger(id)) { + res.status(400).json('The parameter is not an integer') + } + + try { + const dictionary_comments = await recipes_gw.getCommentsDictionary(id) + + if (dictionary_comments.length == 0) { + res.status(404).json('no data found') + } + else { + res.status(200).json(dictionary_comments) + } + } + 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/gateways/recipe.gateway.ts b/API-Project/src/gateways/recipe.gateway.ts index 68e7a08..2f7a504 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -88,4 +88,27 @@ export class RecipeGateway { return recipes as Recipe[]; } + + async getCommentsDictionary(id: number): Promise<{[word: string]: number}> { + let comments_dictionary: {[word: string]: number} = {} + + const client = await this.connection.getPoolClient() + + const query = { + text: 'SELECT comments_dico FROM Recipes WHERE id=$1', + values: [id] + } + + const res = await client.query(query) + + client.release() + + if (res.rows != null && res.rows.length >= 1 && res.rows[0] != null) { + const dictionnary_as_str: string = res.rows[0].comments_dico.replace(/'/g, '"') + comments_dictionary = JSON.parse(dictionnary_as_str); + console.log(comments_dictionary) + } + + return comments_dictionary + } } \ No newline at end of file diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index 44cf7d2..1eb8916 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -5,8 +5,6 @@ import { IngredientsController } from "./controllers/ingredients.controller"; import { RecipesController } from "./controllers/recipes.controller"; import { StepsController } from "./controllers/steps.controller"; - - let helmet = require("helmet"); const app = express(); app.use(helmet.hidePoweredBy());