From ad9950d9f05415752fbb36c5045b440329d55322 Mon Sep 17 00:00:00 2001
From: 65160270 <65160270@go.buu.ac.th>
Date: Sat, 29 Mar 2025 09:34:22 +0700
Subject: [PATCH] update-server

---
 config/database.js        |  1 -
 server.js                 | 14 +++++++-------
 shop-routes/auth.js       |  7 +++----
 shop-routes/cart.js       |  7 +++----
 shop-routes/index.js      | 10 +++++-----
 shop-routes/product.js    | 24 ++++++++++++------------
 views/cart.ejs            |  6 +++---
 views/login.ejs           |  2 +-
 views/partials/header.ejs |  1 -
 9 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/config/database.js b/config/database.js
index 861c9bd..242954d 100644
--- a/config/database.js
+++ b/config/database.js
@@ -11,7 +11,6 @@ const pool = mysql.createPool({
     queueLimit: 0
 });
 
-// ๐Ÿ”น เธ—เธ”เธชเธญเธšเธเธฒเธฃเน€เธŠเธทเนˆเธญเธกเธ•เนˆเธญเธเธฒเธ™เธ‚เน‰เธญเธกเธนเธฅ
 (async () => {
     try {
         const connection = await pool.getConnection();
diff --git a/server.js b/server.js
index 6341394..26ff7e5 100644
--- a/server.js
+++ b/server.js
@@ -113,7 +113,7 @@ app.post("/login", async (req, res) => {
 
         req.session.user = { id: user.id, email: user.email };
         
-        req.session.save(err => {  //เธšเธฑเธ™เธ—เธถเธ Session เธเนˆเธญเธ™ Redirect
+        req.session.save(err => {  
             if (err) {
                 console.error("Session save error:", err);
                 return res.send("Session save failed.");
@@ -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]
@@ -222,7 +222,7 @@ app.get('/order/checkout', isLoggedIn, (req, res) => {
 app.get("/logout", (req, res) => {
     req.session.destroy(err => {
         if (err) return res.status(500).json({ message: "Logout failed." });
-        res.clearCookie('connect.sid', { path: '/' });  // เน€เธ„เธฅเธตเธขเธฃเนŒ Cookie เธญเธขเนˆเธฒเธ‡เธ–เธนเธเธ•เน‰เธญเธ‡
+        res.clearCookie('connect.sid', { path: '/' });  
         res.redirect("/login");
     });
 });
diff --git a/shop-routes/auth.js b/shop-routes/auth.js
index 0dc181c..4221530 100644
--- a/shop-routes/auth.js
+++ b/shop-routes/auth.js
@@ -10,7 +10,7 @@ async function checkUser(email, password) {
     const [rows] = await pool.query('SELECT * FROM users WHERE email = ?', [email]);
     if (rows.length > 0) {
         const user = rows[0];
-        const match = await bcrypt.compare(password, user.password); // เธ•เธฃเธงเธˆเธชเธญเธšเธฃเธซเธฑเธชเธœเนˆเธฒเธ™
+        const match = await bcrypt.compare(password, user.password); 
         return match ? user : null;
     }
     return null;
@@ -18,7 +18,7 @@ async function checkUser(email, password) {
 
 // Route เธชเธณเธซเธฃเธฑเธšเนเธชเธ”เธ‡เธซเธ™เน‰เธฒ Login (GET)
 router.get('/login', (req, res) => {
-    res.render('login'); // เธ•เน‰เธญเธ‡เธกเธตเน„เธŸเธฅเนŒ login.ejs เนƒเธ™เน‚เธŸเธฅเน€เธ”เธญเธฃเนŒ views
+    res.render('login');
 });
 
 // Route เธชเธณเธซเธฃเธฑเธš Login (POST)
@@ -28,8 +28,7 @@ router.post('/login', async (req, res) => {
 
     if (user) {
         req.session.user = { id: user.id, email: user.email };
-        req.session.session_id = req.sessionID; // เธšเธฑเธ™เธ—เธถเธ session_id
-
+        req.session.session_id = req.sessionID; 
         req.session.save(err => {
             if (err) {
                 console.error("Session save error:", err);
diff --git a/shop-routes/cart.js b/shop-routes/cart.js
index 8fc009d..12dedc3 100644
--- a/shop-routes/cart.js
+++ b/shop-routes/cart.js
@@ -49,14 +49,13 @@ router.post('/add', async (req, res) => {
             return res.status(400).send('เธชเธดเธ™เธ„เน‰เธฒเธกเธตเธˆเธณเธ™เธงเธ™เน„เธกเนˆเน€เธžเธตเธขเธ‡เธžเธญ');
         }
 
-        // เธ•เธฃเธงเธˆเธชเธญเธšเธงเนˆเธฒเธชเธดเธ™เธ„เน‰เธฒเธ™เธตเน‰เธกเธตเธญเธขเธนเนˆเนƒเธ™เธ•เธฐเธเธฃเน‰เธฒเธซเธฃเธทเธญเธขเธฑเธ‡
         const [[existingItem]] = await pool.query(
             'SELECT id, quantity FROM cart_items WHERE session_id = ? AND product_id = ?',
             [req.session.id, productId]
         );
 
         if (existingItem) {
-            // เธญเธฑเธ›เน€เธ”เธ•เธˆเธณเธ™เธงเธ™เธชเธดเธ™เธ„เน‰เธฒ
+           
             const newQuantity = existingItem.quantity + parseInt(quantity);
             if (newQuantity > product.stock) {
                 return res.status(400).send('เธชเธดเธ™เธ„เน‰เธฒเธกเธตเธˆเธณเธ™เธงเธ™เน„เธกเนˆเน€เธžเธตเธขเธ‡เธžเธญ');
@@ -66,7 +65,7 @@ router.post('/add', async (req, res) => {
                 [newQuantity, existingItem.id]
             );
         } else {
-            // เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเนƒเธซเธกเนˆ
+           
             await pool.query(
                 'INSERT INTO cart_items (session_id, product_id, quantity) VALUES (?, ?, ?)',
                 [req.session.id, productId, parseInt(quantity)]
@@ -130,7 +129,7 @@ router.post('/remove', async (req, res) => {
 
 router.get('/cart', (req, res) => {
     const cartItems = req.session.cart || [];
-    const editingItemId = req.session.editingItemId || null; // เธ–เน‰เธฒเน„เธกเนˆเธกเธตเธเธฒเธฃเนเธเน‰เน„เธ‚เธเน‡เน€เธ›เน‡เธ™ null
+    const editingItemId = req.session.editingItemId || null; 
 
     res.render('cart', { cartItems, total: calculateTotal(cartItems), editingItemId });
 });
diff --git a/shop-routes/index.js b/shop-routes/index.js
index 89da772..6cf1cde 100644
--- a/shop-routes/index.js
+++ b/shop-routes/index.js
@@ -34,10 +34,10 @@ router.post('/add-to-cart', async (req, res) => {
         if (req.session.editingItemId) {
             console.log("Replacing Item ID:", req.session.editingItemId);
 
-            // เธฅเธšเธชเธดเธ™เธ„เน‰เธฒเธ—เธตเนˆเธ•เน‰เธญเธ‡เธเธฒเธฃเน€เธ›เธฅเธตเนˆเธขเธ™เธญเธญเธ
+            
             req.session.cart = req.session.cart.filter(item => item.id !== req.session.editingItemId);
 
-            // เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเธ•เธฑเธงเนƒเธซเธกเนˆเน€เธ‚เน‰เธฒเน„เธ›
+            
             req.session.cart.push({
                 id: product.id,
                 name: product.name,
@@ -48,11 +48,11 @@ router.post('/add-to-cart', async (req, res) => {
 
             console.log("After Editing:", req.session.cart);
 
-            req.session.editingItemId = null; // เธฅเน‰เธฒเธ‡เธ„เนˆเธฒเธซเธฅเธฑเธ‡เน€เธ›เธฅเธตเนˆเธขเธ™เธชเธดเธ™เธ„เน‰เธฒเน€เธชเธฃเน‡เธˆ
+            req.session.editingItemId = null; 
         } else {
             console.log("Adding New Item");
 
-            // เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเธ•เธฒเธกเธ›เธเธ•เธด
+           
             req.session.cart.push({
                 id: product.id,
                 name: product.name,
@@ -63,7 +63,7 @@ router.post('/add-to-cart', async (req, res) => {
         }
 
         console.log("Final Cart:", req.session.cart);
-        res.redirect('/cart'); // เธเธฅเธฑเธšเน„เธ›เธซเธ™เน‰เธฒเธ•เธฐเธเธฃเน‰เธฒ
+        res.redirect('/cart'); 
     } catch (error) {
         console.error(error);
         res.status(500).send('Error adding product to cart');
diff --git a/shop-routes/product.js b/shop-routes/product.js
index 0511268..91ed28a 100644
--- a/shop-routes/product.js
+++ b/shop-routes/product.js
@@ -19,12 +19,12 @@ const storage = multer.diskStorage({
         cb(null, uploadDir);
     },
     filename: (req, file, cb) => {
-        cb(null, Date.now() + path.extname(file.originalname)); // เธ•เธฑเน‰เธ‡เธŠเธทเนˆเธญเน„เธŸเธฅเนŒเน€เธ›เน‡เธ™ timestamp
+        cb(null, Date.now() + path.extname(file.originalname)); 
     }
 });
 const upload = multer({ storage: storage });
 
-// เธ”เธถเธ‡เธชเธดเธ™เธ„เน‰เธฒเธ—เธฑเน‰เธ‡เธซเธกเธ”
+
 router.get("/", async (req, res) => {
     try {
         const [products] = await pool.execute("SELECT * FROM products");
@@ -35,12 +35,12 @@ router.get("/", async (req, res) => {
     }
 });
 
-// เนเธชเธ”เธ‡เธŸเธญเธฃเนŒเธกเน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒ
+
 router.get("/add", (req, res) => {
     res.render("product_add", { product: {}, message: "" });
 });
 
-// เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเนƒเธซเธกเนˆเธžเธฃเน‰เธญเธกเธฃเธนเธ›
+
 router.post("/add", upload.single("image"), async (req, res) => {
     try {
         console.log("Debugging Data:", req.body);
@@ -65,7 +65,7 @@ router.post("/add", upload.single("image"), async (req, res) => {
 });
 
 
-// เธ”เธถเธ‡เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒเธ•เธฒเธก ID เนเธฅเธฐเนเธชเธ”เธ‡เธซเธ™เน‰เธฒเนเธเน‰เน„เธ‚
+
 router.get("/edit/:id", async (req, res) => {
     try {
         const [rows] = await pool.execute("SELECT * FROM products WHERE id = ?", [req.params.id]);
@@ -79,13 +79,13 @@ router.get("/edit/:id", async (req, res) => {
     }
 });
 
-// เธญเธฑเธ›เน€เธ”เธ•เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒ
+
 router.post("/edit/:id", upload.single("image"), async (req, res) => {
     try {
         const { name, price, stock, description } = req.body;
-        let imagePath = req.body.oldImage; // เนƒเธŠเน‰เธฃเธนเธ›เน€เธ”เธดเธกเธ–เน‰เธฒเน„เธกเนˆเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เนƒเธซเธกเนˆ
+        let imagePath = req.body.oldImage; 
 
-        // เธ–เน‰เธฒเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เธฃเธนเธ›เนƒเธซเธกเนˆ เนƒเธซเน‰เนƒเธŠเน‰เน„เธŸเธฅเนŒเนƒเธซเธกเนˆ
+        
         if (req.file) {
             imagePath = "/uploads/" + req.file.filename;
         }
@@ -104,23 +104,23 @@ router.post("/edit/:id", upload.single("image"), async (req, res) => {
             ]
         );
 
-        res.redirect("/products"); // เธเธฅเธฑเธšเน„เธ›เธขเธฑเธ‡เธซเธ™เน‰เธฒเธฃเธฒเธขเธเธฒเธฃเธชเธดเธ™เธ„เน‰เธฒ
+        res.redirect("/products"); 
     } catch (error) {
         console.error("Error updating product:", error);
         res.status(500).send("Error updating product.");
     }
 });
 
-// เธฅเธšเธชเธดเธ™เธ„เน‰เธฒ
+
 router.post("/delete/:id", async (req, res) => {
     try {
         const productId = req.params.id;
 
-        // เธฅเธšเธชเธดเธ™เธ„เน‰เธฒเธญเธญเธเธˆเธฒเธเธเธฒเธ™เธ‚เน‰เธญเธกเธนเธฅ
+        
         await pool.execute("DELETE FROM products WHERE id = ?", [productId]);
 
         console.log("Product deleted successfully!");
-        res.redirect("/products"); // เธเธฅเธฑเธšเน„เธ›เธขเธฑเธ‡เธซเธ™เน‰เธฒเธฃเธฒเธขเธเธฒเธฃเธชเธดเธ™เธ„เน‰เธฒ
+        res.redirect("/products"); 
     } catch (error) {
         console.error("Error deleting product:", error);
         res.status(500).send("Error deleting product.");
diff --git a/views/cart.ejs b/views/cart.ejs
index 1ea6221..95366a3 100644
--- a/views/cart.ejs
+++ b/views/cart.ejs
@@ -35,7 +35,7 @@
                             </button>
                         </form>
 
-                        <!-- เธ›เธธเนˆเธก Remove (เนเธชเธ”เธ‡เธ•เธฅเธญเธ”) -->
+                        
                         <form action="/cart/remove" method="POST">
                             <input type="hidden" name="cartItemId" value="<%= item.id %>">
                             <button type="submit" class="back-btn" style="margin: 0; padding: 0.5rem 1rem; background-color: red;">
@@ -60,7 +60,7 @@
             <script>
                 document.querySelectorAll(".update-btn").forEach(button => {
                     button.addEventListener("click", async (event) => {
-                        event.stopPropagation(); // เธ›เน‰เธญเธ‡เธเธฑเธ™เธเธฒเธฃเธฃเธšเธเธงเธ™เธเธฑเธšเธ›เธธเนˆเธกเธญเธทเนˆเธ™
+                        event.stopPropagation(); //เธ›เน‰เธญเธ‡เธเธฑเธ™เธเธฒเธฃเธฃเธšเธเธงเธ™เธเธฑเธšเธ›เธธเนˆเธกเธญเธทเนˆเธ™
                         const cartItem = event.target.closest(".cart-item");
                         const cartItemId = cartItem.dataset.id;
                         const quantity = cartItem.querySelector("input[name='quantity']").value;
@@ -72,7 +72,7 @@
                         });
             
                         if (response.ok) {
-                            window.location.reload(); // เธฃเธตเน€เธŸเธฃเธŠเธซเธ™เน‰เธฒเธซเธฅเธฑเธ‡เธญเธฑเธ›เน€เธ”เธ•
+                            window.location.reload(); //เธฃเธตเน€เธŸเธฃเธŠเธซเธ™เน‰เธฒเธซเธฅเธฑเธ‡เธญเธฑเธ›เน€เธ”เธ•
                         } else {
                             alert("Failed to update cart.");
                         }
diff --git a/views/login.ejs b/views/login.ejs
index 5f6b531..5f3e1dd 100644
--- a/views/login.ejs
+++ b/views/login.ejs
@@ -4,7 +4,7 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Login</title>
-    <link rel="stylesheet" href="/css/style.css"> <!-- เน€เธŠเธทเนˆเธญเธกเธ•เนˆเธญ CSS -->
+    <link rel="stylesheet" href="/css/style.css"> 
 </head>
 <body>
     <div class="auth-container">
diff --git a/views/partials/header.ejs b/views/partials/header.ejs
index 25772c7..346dbc2 100644
--- a/views/partials/header.ejs
+++ b/views/partials/header.ejs
@@ -7,7 +7,6 @@
     <nav>
         <div class="container">
             <a href="/" class="logo">E-commerce FishStore</a>
-            <!-- เน€เธžเธดเนˆเธกเนเธ–เธšเธ„เน‰เธ™เธซเธฒ -->
             <form action="/search" method="GET" class="search-form">
                 <input type="text" name="query" placeholder="Search for fish..." required>
                 <button type="submit">Search</button>
-- 
GitLab