From f958d0918b522a34ad77e42ead363df6f79efbd6 Mon Sep 17 00:00:00 2001 From: TheCoreDev Date: Mon, 13 Nov 2023 22:53:12 +0100 Subject: [PATCH] =?UTF-8?q?Ca=20commence=20=C3=A0=20fonctionner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 83 +++++++++++++++++++++++++++++++++++++++---- public/styles.css | 23 +++++++----- views/index.hbs | 8 ++--- views/offrant.hbs | 30 ++++++++++++++++ views/reservation.hbs | 45 +++++++++++++---------- 5 files changed, 152 insertions(+), 37 deletions(-) create mode 100644 views/offrant.hbs diff --git a/app.js b/app.js index 89d0114..4c4ff1c 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,8 @@ const dotenv = require('dotenv') const app = express() const path = require("path") const publicDir = path.join(__dirname, './public') +var bodyParser = require('body-parser') + /* Connexion à la BDD MySQL */ dotenv.config({ path: './.env'}) @@ -22,14 +24,19 @@ db.connect((error) => { } }) + app.set('view engine', 'hbs'); app.use(express.static(publicDir)); /* Pour utiliser le bootstrap local */ app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); -app.use(express.json()); +/* configure the Express.js server to receive the form values as JSON */ +app.use(bodyParser.urlencoded({extended: false})); +app.use(express.json()) + app.get("/", (req, res) => { - db.query('SELECT c.id as "id", c.libelle as "libelle", c.prix, c.photo as "photo", c.lien, p.prenom as "acheteur", c.achete as "achete", c.paye as "paye", p2.prenom as "responsable_achat" FROM cadeau c LEFT OUTER JOIN participations pc on pc.cadeau = c.id LEFT OUTER JOIN participant p on p.id = pc.offrant LEFT OUTER JOIN participant p2 on p2.id = pc.responsable_achat', async (error, resultCadeaux) => { + + db.query('SELECT c.id as "id", c.libelle as "libelle", c.prix, c.photo as "photo", c.lien, p.prenom as "acheteur", c.achete as "achete", c.paye as "paye", p2.prenom as "responsable_achat" FROM cadeau c LEFT OUTER JOIN participations pc on pc.cadeau = c.id LEFT OUTER JOIN participant p on p.id = pc.offrant LEFT OUTER JOIN participant p2 on p2.id = pc.responsable_achat ORDER BY c.prix', async (error, resultCadeaux) => { let message; if(error){ @@ -44,16 +51,57 @@ app.get("/", (req, res) => { }); -app.get("/reserve", (req, res) => { - db.query('SELECT id, libelle, prix, photo, lien FROM cadeau WHERE id = ?', [req.query.id], async (error, result) => { +app.get("/choix-offrant", (req, res) => { + let cadeau = req.query.cadeau; + + db.query('SELECT id, prenom FROM participant ORDER BY prenom', async (error, result) => { if(error) { console.log(error); } if (result.length > 0) { - db.query('SELECT id, prenom, FROM participant', async (error, selectOffrant) => { + res.render('offrant', { "cadeau" : cadeau, "listeOffrants": result}); + } + }) +}) - db.query('SELECT sum(c.prix) FROM cadeau c LEFT OUTER JOIN participations pc on pc.cadeau = c.id WHERE pc.offrant = ?', [], async (error, totalAchats) => { - res.render('reservation', { "id": result[0].id, "libelle": result[0].libelle, "photo": result[0].photo, "offrant": selectOffrant, "totalDepenses": totalAchats } ); + +app.get("/reservation", (req, res) => { + + let reqCadeau = req.query.cadeau; + let reqOffrant = req.query.offrant; + + let totalAchats; + + db.query('SELECT id, libelle, prix, photo, lien, achete, paye FROM cadeau WHERE id = ?', [reqCadeau], async (error, infosCadeau) => { + if(error) { + console.log(error); + } + if (infosCadeau.length > 0) { + db.query('SELECT COALESCE(sum(c.prix),0) as "total" FROM cadeau c RIGHT OUTER JOIN participations pc ON pc.cadeau = c.id INNER JOIN participant p ON p.id = pc.offrant WHERE p.id=?', [reqOffrant], async (error, result) => { + if(error) { + console.log(error); + } + if (result.length > 0) { + totalAchats = result[0] + } + + db.query('SELECT id, prenom FROM participant WHERE id=?', [reqOffrant], async (error, result) => { + if(error) { + console.log(error); + } + if (result.length > 0) { + offrant = result[0]; + } + }); + + db.query('SELECT id, prenom FROM participant ORDER BY prenom', async (error, listeResponsables) => { + if(error) { + console.log(error); + } + res.render('reservation', { "infosCadeaux": infosCadeau[0], + "offrant" : offrant, + "totalAchats": totalAchats.total, + "listeResponsables" : listeResponsables} ); }) }) } @@ -61,6 +109,27 @@ app.get("/reserve", (req, res) => { }); + +app.post("/confirm-reservation", (req, res) => { + + const { cadeau, offrant, achete, paye, responsableAchat } = req.body + + db.query('UPDATE cadeau SET achete=?, paye=? WHERE ID=?', [achete, paye, cadeau], function (err, result) { + if (err) throw err; + console.log("1 record updated"); + }); + + db.query('INSERT INTO participations (offrant, cadeau, responsable_achat) VALUES (?, ?, ?)', [offrant, cadeau, responsableAchat], function (err, result) { + if (err) throw err; + console.log("1 record updated"); + }); + + + res.redirect('/'); + +}); + + app.listen(5006, ()=> { console.log("server started on port 5006") }); diff --git a/public/styles.css b/public/styles.css index 3a39435..95a2743 100644 --- a/public/styles.css +++ b/public/styles.css @@ -4,15 +4,14 @@ text-align: left; } -.tableau li:nth-child(1) { - width: 300px; -} +/* .tableau li:nth-child(2), .tableau li:nth-child(3), .tableau li:nth-child(4), .tableau li:nth-child(5) { width: 100px; } +*/ .tableau li { flex: auto; @@ -30,14 +29,22 @@ font-weight: bold; } -figure img { +img { height: 80px !important; + transition: transform .1s; } -figure img:hover { - height: 180px !important; +img:hover{ + -ms-transform: scale(5); /* IE 9 */ + -webkit-transform: scale(5); /* Safari 3-8 */ + transform: scale(5); } -.image { - width: 300px; +.offrants { + width: 100%; +} + +.depenses { + font-weight: bold; + color: red;; } \ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs index bb506f7..86ed903 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -11,7 +11,7 @@
-
+

Liste des cadeaux de Noël pour Louisa

{{ message }}

@@ -24,12 +24,13 @@
  •  
  •  
  •  
  • -
  • Réservé par
  • +
  • Qui va l'offrir ?
  •  
  • Qui l'achète ?
  • {{#each cadeaux}}
      +
    • Voir le produit
    • Pas d'image @@ -37,9 +38,8 @@
    • {{this.prix}} €
    • -
    • Voir le produit
    • {{#if this.acheteur}}{{this.acheteur}} - {{else}}Le réserver{{/if}} + {{else}}Le réserver{{/if}}
    • diff --git a/views/offrant.hbs b/views/offrant.hbs new file mode 100644 index 0000000..4b143b1 --- /dev/null +++ b/views/offrant.hbs @@ -0,0 +1,30 @@ + + + + + + + Qui êtes-vous ? + + + + + +
      +
      +

      Qui êtes-vous ?

      +

      {{ message }}

      +
      + +
      +
      + {{#each listeOffrants}} + + {{/each}} +
      + Retour aux cadeaux +
      + + diff --git a/views/reservation.hbs b/views/reservation.hbs index 7874169..ac3583a 100644 --- a/views/reservation.hbs +++ b/views/reservation.hbs @@ -11,49 +11,58 @@
      -
      +

      Réserver ce cadeau

      {{ message }}

      - -
      -
      {{libelle}}
      +
      +
      {{infosCadeaux.libelle}}
      + +
      - +
      - + +

      Pour l'instant, tu as déjà dépensé : {{totalAchats}}€

      +
      - +
      - - + +
      - - + +
      - - {{#each responsable}} + {{#each listeResponsables}} {{/each}}
      +
      + +
      + + +
      +
      + + +
      +
      Retour aux cadeaux