'use strict' const fs = require('fs') const path = require('path') //task object constructor var Nouvelle = function(nouvelle) { this.Id_nouvelle = nouvelle.Id_nouvelle; this.Nom_nouvelle = nouvelle.Nom_nouvelle; this.Description = nouvelle.Description; this.Date_generation = nouvelle.Date_generation; }; Nouvelle.postNouvelle = function postNouvelle(newsToAdd, res) { let nouvellePath = path.join(__dirname, '../../data/nouvelles.json') fs.readFile(nouvellePath, (err, data) => { if (err) { res.sendStatus(500) } let nouvelles = JSON.parse(data) let findeNews = nouvelles.find((obj) => obj.Id_nouvelle === newsToAdd.Id_nouvelle) if (!findeNews) { //data[newsToAdd] = JSON.parse(data) fs.writeFile(nouvellePath, JSON.stringify(newsToAdd,null, 4), (error) => { if(error){ res.sendStatus(500) console.log('news added') } }) res.sendStatus(201) } } ) } Nouvelle.getNouvelle = function getNouvelle(Id_nouvelle, callback) { let nouvellePath = path.join(__dirname, '../../data/nouvelles.json') //à modifietr il faut chercher à definir une variable à portée global poura être utilisée dant tous les fichiers fs.readFile(nouvellePath, (err, data) => { if (err) { callback(err) return } let nouvelles = JSON.parse(data) let findeNews = nouvelles.find((obj) => obj.Id_nouvelle === Id_nouvelle) if (findeNews) { console.log(findeNews) callback(null, findeNews) } }) } module.exports = Nouvelle