From 2873a823b9aae613690b7cdb62e56b25b16ed1f1 Mon Sep 17 00:00:00 2001
From: 65160270 <65160270@go.buu.ac.th>
Date: Mon, 24 Mar 2025 15:13:30 +0700
Subject: [PATCH] update-edit

---
 public/css/style.css |  1 +
 shop-routes/cart.js  |  6 +++++
 shop-routes/index.js | 53 ++++++++++++++++++++++++++++++++++++--------
 views/cart.ejs       |  7 +++++-
 4 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/public/css/style.css b/public/css/style.css
index bd42a06..39a11da 100644
--- a/public/css/style.css
+++ b/public/css/style.css
@@ -443,6 +443,7 @@ footer {
     text-decoration: none;
     border-radius: 5px;
     border: 1px solid #ffc107;
+    cursor: pointer;
 }
 
 .edit-btn:hover {
diff --git a/shop-routes/cart.js b/shop-routes/cart.js
index 9da2638..9e3aec5 100644
--- a/shop-routes/cart.js
+++ b/shop-routes/cart.js
@@ -127,4 +127,10 @@ router.post('/remove', async (req, res) => {
     }
 });
 
+router.post('/edit', (req, res) => {
+    const { cartItemId } = req.body;
+    req.session.editingItemId = cartItemId; // เก็บ ID ไว้ชั่วคราว
+    res.redirect('/'); // กลับไปเลือกสินค้าใหม่
+});
+
 module.exports = router;
\ No newline at end of file
diff --git a/shop-routes/index.js b/shop-routes/index.js
index 433cf86..22f835c 100644
--- a/shop-routes/index.js
+++ b/shop-routes/index.js
@@ -1,17 +1,11 @@
-require('dotenv').config(); //โหลดตัวแปร .env ก่อน
+require('dotenv').config();
 const express = require('express');
 const router = express.Router();
-const pool = require('../config/database'); //Import database หลังจากโหลด .env
-const isLoggedIn = (req, res, next) => {
-    if (req.session.userId) {
-        next(); // ผู้ใช้ Login แล้ว
-    } else {
-        res.redirect('/login'); // ยังไม่ Login
-    }
-};
+const pool = require('../config/database');
 
 console.log("Database Host:", process.env.DB_HOST);
 
+// แสดงสินค้าในหน้าแรก
 router.get('/', async (req, res) => {
     try {
         const [products] = await pool.query('SELECT * FROM products WHERE stock > 0');
@@ -22,4 +16,45 @@ router.get('/', async (req, res) => {
     }
 });
 
+// เพิ่มสินค้าในตะกร้า (รองรับระบบ Edit)
+router.post('/add-to-cart', async (req, res) => {
+    const { productId, quantity } = req.body;
+
+    try {
+        const [productResult] = await pool.query('SELECT * FROM products WHERE id = ? AND stock > 0', [productId]);
+        if (productResult.length === 0) {
+            return res.status(404).send('Product not found or out of stock');
+        }
+        const product = productResult[0];
+
+        if (!req.session.cart) {
+            req.session.cart = [];
+        }
+
+        // ถ้ากำลังแก้ไขสินค้า
+        if (req.session.editingItemId) {
+            req.session.cart = req.session.cart.map(item => 
+                item.id === parseInt(req.session.editingItemId)
+                    ? { id: product.id, name: product.name, price: product.price, quantity: parseInt(quantity), stock: product.stock }
+                    : item
+            );
+            req.session.editingItemId = null; // ล้างค่า
+        } else {
+            // เพิ่มสินค้าปกติ
+            req.session.cart.push({
+                id: product.id,
+                name: product.name,
+                price: product.price,
+                quantity: parseInt(quantity),
+                stock: product.stock
+            });
+        }
+
+        res.redirect('/cart'); // กลับไปหน้าตะกร้า
+    } catch (error) {
+        console.error(error);
+        res.status(500).send('Error adding product to cart');
+    }
+});
+
 module.exports = router;
\ No newline at end of file
diff --git a/views/cart.ejs b/views/cart.ejs
index f4a4a0d..4a20a7a 100644
--- a/views/cart.ejs
+++ b/views/cart.ejs
@@ -41,7 +41,12 @@
                                 Remove
                             </button>
                         </form>
-                        <a href="/" class="Edit-btn">Edit</a> <!-- ปุ่ม Edit -->
+                        <form action="/cart/edit" method="POST">
+                            <input type="hidden" name="cartItemId" value="<%= item.id %>">
+                            <button type="submit" class="edit-btn">
+                                Edit
+                            </button>
+                        </form>
                     </div>
                 <% }); %>
             </div>
-- 
GitLab