225 lines
7.1 KiB
JavaScript
225 lines
7.1 KiB
JavaScript
const express = require('express');
|
|
const session = require('express-session');
|
|
const router = require('express').Router();
|
|
const path = require('path');
|
|
|
|
require('dotenv').config({path: 'process.env'});
|
|
|
|
const app = express();
|
|
const mysql = require('mysql');
|
|
|
|
// Datenbankverbindung
|
|
const db = mysql.createConnection({
|
|
host: 'localhost',
|
|
user: 'root',
|
|
password: '',
|
|
database: 'webshop'
|
|
});
|
|
|
|
// Verbindung zur MySQL-Datenbank herstellen
|
|
db.connect(err => {
|
|
if (err) {
|
|
console.error('Fehler beim Verbinden zur Datenbank:', err);
|
|
return;
|
|
}
|
|
console.log('Mit der Datenbank verbunden');
|
|
});
|
|
|
|
app.use(session({
|
|
secret: 'secret',
|
|
resave: true,
|
|
saveUninitialized: true
|
|
}));
|
|
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({extended: true}));
|
|
app.use(express.static(path.join(__dirname, '/scripts')));
|
|
app.use(express.static(path.join(__dirname, '/static')));
|
|
|
|
// API-Route für Produkte
|
|
app.get('/api/products', (req, res) => {
|
|
// SQL-Abfrage für Produktdaten
|
|
const sql = 'SELECT * FROM webshop.product';
|
|
|
|
// Abfrage ausführen
|
|
db.query(sql, (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen der Produkte:', err);
|
|
res.status(500).send('Fehler beim Abrufen der Produkte');
|
|
return;
|
|
}
|
|
res.json(results); // Rückgabe der Produktdaten als JSON
|
|
});
|
|
});
|
|
|
|
app.get('/api/products/new', async (req, res) => {
|
|
// SQL-Abfrage für Produktdaten
|
|
const sql = 'SELECT * FROM webshop.product ORDER BY created_at DESC LIMIT 4';
|
|
|
|
// Abfrage ausführen
|
|
db.query(sql, (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen der Produkte:', err);
|
|
res.status(500).send('Fehler beim Abrufen der Produkte');
|
|
return;
|
|
}
|
|
res.json(results); // Rückgabe der Produktdaten als JSON
|
|
});
|
|
});
|
|
|
|
app.get('/api/products/motorrad', async (req, res) => {
|
|
// SQL-Abfrage für Produktdaten
|
|
const sql = 'SELECT * FROM webshop.product WHERE category_id = 4';
|
|
|
|
// Abfrage ausführen
|
|
db.query(sql, (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen der Produkte:', err);
|
|
res.status(500).send('Fehler beim Abrufen der Produkte');
|
|
return;
|
|
}
|
|
res.json(results); // Rückgabe der Produktdaten als JSON
|
|
});
|
|
});
|
|
|
|
app.get('/api/products/oldtimer', async (req, res) => {
|
|
// SQL-Abfrage für Produktdaten
|
|
const sql = 'SELECT * FROM webshop.product WHERE category_id = 2';
|
|
|
|
// Abfrage ausführen
|
|
db.query(sql, (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen der Produkte:', err);
|
|
res.status(500).send('Fehler beim Abrufen der Produkte');
|
|
return;
|
|
}
|
|
res.json(results); // Rückgabe der Produktdaten als JSON
|
|
});
|
|
});
|
|
|
|
app.get('/api/products/lkw', async (req, res) => {
|
|
// SQL-Abfrage für Produktdaten
|
|
const sql = 'SELECT * FROM webshop.product WHERE category_id = 3';
|
|
|
|
// Abfrage ausführen
|
|
db.query(sql, (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen der Produkte:', err);
|
|
res.status(500).send('Fehler beim Abrufen der Produkte');
|
|
return;
|
|
}
|
|
res.json(results); // Rückgabe der Produktdaten als JSON
|
|
});
|
|
});
|
|
|
|
app.get('/api/products/sportwagen', async (req, res) => {
|
|
// SQL-Abfrage für Produktdaten
|
|
const sql = 'SELECT * FROM webshop.product WHERE category_id = 1';
|
|
|
|
// Abfrage ausführen
|
|
db.query(sql, (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen der Produkte:', err);
|
|
res.status(500).send('Fehler beim Abrufen der Produkte');
|
|
return;
|
|
}
|
|
res.json(results); // Rückgabe der Produktdaten als JSON
|
|
});
|
|
});
|
|
|
|
app.post('/api/user/registration', (req, res) => {
|
|
// SQL-Query für Nutzerregistration
|
|
const {name, lower_name, email, passwd} = req.body;
|
|
const sql = "INSERT INTO user (name, lower_name, email, passwd, passwd_hash_algo) VALUES (?, ?, ?, ?, 'none')"
|
|
|
|
// Query abschicken
|
|
db.query(sql, [name, lower_name, email, passwd], (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Schreiben in die Datenbank: ', err);
|
|
res.status(500).send('Fehler beim Schreiben in die Datenbank');
|
|
return;
|
|
}
|
|
res.status(201).json({message: 'Nutzer erfolgreich hinzugefügt', id: results.insertId})
|
|
})
|
|
})
|
|
|
|
app.post('/api/user/login', (req, res) => {
|
|
const {email, password} = req.body
|
|
const sql = 'SELECT * FROM user WHERE email = ?'
|
|
|
|
db.query(sql, [email], (err, results) => {
|
|
if (err) {
|
|
console.error('Fehler beim Abrufen des Nutzers: ', err)
|
|
return res.status(500).json({message: 'Serverfehler'})
|
|
}
|
|
if (results.length === 0) {
|
|
return res.status(401).json({message: 'E-Mail nicht gefunden'})
|
|
}
|
|
const user = results[0]
|
|
|
|
if (user.passwd !== password) {
|
|
return res.status(401).json({message: 'Falsches Passwort'})
|
|
}
|
|
|
|
req.session.userId = user.id;
|
|
req.session.email = user.email;
|
|
|
|
res.json({message: 'Login erfolgreich', id: user.id})
|
|
})
|
|
})
|
|
|
|
app.post('/api/bestellung', (req, res) => {
|
|
|
|
const {user_id, product_id} = req.body;
|
|
|
|
const sql1 = 'INSERT INTO order_details (, user_id, payment_id, total) VALUES (?, null, null)'
|
|
const sql2 = 'INSERT INTO order_items (user_id, product_id, quantity, order_id) VALUES (?, ?, 1, ?)';
|
|
|
|
db.query(sql1, [user_id, 1, 100.00], (err1, result1) => {
|
|
if (err1) {
|
|
return res.status(500).json({message: 'Fehler beim Erstellen der Bestellung'});
|
|
}
|
|
|
|
db.query(sql2, [user_id, product_id, result1.insertId], (err2, result2) => {
|
|
if (err2) {
|
|
return res.status(500).json({message: 'Fehler beim Hinzufügen des Produkts zur Bestellung'});
|
|
}
|
|
|
|
res.status(201).json({message: 'Produkt bestellt', id: result2.insertId});
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/api/bestellung/daten', (req, res) => {
|
|
|
|
const user_id = req.body;
|
|
|
|
const sql = 'SELECT oi.user_id, oi.product_id, p.product_name, p.price FROM order_items oi INNER JOIN product p ON oi.product_id = p.id 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'});
|
|
}
|
|
|
|
if (results.length === 0) {
|
|
return res.status(404).json({message: 'Keine Bestellung gefunden'});
|
|
}
|
|
|
|
res.json(results);
|
|
});
|
|
});
|
|
|
|
const getIndexRoute = require('./scripts/routes/other/route-index');
|
|
app.use('/', getIndexRoute);
|
|
|
|
app.use((req, res) => {
|
|
res.status(404).sendFile(path.join(__dirname, 'public', '404.html'));
|
|
});
|
|
|
|
// Sever starten
|
|
app.listen(process.env.APP_PORT, () => {
|
|
console.log("\x1b[32m");
|
|
console.log(`Server is running on http://localhost:${process.env.APP_PORT}`);
|
|
console.log("\x1b[0m");
|
|
console.log('Access it now...');
|
|
}); |