From cc96e541b201028fccb98275072647a67fa6b9ec Mon Sep 17 00:00:00 2001 From: Remi R Date: Wed, 6 Dec 2023 15:06:36 +0100 Subject: [PATCH] feat: add route to get rating list --- .../src/controllers/recipes.controller.ts | 22 ++++++++++++++++++ API-Project/src/gateways/recipe.gateway.ts | 23 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/API-Project/src/controllers/recipes.controller.ts b/API-Project/src/controllers/recipes.controller.ts index de57061..b703305 100644 --- a/API-Project/src/controllers/recipes.controller.ts +++ b/API-Project/src/controllers/recipes.controller.ts @@ -88,4 +88,26 @@ RecipesController.get('/getcommentsdictionary/:id', async (req, res) => { } }) +RecipesController.get('/getratinglist/: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 rating_list = await recipes_gw.getRatingList(id) + + if (rating_list.length == 0) { + res.status(404).json('no data found') + } + else { + res.status(200).json(rating_list) + } + } + 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 2f7a504..8c15d96 100644 --- a/API-Project/src/gateways/recipe.gateway.ts +++ b/API-Project/src/gateways/recipe.gateway.ts @@ -106,9 +106,30 @@ export class RecipeGateway { 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 } + + async getRatingList(id: number): Promise { + let rating_list: number[] = [] + + const client = await this.connection.getPoolClient() + + const query = { + text: 'SELECT rating 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 list_as_str: string = res.rows[0].rating + rating_list = JSON.parse(list_as_str); + } + + return rating_list + } } \ No newline at end of file