Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 74faf970 authored by 65160270's avatar 65160270
Browse files

Edit server.js

parent bb7c6a64
No related branches found
No related tags found
No related merge requests found
......@@ -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]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment