From 28ff4a8da7206d84f88aafd1902cee387c91fecc Mon Sep 17 00:00:00 2001 From: by183440 Date: Thu, 17 Nov 2022 11:21:31 +0100 Subject: [PATCH 1/4] Ajout de l'input QuestionInput. Ajout du type Query: questions. Ajout de 3 mutation: - createQuestion - updateQuestion - deleteQuestion --- graphql/schema/annonce.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index c6f9e4c..3cb9c92 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -57,14 +57,26 @@ module.exports = buildSchema(` photos: [String] } + input QuestionInput { + idUtilisateur: ID + idAnnonce : ID + question: String + } type Query { annonces:[Annonce!] + questions:[Question!] + } type Mutation { createAnnonce(annonce: AnnonceInput): String updateAnnonce(idAnnonce: ID!,annonce:AnnonceUpdateInput): Annonce deleteAnnonce(annonceId: ID!): String + + createQuestion(question: QuestionInput): String + updateQuestion(idQuestion: ID!, question: String!):String + deleteQuestion(idQuestion:ID!): String + } schema { -- GitLab From 250fdab5b52145fa7016f60c58ed20d512946cc7 Mon Sep 17 00:00:00 2001 From: by183440 Date: Thu, 17 Nov 2022 11:27:33 +0100 Subject: [PATCH 2/4] ajout de la Query question --- graphql/resolvers/annonce.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index f7f064e..9a9914c 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -1,6 +1,9 @@ const Annonce = require("../../models/annonce"); const annonce = require("../../service/AnnonceService"); +const Question = require("../../models/question") +const question = require("../../service/QuestionService") + module.exports = { annonces: async () => { try { -- GitLab From ce78edf8eec82004d257cc890c7dad9e4f59a1c5 Mon Sep 17 00:00:00 2001 From: by183440 Date: Thu, 17 Nov 2022 11:28:01 +0100 Subject: [PATCH 3/4] ajout de la Query question --- graphql/resolvers/annonce.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index 9a9914c..c0348b2 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -58,4 +58,23 @@ module.exports = { throw error } }, + + questions: async args => { + try { + const questions = await Question.find() + .populate({ + path: 'utilisateur' + }) + .populate({ + path : 'reponses', + populate : { + path : 'utilisateur' + } + + }); + return questions + } catch (error) { + throw error + } + }, } \ No newline at end of file -- GitLab From 10cee23d9b8f47dd3a9b6263d9cf532ed2a7b2ce Mon Sep 17 00:00:00 2001 From: by183440 Date: Thu, 17 Nov 2022 16:38:46 +0100 Subject: [PATCH 4/4] Ajout des fonctions: - createQuestion - updateQuestion - deleteQuestion --- graphql/resolvers/annonce.js | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index c0348b2..4d486de 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -3,6 +3,9 @@ const annonce = require("../../service/AnnonceService"); const Question = require("../../models/question") const question = require("../../service/QuestionService") +const {ObjectId: ObjectID} = require("mongodb"); + +const Reponse = require("../../models/reponse") module.exports = { annonces: async () => { @@ -77,4 +80,76 @@ module.exports = { throw error } }, + + 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 + } + }, + + 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 => { + console.log(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"; + + return "La Question a bien été modifiée"; + + } catch (error) { + throw error + } + }, + deleteQuestion: async args => { + try { + + 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) } } ); + + // 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); + }); + }); + + 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 + } + }, + } \ No newline at end of file -- GitLab