ajout endpoint POST pour ajout avis

This commit is contained in:
2022-12-24 16:11:40 +01:00
parent 70396e0dc1
commit beb7e26ce7
6 changed files with 38 additions and 11 deletions

View File

@@ -0,0 +1,14 @@
import requests
avis = {
"auteur_age": 20,
"auteur_sexe": 'f',
"note": 8,
"source": "borne",
#Optionel
"notes_autre": '{"service":8,"accueil":30}',
"commentaire": "Commentaire"
}
res = requests.post("http://localhost:8080/add_review", data=avis)
print(res.text)

View File

@@ -1,15 +1,16 @@
import * as dotenv from 'dotenv';
import express from 'express';
import { addReviewFromRequest } from './review';
import bodyParser from 'body-parser';
import { addReviewFromRequest } from './review.js';
const app = express();
const router = express.Router()
app.use(bodyParser.urlencoded({extended:true}))
dotenv.config()
app.post('/add_review', (req,res) => addReviewFromRequest(req,res));
// app.post('/add_review', (req, res) => {console.log(req.body);res.send("success")})
app.use(express.json)
app.listen(process.env.PORT, () => {
console.log("Server démaré sur le port " + process.env.PORT)
})
router.post('/add_review', addReviewFromRequest);

View File

@@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mysql2": "^2.3.3"

View File

@@ -10,6 +10,7 @@
"author": "Telereview",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mysql2": "^2.3.3"

View File

@@ -37,9 +37,19 @@ const addReview = (review, authorId, sourceId) => {
}
export const addReviewFromRequest = async (req,res) => {
try {
let notes_autre = {}
try{
notes_autre = JSON.parse(req.body.notes_autre);
}catch(err){};
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)
const review = new Review(author, req.body.note, req.body.source, req.body.commentaire, notes_autre)
let authorId = await addAuteur(author);
let sourceId = await getSourceId(review.source);
await addReview(review, authorId, sourceId )
await addReview(review, authorId, sourceId );
res.send("success")
}catch(err) {
res.status(500).send("Error : " + err.message)
}
}

View File

@@ -1,5 +1,5 @@
const validSources = ["borne", "website"];
const validSexes = ["h","f"];
const validSexes = ["h","f","a"];
export class Review {
constructor(auteur, note, source, commentaire=null, notesAutre={}) {
@@ -14,7 +14,7 @@ export class Review {
}
for(let nom in notesAutre) {
if(notesAutre[nom] < 0 || notesAutre[nom] > 10) {
throw new Error("Note " + rating +"/10 invalide");
throw new Error("Note " + notesAutre[nom] +"/10 invalide");
}
}
if(!validSources.includes(source)) {