gestion plusieurs commentaires par 1 utilisateur

Si un utilisateur post plusieurs commentaires l'utilisateur est ajouté qu'une fois a la bdd
This commit is contained in:
2023-01-03 22:28:57 +01:00
parent e94bf213c2
commit 66d1aaf027
4 changed files with 32 additions and 10 deletions

View File

@@ -27,9 +27,6 @@ export class Review {
throw new Error("Note " + notesAutre[nom] +"/10 invalide"); throw new Error("Note " + notesAutre[nom] +"/10 invalide");
} }
} }
if(!(auteur instanceof Auteur)) {
throw new Error("L'auteur est invalide");
}
} }
} }

View File

@@ -10,7 +10,7 @@ avis = {
"commentaire": "Commentaire" "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) # print(res.text)
#Exemple ajout d'un commentaire trouvé sur les réseaux sociaux #Exemple ajout d'un commentaire trouvé sur les réseaux sociaux
@@ -25,5 +25,5 @@ avis = {
"auteur_lien": "https://instagram.com/michel", "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) print(res.text)

View File

@@ -2,6 +2,29 @@ import { ReseauxAuteur, ReseauxReview } from './structures.js';
import conn from '../database.js'; import conn from '../database.js';
import {getSourceId} from '../utils.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 * ajoute un auteur de commentaire sur les réseaux sociaux a la bdd
* @param {ReseauxAuteur} author * @param {ReseauxAuteur} author
@@ -48,10 +71,12 @@ const addReview = (review, authorId, sourceId) => {
export const addSocialReviewFromRequest = async (req,res) => { export const addSocialReviewFromRequest = async (req,res) => {
try { try {
const author = new ReseauxAuteur(req.body.auteur_nom, req.body.source, req.body.auteur_lien); 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 sourceId = await getSourceId(review.source);
let authorId = await addAuteur(author, sourceId); let authorId = await getAuteurId(author);
console.log(review.note) if(authorId == null) {
authorId = await addAuteur(author, sourceId);
}
await addReview(review, authorId, sourceId); await addReview(review, authorId, sourceId);
res.send("success") res.send("success")
}catch(err) { }catch(err) {

View File

@@ -14,7 +14,7 @@ export class ReseauxReview extends Review{
super(auteur,note,source,commentaire); super(auteur,note,source,commentaire);
this.lien = lien; this.lien = lien;
this.date = date; this.date = date;
if(!(lien instanceof String && lien != null)) { if((typeof lien !== "string" && lien != null)) {
throw new Error("Lien invalide"); throw new Error("Lien invalide");
} }
} }
@@ -32,7 +32,7 @@ export class ReseauxAuteur {
this.source = source; this.source = source;
this.lien = lien; 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"); throw new Error("Auteur invalide");
} }
} }