diff --git a/backend/src/backend/App.js b/backend/src/backend/App.js index d223acf086d656417235ebd6654211757db96ed4..5deb2862d9365bf665a6c2022a178622251bc95e 100644 --- a/backend/src/backend/App.js +++ b/backend/src/backend/App.js @@ -4,53 +4,21 @@ const db = require('./DatabaseConnection') const app = express() const port = 3001 - - +const cors = require('cors'); app.use(bodyParser.json()); +app.use(cors({ + origin: '*' +})); app.get('/', (req, res) => { - res.send('Hello World!') + res.send('Hello World!') }) -app.post('/liste-horaires', (req, res) => { +app.get('/liste-rdv-magasin', (req, res) => { let date = formatDate(req.body.date); - console.log(date) - db.executeQuery("select Heure from rendez_vous WHERE Date = '"+date+"' AND Id_Adresse = "+req.body.address).then((result) => { - console.log(result); - res.status(200).send(result); - }).catch((err) => { - res.status(500).send(err); - }); -}) - -app.post('/prendre-rdv', (req, res) => { - let date = formatDate(req.body.date); - selectAppointmentWithDate(date,req.body.address, 'Heure, Id_Adresse').then((result) => { - for(let i = 0; i < result.length; i++) { - if(result[i].Heure == req.body.hour && result[i].Id_Adresse == req.body.address) { - res.status(400).send("Horaire déjà pris"); - return; - } - } - - let hour = req.body.hour; - let client = req.body.client; - let address = req.body.address; - db.updateQuery("INSERT INTO Rendez_Vous (Date, Heure, Description, Id_Client, Id_Adresse) VALUES ('" + date + "', STR_TO_DATE('" + hour + "', '%H:%i'), NULL, '" + client + "', '" + address + "')") - .then(() => { - res.status(200).send("Rendez-vous pris"); - }) - .catch((err) => { - console.log(err); - res.status(500).send(err); - }); - }).catch((err) => { - res.status(500).send(err); - }); -}) + let id_address = req.body.id_address; -app.get('/liste-magasins', (req, res) => { - db.executeQuery("SELECT * FROM Adresse") + db.executeQuery("SELECT * FROM Rendez_Vous where Id_Adresse = " + id_address + " and Date = " + date) .then((result) => { console.log(result); res.status(200).send(result); @@ -60,29 +28,71 @@ app.get('/liste-magasins', (req, res) => { }); }) -app.post('/liste-rdv', (req, res) => { - let client_id = req.body.client_id - db.executeQuery("SELECT * FROM Rendez_Vous JOIN adresse on rendez_vous.Id_Adresse = adresse.Id_Adresse WHERE Id_Client = " + client_id + " ORDER BY Date DESC") - .then((result) => { - for (let i = 0; i < result.length; i++) { - console.log(formatDate(result[i].Date)) - result[i].Date = formatDate(result[i].Date); - } - console.log(result); - res.status(200).send(result); - }) - .catch((err) => { - console.log(err); - res.status(500).send(err); - }); +app.listen(port, () => { + console.log(`Example app listening on port ${port}`) +}) + +app.post('/liste-horaires', (req, res) => { + let date = formatDate(req.body.date); + console.log(date) + db.executeQuery("select Heure from rendez_vous WHERE Date = '"+date+"' AND Id_Adresse = "+req.body.address).then((result) => { + console.log(result); + res.status(200).send(result); + }).catch((err) => { + res.status(500).send(err); + }); +}) + +app.get('/prendre-rdv', (req, res) => { + selectAppointmentWithDate(req.body.date, 'Heure').then((result) => { + res.status(200).send(result); + }).catch((err) => { + res.status(500).send(err); + }); +}) + + +app.post('/prendre-rdv', (req, res) => { + selectAppointmentWithDate(req.body.date, 'Heure, Id_Adresse').then((result) => { + for (let i = 0; i < result.length; i++) { + if (result[i].Heure == req.body.hour && result[i].Id_Adresse == req.body.address) { + res.status(400).send("Horaire déjà pris"); + return; + } + } + let date = formatDate(req.body.date); + let hour = req.body.hour; + let client = req.body.client; + let address = req.body.address; + db.updateQuery("INSERT INTO Rendez_Vous (Date, Heure, Description, Id_Client, Id_Adresse) VALUES ('" + date + "', STR_TO_DATE('" + hour + "', '%H:%i'), NULL, '" + client + "', '" + address + "')") + .then(() => { + res.status(200).send("Rendez-vous pris"); + }) + .catch((err) => { + console.log(err); + res.status(500).send(err); + }); + }).catch((err) => { + res.status(500).send(err); + }); +}) + +app.get('/liste-magasins', (req, res) => { + db.executeQuery("SELECT * FROM Adresse") + .then((result) => { + console.log(result); + res.status(200).send(result); + }) + .catch((err) => { + res.status(500).send(err); + }); }) -app.post('/supp-rendez-vous', (req, res) => { +app.delete('/supp-rendez-vous', (req, res) => { let date = formatDate(req.body.date); db.executeQuery("SELECT * FROM rendez_vous WHERE Id_RDV = " + req.body.idrdv + " AND Id_Client = " + req.body.idclient) .then((result) => { - console.log(formatDate(result[0]['Date'])) - if(formatDate(result[0]['Date']) >= date){ + if(formatDate(result[0]['Date']) > date){ db.executeQuery("DELETE FROM rendez_vous WHERE Id_RDV = " + req.body.idrdv) .then(r => { console.log(r) @@ -100,45 +110,134 @@ app.post('/supp-rendez-vous', (req, res) => { }) }) +app.post('/ajouter-conseil', (req, res) => { + let id_rdv = req.body.idrdv; + let description = req.body.description; + + let id_article = req.body.idarticle + let has_bought = req.body.hasbought; + + if(typeof id_article === 'undefined'){ + db.executeQuery('INSERT INTO conseil (Description, id_RDV) VALUES ("' + description + '", "' + id_rdv + '")') + .then(() => { + res.status(200).send("Le conseil à bien été ajouté."); + }) + .catch((err) => { + res.status(500).send(err); + }); + } else { + if(typeof has_bought === 'undefined'){ + has_bought = 0; + } + db.executeQuery('INSERT INTO conseil (Description, Has_Bought, Id_RDV, Id_Article) VALUES ("' + description + '", "' + has_bought + '", "' + id_rdv + '", "' + id_article + '")') + .then(() => { + res.status(200).send("Le conseil à bien été ajouté."); + }) + .catch((err) => { + res.status(500).send(err); + }); + } +}) - -app.listen(port, () => { - console.log(`Example app listening on port ${port}`) +app.post('/inscription', (req, res) => { + db.executeQuery("INSERT INTO `Client` (`Id_Client`, `Nom`, `Prenom`, `Mail`, `Password`, `Date_Naissance`, `Sexe`, `Taille`, `Poids`, `Pointure`, `IsAdmin`) VALUES (NULL, '" + + req.body.nom + "', '" + req.body.prenom + "', '" + req.body.email + "', '" + req.body.password + "', '" + + req.body.date_naissance + "', '" + req.body.sexe + "', '" + req.body.taille + "', '" + + req.body.poids + "', '" + req.body.pointure + "', '0')") + .then(() => { + res.status(200).send("Le client à bien été ajouté."); + }) + .catch((err) => { + res.status(500).send(err); + }); }) -function selectAppointmentWithDate(date,address, column = "*") { - return db.executeQuery("SELECT " + column + " FROM Rendez_Vous WHERE DATE=" + date + " AND Id_Adresse="+address) - .then((result) => { - console.log(result) - for (let i = 0; i < result.length; i++) { - - result[i].Heure = formatHour(result[i].Heure); +app.put('/update-conseil', (req, res) => { + let id_conseil = req.body.idconseil; + let description = req.body.description; + + let id_article = req.body.idarticle + let has_bought = req.body.hasbought; + + if(typeof id_article === 'undefined'){ + db.executeQuery('UPDATE conseil SET Description = "' + description + '" WHERE Id_Conseil = "' + id_conseil + '"') + .then(() => { + res.status(200).send("Le conseil à bien été mis à jour."); + }) + .catch((err) => { + res.status(500).send(err); + }); + } else { + if(typeof has_bought === 'undefined'){ + has_bought = 0; + } + db.executeQuery('UPDATE conseil SET Description = "' + description + '", Id_Article = "' + id_article + '", Has_Bought = "' + has_bought + '" WHERE Id_Conseil = "' + id_conseil + '"') + .then(() => { + res.status(200).send("Le conseil à bien été mis à jour."); + }) + .catch((err) => { + res.status(500).send(err); + }); } - console.log(result); - return result; - }) - .catch((err) => { - console.log(err); - return err; - }); +}) + +/* app.put('/update-rendez-vous', (req, res) => { + let realDate = formatDate(req.body.realdate); + let newDate = formatDate(req.body.newdate); + let heure = formatHour(req.body.heure); + db.executeQuery("SELECT * FROM rendez_vous WHERE Id_RDV = " + req.body.idrdv + " AND Id_Client = " + req.body.idclient) + .then((result) => { + if(formatDate(result[0]['Date']) > date){ + db.executeQuery("UPDATE rendez_vous SET Date = " + date + " AND Heure = " + heure + " WHERE Id_RDV = " + result[0]['Id_RDV'] + " AND Id_Client = " + result[0]['Id_Client']) + .then(r => { + console.log(r) + res.status(200).send(r); + }) + .catch((err) => { + res.status(500).send(err); + }) + } else { + res.status(500).send("Impossible de modifier un rendez-vous déjà passé."); + } + }) + .catch((err) => { + res.status(500).send("Aucun rendez-vous à modifier"); + }) +})*/ + + + +function selectAppointmentWithDate(date, column = "*") { + return db.executeQuery("SELECT " + column + " FROM Rendez_Vous WHERE DATE='" + date + "'") + .then((result) => { + for (let i = 0; i < result.length; i++) { + result[i].Heure = formatHour(result[i].Heure); + } + console.log(result); + return result; + }) + .catch((err) => { + console.log(err); + return err; + }); } function formatDate(date) { - var d = new Date(date), - month = '' + (d.getMonth() + 1), - day = '' + d.getDate(), - year = d.getFullYear(); + var d = new Date(date), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); - if (month.length < 2) - month = '0' + month; - if (day.length < 2) - day = '0' + day; + if (month.length < 2) + month = '0' + month; + if (day.length < 2) + day = '0' + day; - return [year, month, day].join('-'); + return [year, month, day].join('-'); } function formatHour(hour) { var hours = hour.substring(0, 2); var minutes = hour.substring(3, 5); return hours + ":" + minutes; -} +} \ No newline at end of file diff --git a/db/creation_db.sql b/db/creation_db.sql index 3ce184dd01ec60cb5b8266c71fdf568e32c47b33..a426d71084e11a6ea525152a3f8f4e36d0fa8838 100644 --- a/db/creation_db.sql +++ b/db/creation_db.sql @@ -5,7 +5,7 @@ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; -SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; +SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUPrendez_vous_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; -- ----------------------------------------------------- -- Schema mydb @@ -24,7 +24,7 @@ CREATE TABLE IF NOT EXISTS `mydb`.`Client` ( `Id_Client` INT NOT NULL AUTO_INCREMENT, `Nom` VARCHAR(80) NULL, `Prenom` VARCHAR(80) NULL, - `Mail` VARCHAR(80) NULL UNIQUE, + `Mail` VARCHAR(80) NULL UNIQUE, `Password` VARCHAR(80) NULL, `Date_Naissance` DATE NULL, `Sexe` VARCHAR(80) NULL, @@ -48,7 +48,6 @@ CREATE TABLE IF NOT EXISTS `mydb`.`Adresse` ( PRIMARY KEY (`Id_Adresse`)) ENGINE = InnoDB; - -- ----------------------------------------------------- -- Table `mydb`.`Rendez_Vous` -- ----------------------------------------------------- @@ -97,8 +96,10 @@ ENGINE = InnoDB; -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `mydb`.`Article` ( `Id_Article` INT NOT NULL AUTO_INCREMENT, - `Nom` VARCHAR(80) NULL, + `Nom` VARCHAR(80) NOT NULL, `Description` VARCHAR(80) NULL, + `Image` VARCHAR(250) NULL, + `Prix` INT NOT NULL, PRIMARY KEY (`Id_Article`)) ENGINE = InnoDB; @@ -111,7 +112,7 @@ CREATE TABLE IF NOT EXISTS `mydb`.`Conseil` ( `Description` VARCHAR(80) NULL, `Has_Bought` TINYINT(1) NULL, `Id_RDV` INT NOT NULL, - `Id_Article` INT NOT NULL, + `Id_Article` INT NULL, PRIMARY KEY (`Id_Conseil`), INDEX `fk_Conseil_Rendez_Vous1_idx` (`Id_RDV` ASC) VISIBLE, INDEX `fk_Conseil_Article1_idx` (`Id_Article` ASC) VISIBLE, diff --git a/db/db.sql b/db/db.sql index 9129138b337c4d08a8ae170f01b431d54b317659..d41f2eacb2869cc4b3fdb85df1161c2080a6f508 100644 --- a/db/db.sql +++ b/db/db.sql @@ -11,34 +11,34 @@ VALUES ('Rue Francois Durafour 13', '1220', 'Vernier', 'https://i.skyrock.net/9640/6429640/pics/164610040_small.jpg'); INSERT INTO mydb.Article -(Nom, Description) +(Nom, Description, Image, Prix) VALUES -('Bar protein', 'Je suis bonne.'), -('Chaussure Betton', 'Tu vas beton.'); +('Bar protein', 'Je suis bonne.', 'https://image.migros.ch/mo-boxed/v-w-480-h-360/af37a13a9d139d707adb40eab1bc31d3db6133d0/sponser-low-carb-bar-choco-brownie.jpg', '2'), +('Chaussure Betton', 'Tu vas beton.', 'https://static.nike.com/a/images/t_PDP_1728_v1/d6420cb8-3eb5-4fb8-ab0e-5676a75710cb/chaussure-de-running-sur-route-pegasus-39-pour-tw7PWn.png', '110'); INSERT INTO mydb.Rendez_Vous (Date, Heure, Description, Id_Client, Id_Adresse) -VALUES +VALUES ('2022-12-05', '14:30', 'Premier visite', (SELECT Id_Client FROM mydb.Client WHERE mail = 'andre.gouveiad@etu.hesge.ch'), (SELECT Id_Adresse FROM mydb.Adresse WHERE Adresse = 'Rue Grange-Levrier 1')), ('2022-11-27', '19:00', 'Visite pour verifier la progression de la course',(SELECT Id_Client FROM mydb.Client WHERE Prenom = 'Valentin'), (SELECT Id_Adresse FROM mydb.Adresse WHERE Adresse = 'Rue Francois Durafour 13')); INSERT INTO mydb.Conseil (Description, Has_Bought, Id_RDV, Id_Article) -VALUES +VALUES ('Faire exercices pour fésier pour le genoux. La bar permetera de faire muscles', 0, (SELECT Id_RDV FROM mydb.Rendez_vous WHERE Date = '2022-12-05'), (SELECT Id_Article FROM mydb.Article WHERE Nom = 'Bar protein')), ('Meilleur maintien de la cheville. Plus adapter pour la plante des pieds', 1, (SELECT Id_RDV FROM mydb.Rendez_vous WHERE Date = '2022-11-27'), (SELECT Id_Article FROM mydb.Article WHERE Nom = 'Chaussure Betton')); INSERT INTO mydb.Video (Nom_Fichier, Id_RDV) -VALUES +VALUES ('Andre-Course-2022-12-05', (SELECT Id_RDV FROM mydb.Rendez_Vous WHERE Date = '2022-12-05')), ('Valentin-Fente-2022-11-27', (SELECT Id_RDV FROM mydb.Rendez_Vous WHERE Date = '2022-11-27')); INSERT INTO mydb.Jour (Nom) -VALUES +VALUES ('Lundi'), -('Mardi'),adresseadresse +('Mardi'), ('Mercredi'), ('Jeudi'), ('Vendredi');