- 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
This commit is contained in:
parent
c5ef5d49c0
commit
40e5db6551
@ -36,19 +36,16 @@
|
||||
<label for="ort">Ort:</label>
|
||||
<input type="text" id="ort" name="ort" required placeholder="Ort">
|
||||
|
||||
<label for="telefon Nr">Telefon Nr (optional):</label>
|
||||
<input type="tel" id="telefon Nr" name="telefon" placeholder="Telefon Nr">
|
||||
|
||||
<label for="ArtikelNr">Artikel Nr.:</label>
|
||||
<textarea id="ArtikelNr" name="ArtikelNr" rows="4" required placeholder="Artikel Nr."></textarea>
|
||||
|
||||
<a href="/bestellung" class="button-submit" id="bestellformular">Bestellung absenden</a>
|
||||
<a href="/bestellung" class="button-submit" id="sendOrder">Bestellung absenden</a>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<script>
|
||||
document.getElementById('bestellform').addEventListener('bestellformular', async (event) => {
|
||||
document.getElementById('bestellform').addEventListener('sendOrder', async (event) => {
|
||||
event.preventDefault(); // Verhindert das Standardformularverhalten
|
||||
|
||||
const userId = document.getElementById('KundenNr').value;
|
||||
|
||||
@ -27,17 +27,27 @@
|
||||
|
||||
<script>
|
||||
async function ladeBestellung() {
|
||||
const user_id = sessionStorage.getItem('user_id')
|
||||
console.log('User ID: ', user_id)
|
||||
if (!user_id){
|
||||
console.log("is null")
|
||||
alert('Sie sind nicht eingeloggt! Bitte loggen sie sich ein damit diese Seite angezeigt werden kann.')
|
||||
} else if (user_id <= 1) {
|
||||
try {
|
||||
const response = await fetch('/api/bestellung/daten');
|
||||
|
||||
const response = await fetch('/api/bestellung/daten', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(user_id)
|
||||
})
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
|
||||
document.getElementById('kundenNr').textContent = data.kundenNr;
|
||||
document.getElementById('produktId').textContent = data.artikelNr;
|
||||
document.getElementById('produktName').textContent = data.produktName;
|
||||
document.getElementById('preis').textContent = data.preis + ' €';
|
||||
document.getElementById('kundenNr').textContent = user_id
|
||||
document.getElementById('produktId').textContent = data.product_id
|
||||
//document.getElementById('produktName').textContent = data.
|
||||
document.getElementById('preis').textContent = data.total + ' €'
|
||||
} else {
|
||||
console.error('Fehler beim Laden der Bestelldaten.');
|
||||
}
|
||||
@ -45,6 +55,7 @@
|
||||
console.error('Fehler: ', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('DOMContentLoaded', ladeBestellung);
|
||||
</script>
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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.')
|
||||
|
||||
19
server.js
19
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' });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user