diff --git a/graphql/resolvers/index.js b/graphql/resolvers/index.js index eddc4bf4b7abaea19410536c80fe8702f2b19ecf..360e808947ead1d44d11d6eec74046b0c06c06ab 100644 --- a/graphql/resolvers/index.js +++ b/graphql/resolvers/index.js @@ -4,6 +4,7 @@ const Reponse = require("../../models/reponse"); const reponse = require("../../service/ReponseService"); const Question = require("../../models/question"); const question = require("../../service/QuestionService"); +const Utilisateur = require("../../models/utilisateur"); const { ObjectId: ObjectID } = require("mongodb"); @@ -105,187 +106,207 @@ module.exports = { }, questions: async() => { - try { - const questions = await Question.find() - .populate({ - path: "utilisateur", - }) - .populate({ - path: "reponses", - populate: { - path: "utilisateur", - }, - }); - return questions; - } catch (error) { - throw error; - } + + const questions = await Question.find() + .populate({ + path: 'utilisateur' + }) + .populate({ + path : 'reponses', + populate : { + path : 'utilisateur' + } + + }); + return questions + }, - createQuestion: async(args) => { - try { - const argQuestion = args.question; - question.addQuestion( - argQuestion.body, - argQuestion.idutilisateur, - argQuestion.idAnnonce, - argQuestion.question - ); - return "La question a bien été créée."; - } catch (error) { - throw error; + createQuestion: async args => { + + const argQuestion = args.question; + try + { + new ObjectID(argQuestion.idAnnonce) + const data = await Annonce.findById(new ObjectID(argQuestion.idAnnonce)) + if(data === null) + return "L'annonce n'existe pas" + } + catch (e){ + return "L'annonce n'existe pas" + } + try + { + new ObjectID(argQuestion.idUtilisateur) + const data = await Utilisateur.findById(new ObjectID(argQuestion.idUtilisateur)) + if(data === null) + return "L'utilisateur n'existe pas" + } + catch (e){ + + return "L'utilisateur n'existe pas" } + question.addQuestion(argQuestion.body, argQuestion.idUtilisateur, argQuestion.idAnnonce, argQuestion.question); + return "La question a bien été créée." }, - updateQuestion: async(args) => { - try { - const questionId = args.idQuestion; - const laQuestion = args.question; - const res = await new Promise(function(resolve, reject) { - Question.findByIdAndUpdate( - questionId, { question: laQuestion }, { useFindAndModify: false } - ) - .then((data) => { - resolve(data); - }) - .catch((err) => { - const error = { error: err.message }; - resolve(error); - }); + updateQuestion: async args => { + const questionId = args.idQuestion; + const laQuestion = args.question; + const res = await new Promise(function (resolve, reject) { + Question.findByIdAndUpdate(questionId, {question:laQuestion}, {useFindAndModify: false}) + .then(data => { + resolve(data); + }) + .catch(err => { + const error = {error: err.message}; + resolve(error); }); + }); - if (res === null || "error" in res) - return "La question " + questionId + " n'existe pas"; + if (res === null || ("error" in res)) + return "La question " + questionId + " n'existe pas"; + + return "La Question a bien été modifiée"; - return "La Question a bien été modifiée"; - } catch (error) { - throw error; - } }, + deleteQuestion: async args => { - deleteQuestion: async(args) => { - try { - const idQuestion = args.idQuestion; + const idQuestion = args.idQuestion; - // Supprime la question dans l'annonce - const idAnnonce = await Annonce.find({ questions: new ObjectID(idQuestion) }, { _id: 1 }); - await Annonce.findByIdAndUpdate(idAnnonce, { - $pull: { questions: new ObjectID(idQuestion) }, - }); + const res = await new Promise(function (resolve, reject) { + Question.findByIdAndDelete(idQuestion) + .then(async data => { + // Supprime la question dans l'annonce + const idAnnonce = await Annonce.find({questions: new ObjectID(idQuestion)}, {_id: 1}); + await Annonce.findByIdAndUpdate(idAnnonce, {'$pull': {'questions': new ObjectID(idQuestion)}}); - // Supprime les réponses à la question que l'on veut supprimer - const getReponses = await Question.find({ _id: new ObjectID(idQuestion) }, { reponses: 1 }); - if (getReponses.length > 0) - getReponses[0].reponses.forEach( - async(element) => - await Reponse.findByIdAndRemove(new ObjectID(element)) - ); - - const res = await new Promise(function(resolve, reject) { - Question.findByIdAndRemove(idQuestion) - .then((data) => { - resolve(data); - }) - .catch((err) => { - const error = { error: err.message }; - resolve(error); - }); + // Supprime les réponses à la question que l'on veut supprimer + const getReponses = await Question.find({_id: new ObjectID(idQuestion)}, {"reponses": 1}); + if (getReponses.length > 0) + getReponses[0].reponses.forEach(async element => await Reponse.findByIdAndRemove(new ObjectID(element))) + + resolve(data); + }) + .catch(err => { + + const error = {error: err.message}; + resolve(error); }); + }); + if (res === null || ("error" in res)) + return "La question n'existe pas"; + + return "La Question a bien été supprimée"; - if (res === null || "error" in res) - return "La question " + idQuestion + " n'existe pas"; - return "La Question a bien été supprimée"; - } catch (error) { - throw error; - } }, - reponses: async() => { - try { - const reponses = Reponse.find().populate({ - path: "utilisateur", - }); + reponses: async () => { + + const reponses = Reponse.find() + .populate({ + path: "utilisateur" + }); + + return reponses; - return reponses; - } catch (error) { - throw error; - } }, - searchReponse: async(args) => { - try { - const reponses = Reponse.find(args.reponse).populate({ - path: "utilisateur", - }); + searchReponse: async args => { - return reponses; - } catch (error) { - throw error; + try { + if("_id" in args.reponse) + new ObjectID(args.reponse._id) + }catch (e){ throw "la réponse n'existe pas" } + try + { + if("utilisateur" in args.reponse) + new ObjectID(args.reponse.utilisateur) } + catch (e){throw "l'utilisateur n'existe pas" } + + + const reponses = Reponse.find(args.reponse) + .populate({ + path: "utilisateur" + }); + + return reponses; + }, - createReponse: async(args) => { - try { - const questionId = args.questionId; - const utilisateurId = args.utilisateurId; - const laReponse = args.reponse; - - reponse.addReponse("", utilisateurId, questionId, laReponse); - return "La réponse a bien été créée"; - } catch (error) { - throw error; + createReponse: async args => { + const questionId = args.questionId; + const utilisateurId = args.utilisateurId; + const laReponse = args.reponse; + try + { + new ObjectID(utilisateurId) + const data = await Utilisateur.findById(new ObjectID(utilisateurId)) + if(data === null) + return "L'utilisateur n'existe pas" + } + catch (e){ + return "L'utilisateur n'existe pas" + } + try + { + new ObjectID(questionId) + const data = await Question.findById(new ObjectID(questionId)) + if(data === null) + return "La question n'existe pas" + } + catch (e){ + return "La question n'existe pas" } + + reponse.addReponse("", utilisateurId, questionId, laReponse); + return "La réponse a bien été créée"; + }, - updateReponse: async(args) => { - try { - const reponseId = args.reponseId; - const laReponse = args.reponse; - - const res = await new Promise(function(resolve, reject) { - Reponse.findByIdAndUpdate( - reponseId, { reponse: laReponse }, { useFindAndModify: false } - ) - .then((data) => { - resolve(data); - }) - .catch((err) => { - const error = { error: err.message }; - resolve(error); - }); + updateReponse: async args => { + const reponseId = args.reponseId; + const laReponse = args.reponse; + + const res = await new Promise(function(resolve, reject) { + Reponse.findByIdAndUpdate(reponseId, {reponse: laReponse}, { useFindAndModify: false }) + .then(data =>{ + resolve(data); + }) + .catch(err => { + const error = {error : err.message}; + resolve(error); }); + }); - if (res === null || "error" in res) return "La réponse n'existe pas"; + if(res === null || ("error" in res)) + return "La réponse n'existe pas"; + + return "La réponse a bien été modifiée"; - return "La réponse a bien été modifiée"; - } catch (error) { - throw error; - } }, - deleteReponse: async(args) => { - try { - const reponseId = args.reponseId; - - const res = await new Promise(function(resolve, reject) { - Reponse.findByIdAndDelete(reponseId) - .then((data) => { - Question.findOneAndUpdate({ reponses: reponseId }, { $pull: { reponses: new ObjectID(reponseId) } }).then(); - - resolve(data); - }) - .catch((err) => { - const error = { error: err.message }; - resolve(error); - }); + deleteReponse: async args => { + const reponseId = args.reponseId; + + const res = await new Promise(function(resolve, reject) { + Reponse.findByIdAndDelete(reponseId) + .then(data => { + Question.findOneAndUpdate({reponses: reponseId}, {$pull: {reponses: new ObjectID(reponseId)}}).then() + resolve(data); + }) + .catch(err => { + const error = {error : err.message}; + resolve(error); }); + }); - if (res === null || "error" in res) return "La réponse n'existe pas"; + if(res === null || ("error" in res)) + return "La réponse n'existe pas"; - return "La réponse a bien été supprimée"; - } catch (error) { - throw error; - } - }, + return "La réponse a bien été supprimée"; + + } }; \ No newline at end of file