feat: add route to get comments_dictionary

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

@ -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 }

@ -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
}
}

@ -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());

Loading…
Cancel
Save