diff --git a/controllers/orderController.js b/controllers/orderController.js deleted file mode 100644 index 761a68d1f6289ad580adf574a968f7622090d5db..0000000000000000000000000000000000000000 --- a/controllers/orderController.js +++ /dev/null @@ -1,36 +0,0 @@ - -exports.showOrderPage = (req, res) => { - if (!req.session.userId) { - req.flash('error', 'คุณต้องเข้าสู่ระบบก่อนถึงจะเพิ่มสินค้าได้'); - return res.redirect('/login'); - } - - res.render('order', { title: 'เพิ่มสินค้า' }); - }; - - // บันทึกสินค้าใหม่ - exports.submitOrder = (req, res) => { - const { productName, price, quantity, description } = req.body; - - // ตรวจสอบว่าได้กรอกข้อมูลครบหรือไม่ - if (!productName || !price || !quantity || !description) { - req.flash('error', 'กรุณากรอกข้อมูลให้ครบถ้วน'); - return res.redirect('/order'); - } - - // สมมุติว่าบันทึกข้อมูลสินค้าในฐานข้อมูล - // ตัวอย่างการใช้ MySQL (ตัวอย่างนี้ใช้ `mysql2`): - const db = require('../db'); // เชื่อมต่อฐานข้อมูล - - const query = 'INSERT INTO products (product_name, price, quantity, description) VALUES (?, ?, ?, ?)'; - db.execute(query, [productName, price, quantity, description]) - .then(() => { - req.flash('success', 'สินค้าได้ถูกเพิ่มเรียบร้อยแล้ว!'); - res.redirect('/order'); - }) - .catch(err => { - req.flash('error', 'เกิดข้อผิดพลาดในการเพิ่มสินค้า'); - res.redirect('/order'); - }); - }; - \ No newline at end of file diff --git a/controllers/registerController.js b/controllers/registerController.js index 059c580ffcd8296de10197fe3e360249856cfe2c..dfc085fdc66eb45fcbe0b64c76e83b51450cac53 100644 --- a/controllers/registerController.js +++ b/controllers/registerController.js @@ -7,7 +7,7 @@ module.exports = { }, registerUser: async (req, res) => { - const { email, username, rpassword, confirm_password, fname, lname } = req.body; + const { email, rpassword, confirm_password } = req.body; // ตรวจสอบว่ารหัสผ่านทั้งสองตรงกันหรือไม่ if (rpassword !== confirm_password) { @@ -27,8 +27,8 @@ module.exports = { const hashedPassword = await bcrypt.hash(rpassword, 10); // บันทึกข้อมูลผู้ใช้ในฐานข้อมูล - const query = 'INSERT INTO users (email, username, password, fname, lname) VALUES (?, ?, ?, ?, ?)'; - await pool.execute(query, [email, username, hashedPassword, fname, lname]); + const query = 'INSERT INTO users (email, password) VALUES (?, ?)'; + await pool.execute(query, [email, hashedPassword]); // ส่งข้อความแจ้งเตือนและเปลี่ยนเส้นทางไปที่หน้า login res.redirect('/login'); diff --git a/db.js b/db.js index e15872fe13a3f4aec8442e1c1f91780eb177989b..cfadf33b860426ee0d292852c249959c39704ad8 100644 --- a/db.js +++ b/db.js @@ -10,6 +10,7 @@ const pool = mysql.createPool({ connectionLimit: 10, queueLimit: 0 }); + db.connect((err) => { if (err) { console.error('Error connecting to the database:', err); @@ -17,4 +18,5 @@ db.connect((err) => { console.log('Connected to the database'); } }); + module.exports = pool; diff --git a/index.js b/index.js index 1f683d1d06bb66d0154aefcb94b3f5a808017917..bd51b016ffd4f8d30d17a86633bc4124bd7e2285 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ const indexController = require('./controllers/indexController'); const loginController = require('./controllers/loginController'); const registerController = require('./controllers/registerController'); const logoutController = require('./controllers/logoutController'); -const orderController = require('./controllers/orderController'); // เพิ่ม Controller สำหรับ Order + // เส้นทางหน้าแรก app.get('/', indexController); @@ -41,9 +41,6 @@ app.post('/user/register', registerController.registerUser); // เส้นทาง Logout app.get('/logout', logoutController); -// เส้นทาง Order (GET และ POST) -app.get('/order', orderController.showOrderPage); // แสดงหน้าสั่งซื้อ -app.post('/order', orderController.submitOrder); // ส่งคำสั่งซื้อ // เริ่มต้นเซิร์ฟเวอร์ const port = process.env.PORT || 3000; diff --git a/views/order.ejs b/views/order.ejs deleted file mode 100644 index 673df8d8cf2b56282679dfbcf58c51d9b8a4aa51..0000000000000000000000000000000000000000 --- a/views/order.ejs +++ /dev/null @@ -1,48 +0,0 @@ -<!DOCTYPE html> -<html lang="th"> -<head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>เพิ่มสินค้า</title> - <link rel="stylesheet" href="/css/style.css"> -</head> -<body> - <h1>เพิ่มสินค้าใหม่</h1> - - <% if (flash.error && flash.error.length > 0) { %> - <div class="alert alert-danger"> - <%= flash.error %> - </div> - <% } %> - <% if (flash.success && flash.success.length > 0) { %> - <div class="alert alert-success"> - <%= flash.success %> - </div> - <% } %> - - <form action="/order" method="POST"> - <div class="form-group"> - <label for="productName">ชื่อสินค้า:</label> - <input type="text" id="productName" name="productName" class="form-control" required> - </div> - - <div class="form-group"> - <label for="price">ราคา:</label> - <input type="number" id="price" name="price" class="form-control" required> - </div> - - <div class="form-group"> - <label for="quantity">จำนวน:</label> - <input type="number" id="quantity" name="quantity" class="form-control" required> - </div> - - <div class="form-group"> - <label for="description">คำอธิบายสินค้า:</label> - <textarea id="description" name="description" class="form-control" required></textarea> - </div> - - <button type="submit" class="btn btn-primary">เพิ่มสินค้า</button> - </form> - -</body> -</html>