diff --git a/server.js b/server.js index 6341394ffd81d8b42462072b9e13c2b365f57bcb..b7871c2066fc3795b75e487474986979c64783c5 100644 --- a/server.js +++ b/server.js @@ -23,7 +23,7 @@ app.use(session({ cookie: { maxAge: 24 * 60 * 60 * 1000, secure: false, // เปลี่ยนเป็น true ถ้าใช้ HTTPS - httpOnly: false // เปลี่ยนจาก true เป็น false เพื่อลองดูว่า cookie เก็บค่าหรือไม่ + httpOnly: false // เปลี่ยนจาก true เป็น false เพื่อลองดูว่า cookie เก็บค่า } })); @@ -72,7 +72,7 @@ const isLoggedIn = (req, res, next) => { } }; -//Register + app.get('/register', (req, res) => { res.render('register'); }); @@ -143,7 +143,7 @@ app.get("/search", async (req, res) => { app.get("/cart", isLoggedIn, async (req, res) => { try { - // ดึงรายการสินค้าในตะกร้าจากฐานข้อมูล + //ดึงรายการสินค้าในตะกร้าจากฐานข้อมูล const [cartItems] = await pool.execute(` SELECT c.id, p.name, p.price, c.quantity, p.stock, p.image FROM cart_items c @@ -151,7 +151,7 @@ app.get("/cart", isLoggedIn, async (req, res) => { WHERE c.user_id = ? `, [req.session.user.id]); - // คำนวณยอดรวม + //คำนวณยอดรวม const total = cartItems.reduce((sum, item) => sum + item.price * item.quantity, 0); res.render("cart", { cartItems, total }); @@ -170,20 +170,20 @@ app.post("/cart/add", async (req, res) => { const userId = req.session.user.id; try { - // เช็คว่าสินค้ามีอยู่ในตะกร้าหรือยัง + //เช็คว่าสินค้ามีอยู่ในตะกร้าหรือยัง const [existingCart] = await pool.execute( "SELECT * FROM cart_items WHERE user_id = ? AND product_id = ?", [userId, productId] ); if (existingCart.length > 0) { - // ถ้ามีสินค้าอยู่แล้ว ให้เพิ่มจำนวน + //ถ้ามีสินค้าอยู่แล้ว ให้เพิ่มจำนวน await pool.execute( "UPDATE cart_items SET quantity = quantity + ? WHERE user_id = ? AND product_id = ?", [quantity, userId, productId] ); } else { - // ถ้าไม่มีสินค้า ให้เพิ่มใหม่ + //ถ้าไม่มีสินค้า ให้เพิ่มใหม่ await pool.execute( "INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)", [userId, productId, quantity]