feat: add route to get rating list
continuous-integration/drone/push Build is failing Details

WORK-RRE
Rémi REGNAULT 1 year ago
parent c67ea5fac5
commit cc96e541b2

@ -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 } export { RecipesController }

@ -106,9 +106,30 @@ export class RecipeGateway {
if (res.rows != null && res.rows.length >= 1 && res.rows[0] != null) { if (res.rows != null && res.rows.length >= 1 && res.rows[0] != null) {
const dictionnary_as_str: string = res.rows[0].comments_dico.replace(/'/g, '"') const dictionnary_as_str: string = res.rows[0].comments_dico.replace(/'/g, '"')
comments_dictionary = JSON.parse(dictionnary_as_str); comments_dictionary = JSON.parse(dictionnary_as_str);
console.log(comments_dictionary)
} }
return comments_dictionary return comments_dictionary
} }
async getRatingList(id: number): Promise<number[]> {
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
}
} }
Loading…
Cancel
Save