ajout des structures de données + connexion BDD

This commit is contained in:
2022-12-23 21:32:52 +01:00
parent 8a4f9d91fc
commit 9da7b5f675
7 changed files with 1301 additions and 1 deletions

3
.gitignore vendored
View File

@@ -17,3 +17,6 @@ _book
*.log *.log
.*.swp .*.swp
settings.json settings.json
.env

14
code/server/database.js Normal file
View 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
View 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

File diff suppressed because it is too large Load Diff

16
code/server/package.json Normal file
View 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
View 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
View 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");
}
}
}