mirror of
https://git.roussel.pro/telecom-paris/pact.git
synced 2026-02-09 10:30:17 +01:00
ajout insertion d'avis et auteurs
This commit is contained in:
@@ -1,7 +1,53 @@
|
||||
import { Auteur, Review } from './structures'
|
||||
import { Auteur, Review } from './structures.js';
|
||||
import conn from './database.js';
|
||||
|
||||
export const addReviewFromRequest = (req,res) => {
|
||||
const author = new Auteur(req.body.age,req.body.sexe);
|
||||
const review = new Review(author, req.body.note, req.body.source)
|
||||
//TODO: AJouter l'avis a la bdd
|
||||
}
|
||||
const addAuteur = (author) => {
|
||||
return new Promise((resolve,reject) => {
|
||||
const sql = "INSERT INTO auteurs (age, sexe) VALUES (?);"
|
||||
conn.query(sql, [[author.age, author.sexe]], (err, res) => {
|
||||
if(err) {
|
||||
reject(err)
|
||||
}else {
|
||||
resolve(res.insertId);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const getSourceId = (source) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const sql = "SELECT id from sources WHERE nom = ?";
|
||||
conn.query(sql, [source], (err, res) => {
|
||||
resolve(res[0].id);
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const addReview = (review, authorId, sourceId) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const sql = "INSERT INTO avis_borne (id_auteur, note_principale, notes_autre, commentaire, source_id) VALUES (?);"
|
||||
conn.query(sql, [[authorId, review.note, JSON.stringify(review.notesAutre), review.commentaire, sourceId]], (err, res) => {
|
||||
if(err) {
|
||||
reject(err)
|
||||
}else {
|
||||
resolve(res.insertId);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const addReviewFromRequest = async (req,res) => {
|
||||
const author = new Auteur(req.body.auteur.age,req.body.auteur.sexe);
|
||||
const review = new Review(author, req.body.note, req.body.source, req.body.message, req.body.notes_autre)
|
||||
let authorId = await addAuteur(author);
|
||||
let sourceId = await getSourceId(review.source);
|
||||
await addReview(review, authorId, sourceId )
|
||||
}
|
||||
|
||||
addReviewFromRequest({body:{
|
||||
auteur: {age: 20, sexe:'f'},
|
||||
note: 9,
|
||||
source: "borne",
|
||||
message: "Test message",
|
||||
}})
|
||||
// (async () => {console.log(await getSourceId("website"))})();
|
||||
Reference in New Issue
Block a user