From e79f26815056ef399b35c7e42e766b17d60f1a01 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Wed, 16 Nov 2022 22:14:38 +0100 Subject: [PATCH 1/6] =?UTF-8?q?Ajout=20de=20la=20query=20reponses(fonction?= =?UTF-8?q?=20qui=20permet=20d'afficher=20toutes=20les=20r=C3=A9ponses=20d?= =?UTF-8?q?e=20la=20bdd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphql/resolvers/annonce.js | 14 ++++++++++++++ graphql/schema/annonce.js | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index f7f064e..c788ca4 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -1,5 +1,6 @@ const Annonce = require("../../models/annonce"); const annonce = require("../../service/AnnonceService"); +const Reponse = require("../../models/reponse"); module.exports = { annonces: async () => { @@ -55,4 +56,17 @@ module.exports = { throw error } }, + + reponses: async () => { + try { + const reponses = Reponse.find() + .populate({ + path: "utilisateur" + }); + + return reponses; + } catch (error) { + throw error + } + }, } \ No newline at end of file diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index c6f9e4c..16ee529 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -46,7 +46,7 @@ module.exports = buildSchema(` photos: [String] } - input AnnonceUpdateInput { + input AnnonceUpdateInput { titre: String typeBien: String statusPublication: String @@ -59,6 +59,7 @@ module.exports = buildSchema(` type Query { annonces:[Annonce!] + reponses:[Reponse!] } type Mutation { -- GitLab From 25b07852a9708c7333bc8681262ca06005aefb06 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Wed, 16 Nov 2022 22:33:37 +0100 Subject: [PATCH 2/6] ajout de la mutation createReponse --- graphql/resolvers/annonce.js | 14 ++++++++++++++ graphql/schema/annonce.js | 1 + 2 files changed, 15 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index c788ca4..eb2eaa7 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -1,6 +1,7 @@ const Annonce = require("../../models/annonce"); const annonce = require("../../service/AnnonceService"); const Reponse = require("../../models/reponse"); +const reponse = require("../../service/ReponseService"); module.exports = { annonces: async () => { @@ -69,4 +70,17 @@ module.exports = { throw error } }, + + 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 + } + }, } \ No newline at end of file diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index 16ee529..959e26a 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -66,6 +66,7 @@ module.exports = buildSchema(` createAnnonce(annonce: AnnonceInput): String updateAnnonce(idAnnonce: ID!,annonce:AnnonceUpdateInput): Annonce deleteAnnonce(annonceId: ID!): String + createReponse(utilisateurId: ID!, questionId: ID!, reponse: String!): String } schema { -- GitLab From c89cc5d69cfcb27b596dfeb46c56a5d25272f280 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Thu, 17 Nov 2022 11:15:12 +0100 Subject: [PATCH 3/6] ajout de la mutation updateReponse --- graphql/resolvers/annonce.js | 26 ++++++++++++++++++++++++++ graphql/schema/annonce.js | 1 + 2 files changed, 27 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index eb2eaa7..11e88fb 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -83,4 +83,30 @@ module.exports = { throw error } }, + + 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); + }); + }); + + if(res === null || ("error" in res)) + return "La réponse " + reponseId + " n'existe pas"; + + return "La réponse a bien été modifiée"; + + } catch (error) { + throw error + } + }, } \ No newline at end of file diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index 959e26a..6fbd510 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -67,6 +67,7 @@ module.exports = buildSchema(` updateAnnonce(idAnnonce: ID!,annonce:AnnonceUpdateInput): Annonce deleteAnnonce(annonceId: ID!): String createReponse(utilisateurId: ID!, questionId: ID!, reponse: String!): String + updateReponse(reponseId: ID!, reponse: String!): String } schema { -- GitLab From 9651501050c06ab5e833247d61996b6bf66cf40a Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Thu, 17 Nov 2022 11:34:34 +0100 Subject: [PATCH 4/6] ajout de la mutation deleteReponse --- graphql/resolvers/annonce.js | 27 +++++++++++++++++++++++++++ graphql/schema/annonce.js | 1 + 2 files changed, 28 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index 11e88fb..929cba2 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -109,4 +109,31 @@ module.exports = { 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); + }); + }); + + if(res === null || ("error" in res)) + return "La réponse " + reponseId + " n'existe pas"; + + return "La réponse a bien été supprimée"; + + } catch (error) { + throw error + } + } } \ No newline at end of file diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index 6fbd510..7783668 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -68,6 +68,7 @@ module.exports = buildSchema(` deleteAnnonce(annonceId: ID!): String createReponse(utilisateurId: ID!, questionId: ID!, reponse: String!): String updateReponse(reponseId: ID!, reponse: String!): String + deleteReponse(reponseId: ID!): String } schema { -- GitLab From c5046be8343b7f07c4fab53162f657052e1969a5 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Thu, 17 Nov 2022 11:43:12 +0100 Subject: [PATCH 5/6] ajout de la query searchReponse / input SearchReponseInput --- graphql/resolvers/annonce.js | 13 +++++++++++++ graphql/schema/annonce.js | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index 929cba2..5b30a24 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -71,6 +71,19 @@ module.exports = { } }, + searchReponse: async args => { + try { + const reponses = Reponse.find(args.reponse) + .populate({ + path: "utilisateur" + }); + + return reponses; + } catch (error) { + throw error + } + }, + createReponse: async args => { try { const questionId = args.questionId; diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index 7783668..dd86c2b 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -57,9 +57,15 @@ module.exports = buildSchema(` photos: [String] } + input SearchReponseInput { + _id: ID + utilisateur: ID + } + type Query { annonces:[Annonce!] reponses:[Reponse!] + searchReponse(reponse:SearchReponseInput): [Reponse!] } type Mutation { -- GitLab From 4fbf2dc7d5796dd8c6a1746b3f2212dce47395c7 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Thu, 17 Nov 2022 11:53:05 +0100 Subject: [PATCH 6/6] ajout de la query searchAnnonce / input SearchAnnonceInput --- graphql/resolvers/annonce.js | 23 +++++++++++++++++++++++ graphql/schema/annonce.js | 12 ++++++++++++ 2 files changed, 35 insertions(+) diff --git a/graphql/resolvers/annonce.js b/graphql/resolvers/annonce.js index 5b30a24..6117217 100644 --- a/graphql/resolvers/annonce.js +++ b/graphql/resolvers/annonce.js @@ -26,6 +26,29 @@ module.exports = { throw error } }, + + searchAnnonces: async args => { + try { + const annonces = await Annonce.find(args.annonce) + .populate({ + path : 'questions', + populate : { + path : 'utilisateur' + }}) + .populate({ + path : 'questions', + populate : { + path : 'reponses', + populate : { + path : 'utilisateur' + } + } + }); + return annonces; + } catch (error) { + throw error + } + }, createAnnonce: async args => { try { diff --git a/graphql/schema/annonce.js b/graphql/schema/annonce.js index dd86c2b..619760d 100644 --- a/graphql/schema/annonce.js +++ b/graphql/schema/annonce.js @@ -57,6 +57,17 @@ module.exports = buildSchema(` photos: [String] } + input SearchAnnonceInput { + _id: ID + titre: String + typeBien: String + statusPublication: String + statusBien: String + description: String + prixBien: Int + dateDisponibilite: String + } + input SearchReponseInput { _id: ID utilisateur: ID @@ -64,6 +75,7 @@ module.exports = buildSchema(` type Query { annonces:[Annonce!] + searchAnnonces(annonce:SearchAnnonceInput): [Annonce!] reponses:[Reponse!] searchReponse(reponse:SearchReponseInput): [Reponse!] } -- GitLab