mirror of
https://git.roussel.pro/telecom-paris/pact.git
synced 2026-02-09 10:30:17 +01:00
ajout endpoint POST pour ajout avis
This commit is contained in:
14
code/server/exemple_utilisation.py
Normal file
14
code/server/exemple_utilisation.py
Normal 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)
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
import * as dotenv from 'dotenv';
|
import * as dotenv from 'dotenv';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { addReviewFromRequest } from './review';
|
import bodyParser from 'body-parser';
|
||||||
|
import { addReviewFromRequest } from './review.js';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
|
app.use(bodyParser.urlencoded({extended:true}))
|
||||||
dotenv.config()
|
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, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
console.log("Server démaré sur le port " + process.env.PORT)
|
console.log("Server démaré sur le port " + process.env.PORT)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.post('/add_review', addReviewFromRequest);
|
|
||||||
1
code/server/package-lock.json
generated
1
code/server/package-lock.json
generated
@@ -9,6 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.20.1",
|
||||||
"dotenv": "^16.0.3",
|
"dotenv": "^16.0.3",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"mysql2": "^2.3.3"
|
"mysql2": "^2.3.3"
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"author": "Telereview",
|
"author": "Telereview",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.20.1",
|
||||||
"dotenv": "^16.0.3",
|
"dotenv": "^16.0.3",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"mysql2": "^2.3.3"
|
"mysql2": "^2.3.3"
|
||||||
|
|||||||
@@ -37,9 +37,19 @@ const addReview = (review, authorId, sourceId) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const addReviewFromRequest = async (req,res) => {
|
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 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 authorId = await addAuteur(author);
|
||||||
let sourceId = await getSourceId(review.source);
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
const validSources = ["borne", "website"];
|
const validSources = ["borne", "website"];
|
||||||
const validSexes = ["h","f"];
|
const validSexes = ["h","f","a"];
|
||||||
|
|
||||||
export class Review {
|
export class Review {
|
||||||
constructor(auteur, note, source, commentaire=null, notesAutre={}) {
|
constructor(auteur, note, source, commentaire=null, notesAutre={}) {
|
||||||
@@ -14,7 +14,7 @@ export class Review {
|
|||||||
}
|
}
|
||||||
for(let nom in notesAutre) {
|
for(let nom in notesAutre) {
|
||||||
if(notesAutre[nom] < 0 || notesAutre[nom] > 10) {
|
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)) {
|
if(!validSources.includes(source)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user