mirror of
https://git.roussel.pro/telecom-paris/pact.git
synced 2026-02-09 10:30:17 +01:00
ajout des structures de données + connexion BDD
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -17,3 +17,6 @@ _book
|
|||||||
*.log
|
*.log
|
||||||
.*.swp
|
.*.swp
|
||||||
settings.json
|
settings.json
|
||||||
|
|
||||||
|
|
||||||
|
.env
|
||||||
14
code/server/database.js
Normal file
14
code/server/database.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import * as dotenv from 'dotenv'
|
||||||
|
import mysql from 'mysql';
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const conn = mysql.createConnection({
|
||||||
|
host: process.env.DB_HOST,
|
||||||
|
user: process.env.DB_USER,
|
||||||
|
password: process.env.DB_PASSWORD,
|
||||||
|
database: process.env.DB_NAME,
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
export default conn;
|
||||||
15
code/server/main.js
Normal file
15
code/server/main.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import * as dotenv from 'dotenv';
|
||||||
|
import express from 'express';
|
||||||
|
import { addReviewFromRequest } from './review';
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const router = express.Router()
|
||||||
|
|
||||||
|
dotenv.config()
|
||||||
|
|
||||||
|
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);
|
||||||
1204
code/server/package-lock.json
generated
Normal file
1204
code/server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
code/server/package.json
Normal file
16
code/server/package.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "Telereview",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^16.0.3",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"mysql": "^2.18.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
code/server/review.js
Normal file
7
code/server/review.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { Auteur, Review } from './structures'
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
41
code/server/structures.js
Normal file
41
code/server/structures.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
const validSources = ["borne", "website"];
|
||||||
|
const validSexes = ["h","f"];
|
||||||
|
|
||||||
|
export class Review {
|
||||||
|
constructor(author, mainRating, source, message="", otherRatings={}) {
|
||||||
|
this.author = author;
|
||||||
|
this.mainRating = mainRating;
|
||||||
|
this.source = source;
|
||||||
|
this.message = message;
|
||||||
|
this.otherRatings = otherRatings;
|
||||||
|
//On vérifie si toutes les données sont correctes
|
||||||
|
if(mainRating < 0 || mainRating > 10) {
|
||||||
|
throw new Error("Note principale invalide");
|
||||||
|
}
|
||||||
|
for(rating of otherRatings) {
|
||||||
|
if(rating < 0 || rating > 10) {
|
||||||
|
throw new Error("Note " + rating +"/10 invalide");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!validSources.includes(source)) {
|
||||||
|
throw new Error("Source invalide");
|
||||||
|
}
|
||||||
|
if(!author instanceof Auteur) {
|
||||||
|
throw new Error("L'auteur est invalide");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Auteur {
|
||||||
|
constructor(age=undefined, sexe=undefined) {
|
||||||
|
this.age = age;
|
||||||
|
this.sexe = sexe;
|
||||||
|
|
||||||
|
if(sexe != undefined && !validSexes.includes(sexe) ) {
|
||||||
|
throw new Error("Sexe invalide");
|
||||||
|
}
|
||||||
|
if(age != undefined && age <= 0) {
|
||||||
|
throw new Error("Age invalide");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user