From 66d1aaf027c90d175190ee9fa18c82e811dc4c95 Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Tue, 3 Jan 2023 22:28:57 +0100 Subject: [PATCH] =?UTF-8?q?gestion=20plusieurs=20commentaires=20par=201=20?= =?UTF-8?q?utilisateur=20Si=20un=20utilisateur=20post=20plusieurs=20commen?= =?UTF-8?q?taires=20l'utilisateur=20est=20ajout=C3=A9=20qu'une=20fois=20a?= =?UTF-8?q?=20la=20bdd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/server/borne/structures.js | 3 -- code/server/exemple_utilisation.py | 4 +-- code/server/reseaux_sociaux/post_handler.js | 31 +++++++++++++++++++-- code/server/reseaux_sociaux/structures.js | 4 +-- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/code/server/borne/structures.js b/code/server/borne/structures.js index d9fab3e..c8f7984 100644 --- a/code/server/borne/structures.js +++ b/code/server/borne/structures.js @@ -27,9 +27,6 @@ export class Review { throw new Error("Note " + notesAutre[nom] +"/10 invalide"); } } - if(!(auteur instanceof Auteur)) { - throw new Error("L'auteur est invalide"); - } } } diff --git a/code/server/exemple_utilisation.py b/code/server/exemple_utilisation.py index e5a0e4c..41ef34f 100644 --- a/code/server/exemple_utilisation.py +++ b/code/server/exemple_utilisation.py @@ -10,7 +10,7 @@ avis = { "commentaire": "Commentaire" } -res = requests.post("http://localhost:8080/add_review", data=avis) +# res = requests.post("http://localhost:8080/add_review", data=avis) # print(res.text) #Exemple ajout d'un commentaire trouvé sur les réseaux sociaux @@ -25,5 +25,5 @@ avis = { "auteur_lien": "https://instagram.com/michel", } -# res = requests.post("http://localhost:8080/add_social_review", data=avis) +res = requests.post("http://localhost:8080/add_social_review", data=avis) print(res.text) \ No newline at end of file diff --git a/code/server/reseaux_sociaux/post_handler.js b/code/server/reseaux_sociaux/post_handler.js index a6975da..79fb441 100644 --- a/code/server/reseaux_sociaux/post_handler.js +++ b/code/server/reseaux_sociaux/post_handler.js @@ -2,6 +2,29 @@ import { ReseauxAuteur, ReseauxReview } from './structures.js'; import conn from '../database.js'; import {getSourceId} from '../utils.js'; +/**Récupérer l'id d'un auteur particulier dans la base de donnée s'il existe + * @param {ReseauxAuteur} author L'auteur dont on veut l'id* + * @returns Une Promise qui donne l'id de l'utilisateur ou null si il n'existe pas + */ +const getAuteurId = (author) => { + return new Promise(async (resolve,reject) => { + const sql = "SELECT id FROM reseaux_sociaux_auteurs WHERE nom_utilisateur = ? AND source_id = ? AND lien = ?;" + let sourceId = await getSourceId(author.source); + let query = conn.query(sql, [author.nom, sourceId, author.lien], (err, res) => { + if(err) { + reject(new Error(err.message)) + }else { + if(res.length > 0) { + resolve(res[0].id); + }else { + resolve(null); + } + } + }) + }) +} + + /** * ajoute un auteur de commentaire sur les réseaux sociaux a la bdd * @param {ReseauxAuteur} author @@ -48,10 +71,12 @@ const addReview = (review, authorId, sourceId) => { export const addSocialReviewFromRequest = async (req,res) => { try { const author = new ReseauxAuteur(req.body.auteur_nom, req.body.source, req.body.auteur_lien); - const review = new ReseauxReview(author,req.body.note, req.body.source, req.body.date, req.body.commentaire, req.body.lien) + const review = new ReseauxReview(author, req.body.source, req.body.date, req.body.note, req.body.commentaire, req.body.lien) let sourceId = await getSourceId(review.source); - let authorId = await addAuteur(author, sourceId); - console.log(review.note) + let authorId = await getAuteurId(author); + if(authorId == null) { + authorId = await addAuteur(author, sourceId); + } await addReview(review, authorId, sourceId); res.send("success") }catch(err) { diff --git a/code/server/reseaux_sociaux/structures.js b/code/server/reseaux_sociaux/structures.js index adcbbb0..b2761ac 100644 --- a/code/server/reseaux_sociaux/structures.js +++ b/code/server/reseaux_sociaux/structures.js @@ -14,7 +14,7 @@ export class ReseauxReview extends Review{ super(auteur,note,source,commentaire); this.lien = lien; this.date = date; - if(!(lien instanceof String && lien != null)) { + if((typeof lien !== "string" && lien != null)) { throw new Error("Lien invalide"); } } @@ -32,7 +32,7 @@ export class ReseauxAuteur { this.source = source; this.lien = lien; - if(!(this.nom instanceof String) || !(this.source instanceof String) || !(this.lien instanceof String)) { + if((typeof this.nom !== "string") || (typeof this.source !== "string")){ throw new Error("Auteur invalide"); } }