repaired /bestellformular und /bestellung

This commit is contained in:
florianspengler 2025-04-29 09:39:11 +02:00
parent b66c54ff57
commit 7eb7321680
2 changed files with 87 additions and 33 deletions

View File

@ -27,38 +27,76 @@
<script> <script>
async function ladeBestellung() { async function ladeBestellung() {
const user_id = sessionStorage.getItem('user_id') const user_id = sessionStorage.getItem('user_id');
if (!user_id) { if (!user_id) {
alert('Sie sind nicht eingeloggt! Bitte loggen sie sich ein damit diese Seite angezeigt werden kann.') alert('Sie sind nicht eingeloggt! Bitte loggen Sie sich ein.');
window.location.href = '/login' window.location.href = '/login';
} else if (user_id >= 1) { return;
}
try { try {
const response = await fetch('/api/bestellung/daten', { const response = await fetch('/api/bestellung/daten', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({user_id: user_id}) body: JSON.stringify({ user_id: user_id })
}) });
if (response.ok) { if (response.ok) {
const data = await response.json(); const daten = await response.json();
console.log(data) const bestellungContainer = document.getElementById('bestellung');
document.getElementById('kundenNr').textContent = user_id bestellungContainer.innerHTML = '';
//document.getElementById('produktId').textContent = data.product_id
//document.getElementById('produktName').textContent = data. const bestellungen = {};
document.getElementById('preis').textContent = data.total + ' €'
daten.forEach(item => {
if (!bestellungen[item.order_id]) {
bestellungen[item.order_id] = {
total: item.order_total,
produkte: []
};
}
bestellungen[item.order_id].produkte.push({
name: item.product_name,
preis: item.product_price,
quantity: item.quantity
});
});
for (const orderId in bestellungen) {
const bestellung = bestellungen[orderId];
const bestellCard = document.createElement('div');
bestellCard.className = 'bestell-info-card';
let produkteHTML = '';
bestellung.produkte.forEach(produkt => {
produkteHTML += `
<p>• ${produkt.name} — ${produkt.quantity} Stück — Preis: ${produkt.preis.toFixed(2)} €</p>
`;
});
bestellCard.innerHTML = `
<h3>Bestellnummer: ${orderId}</h3>
${produkteHTML}
<p><strong>Gesamtsumme:</strong> ${bestellung.total.toFixed(2)} €</p>
`;
bestellungContainer.appendChild(bestellCard);
}
} else if (response.status === 404) {
document.getElementById('bestellung').innerHTML = '<p>Keine Bestellungen gefunden.</p>';
} else { } else {
console.error('Fehler beim Laden der Bestelldaten.'); console.error('Fehler beim Laden der Bestellungen.');
} }
} catch (error) { } catch (error) {
console.error('Fehler: ', error); console.error('Fehler: ', error);
} }
} }
}
window.addEventListener('DOMContentLoaded', ladeBestellung); window.addEventListener('DOMContentLoaded', ladeBestellung);
</script> </script>
<!-- Footer wird dynamisch geladen --> <!-- Footer wird dynamisch geladen -->
<div id="footer"></div> <div id="footer"></div>

View File

@ -225,21 +225,37 @@ app.post('/api/bestellung', (req, res) => {
}); });
}); });
app.post('/api/bestellung/daten', (req, res) => { app.post('/api/bestellung/daten', (req, res) => {
const { user_id } = req.body;
const user_id = req.body; const sql = `
SELECT
od.id AS order_id,
od.total AS order_total,
oi.product_id,
oi.quantity,
p.name AS product_name,
p.price AS product_price
FROM
webshop.order_details od
INNER JOIN
webshop.order_items oi ON od.id = oi.order_id
INNER JOIN
webshop.product p ON oi.product_id = p.id
WHERE
od.user_id = ?
ORDER BY
od.id DESC
`;
const sql = 'SELECT oi.user_id, oi.product_id, p.name AS product_name, p.price FROM order_items oi INNER JOIN product p ON oi.product_id = p.id WHERE oi.user_id = ? '
db.query(sql, [user_id], (err, results) => { db.query(sql, [user_id], (err, results) => {
if (err) { if (err) {
console.error('Fehler beim Abrufen der Bestellung: ', err); console.error('Fehler beim Abrufen der Bestellungen: ', err);
return res.status(500).json({message: 'Fehler beim Abrufen der Bestellung'}); return res.status(500).json({ message: 'Fehler beim Abrufen der Bestellungen' });
} }
if (results.length === 0) { if (results.length === 0) {
return res.status(404).json({message: 'Keine Bestellung gefunden'}); return res.status(404).json({ message: 'Keine Bestellungen gefunden.' });
} }
res.json(results); res.json(results);