From 40e5db6551ac46cce33febe6eeedaf5233dfe52c Mon Sep 17 00:00:00 2001 From: vextv Date: Sat, 26 Apr 2025 19:23:04 +0200 Subject: [PATCH] - WIP: reworked the order function and added error handling for it - added a function to save the user_id in the sessionStorage of the browser --- public/bestellformular/bestellformular.html | 125 ++++++++++---------- public/bestellung/bestellung.html | 41 ++++--- public/login/login.html | 5 +- public/registrieren/registrieren.html | 1 + server.js | 19 ++- 5 files changed, 100 insertions(+), 91 deletions(-) diff --git a/public/bestellformular/bestellformular.html b/public/bestellformular/bestellformular.html index 7678c66..4c81016 100644 --- a/public/bestellformular/bestellformular.html +++ b/public/bestellformular/bestellformular.html @@ -1,89 +1,86 @@ - - - Bestellformular - - - + + + Bestellformular + + +
- -
+ +
-
-
-
-

Bestellformular

+
+
+ +

Bestellformular

- - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + Bestellung absenden + +
+
- Bestellung absenden - -
-
+ - const result = await response.json(); - - if (response.ok) { - alert('Bestellung erfolgreich!'); - window.location.href = '/bestellung.html'; // Weiterleitung zur Bestellübersicht - } else { - alert('Fehler bei der Bestellung: ' + result.message); - } - } catch (error) { - console.error('Fehler:', error); - alert('Fehler beim Bestellen.'); - } - }); - - - - + +
diff --git a/public/bestellung/bestellung.html b/public/bestellung/bestellung.html index 3916acd..9edc202 100644 --- a/public/bestellung/bestellung.html +++ b/public/bestellung/bestellung.html @@ -27,26 +27,37 @@ diff --git a/public/login/login.html b/public/login/login.html index f534d6f..52d9e7a 100644 --- a/public/login/login.html +++ b/public/login/login.html @@ -37,8 +37,8 @@ document.getElementById('submit').addEventListener('click', async (event) => { event.preventDefault() - const email = document.getElementById('email').value; - const password = document.getElementById('password').value; + const email = document.getElementById('email').value + const password = document.getElementById('password').value try { const response = await fetch('/api/user/login', { @@ -50,6 +50,7 @@ }) if (response.ok) { const data = await response.json(); + sessionStorage.setItem("user_id", data.id) alert('Login erfolgreich!'); window.location.href = '/'; // Redirect to home page after login } else { diff --git a/public/registrieren/registrieren.html b/public/registrieren/registrieren.html index 4c5135c..f7ae6d6 100644 --- a/public/registrieren/registrieren.html +++ b/public/registrieren/registrieren.html @@ -75,6 +75,7 @@ }) if (response.ok) { const data = await response.json(); + sessionStorage.setItem("user_id", data.id) alert('Nutzer erfolgreich hinzugefügt! Ihre Kundennummer: ' + data.id) } else { alert('Fehler bei der Registrierung.') diff --git a/server.js b/server.js index 53cf5ba..7f40dd3 100644 --- a/server.js +++ b/server.js @@ -169,27 +169,26 @@ app.post('/api/user/login', (req, res) => { }) app.post('/api/bestellung', (req, res) => { - const { userId, productId} = req.body; - // 1. Produktpreis holen - const productSql = 'SELECT price FROM product WHERE productId = ?'; - const producNameSql = 'SELECT name FROM product WHERE productId = ?'; + const { user_id, product_id} = req.body; + const sql = 'INSERT INTO order_items (user_id, product_id, quantity) VALUES (?, ?, 1)' - db.query(productSql, [productId], (err, productResults) => { - if (err || productResults.length === 0) { + + db.query(sql, [user_id, product_id], (err, results) => { + if (err || results.length === 0) { console.error('Fehler beim Abrufen des Produkts: ', err); return res.status(500).json({ message: 'Produkt nicht gefunden oder Serverfehler' }); } - - const productPrice = productResults[0].price; - + res.status(201).json({message: 'Produkt bestellt', id: results.insertId}) }); }); app.get('/api/bestellung/daten', (req, res) => { + const user_id = req.body; - db.query(sql, [userId], (err, results) => { + const sql = 'SELECT * FROM order_items WHERE user_id = ? ' + db.query(sql, [user_id], (err, results) => { if (err) { console.error('Fehler beim Abrufen der Bestellung: ', err); return res.status(500).json({ message: 'Fehler beim Abrufen der Bestellung' });