From d8295efd00ae4c6b79c7458c07b56578fdb1d9e7 Mon Sep 17 00:00:00 2001
From: 65160270 <65160270@go.buu.ac.th>
Date: Fri, 28 Mar 2025 21:26:43 +0700
Subject: [PATCH] update-cart

---
 shop-routes/product.js | 45 +++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 20 deletions(-)

diff --git a/shop-routes/product.js b/shop-routes/product.js
index 1047cd2..b1ddf20 100644
--- a/shop-routes/product.js
+++ b/shop-routes/product.js
@@ -4,6 +4,7 @@ const multer = require("multer");
 const path = require("path");
 const fs = require("fs");
 
+
 const router = express.Router();
 
 // เธ•เธฑเน‰เธ‡เธ„เนˆเธฒเธ—เธตเนˆเน€เธเน‡เธšเน„เธŸเธฅเนŒเธฃเธนเธ›
@@ -18,7 +19,7 @@ const storage = multer.diskStorage({
         cb(null, uploadDir);
     },
     filename: (req, file, cb) => {
-        cb(null, Date.now() + path.extname(file.originalname));
+        cb(null, Date.now() + path.extname(file.originalname)); // เธ•เธฑเน‰เธ‡เธŠเธทเนˆเธญเน„เธŸเธฅเนŒเน€เธ›เน‡เธ™ timestamp
     }
 });
 const upload = multer({ storage: storage });
@@ -29,7 +30,7 @@ router.get("/", async (req, res) => {
         const [products] = await pool.execute("SELECT * FROM products");
         res.render("product", { products });
     } catch (error) {
-        console.error("โŒ Error fetching products:", error);
+        console.error("Error fetching products:", error);
         res.status(500).send("Error loading products.");
     }
 });
@@ -41,7 +42,8 @@ router.get("/add", (req, res) => {
 
 // เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเนƒเธซเธกเนˆเธžเธฃเน‰เธญเธกเธฃเธนเธ›
 router.post("/add", upload.single("image"), async (req, res) => {
-    console.log("๐Ÿ“ธ Uploaded file:", req.file);
+    console.log("Received Data:", req.body); // เน€เธŠเน‡เธ„เธ„เนˆเธฒเธ—เธตเนˆเธชเนˆเธ‡เธกเธฒ
+    console.log("Uploaded file:", req.file); // เน€เธŠเน‡เธ„เธงเนˆเธฒ multer เน„เธ”เน‰เน„เธŸเธฅเนŒเน„เธซเธก
 
     try {
         const { name, price, stock, description } = req.body;
@@ -49,9 +51,10 @@ router.post("/add", upload.single("image"), async (req, res) => {
         const stockNum = parseInt(stock, 10);
 
         if (isNaN(priceNum) || isNaN(stockNum)) {
-            return res.status(400).send("โŒ Price and stock must be valid numbers.");
+            return res.status(400).send("Price and stock must be valid numbers.");
         }
 
+        // เธ–เน‰เธฒเน„เธกเนˆเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เน„เธŸเธฅเนŒ เนƒเธซเน‰ imagePath เน€เธ›เน‡เธ™ null
         const imagePath = req.file ? "/uploads/" + req.file.filename : null;
 
         await pool.execute(
@@ -61,7 +64,7 @@ router.post("/add", upload.single("image"), async (req, res) => {
 
         res.redirect("/products");
     } catch (error) {
-        console.error("โŒ Error adding product:", error);
+        console.error("Error adding product:", error);
         res.status(500).send("Error adding product.");
     }
 });
@@ -71,11 +74,11 @@ router.get("/edit/:id", async (req, res) => {
     try {
         const [rows] = await pool.execute("SELECT * FROM products WHERE id = ?", [req.params.id]);
         if (rows.length === 0) {
-            return res.status(404).send("โŒ เน„เธกเนˆเธžเธšเธชเธดเธ™เธ„เน‰เธฒ");
+            return res.status(404).send("เน„เธกเนˆเธžเธšเธชเธดเธ™เธ„เน‰เธฒ");
         }
         res.render("product_edit", { product: rows[0] });
     } catch (error) {
-        console.error("โŒ Error fetching product:", error);
+        console.error("Error fetching product:", error);
         res.status(500).send("Error loading product.");
     }
 });
@@ -83,32 +86,34 @@ router.get("/edit/:id", async (req, res) => {
 // เธญเธฑเธ›เน€เธ”เธ•เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒ
 router.post("/edit/:id", upload.single("image"), async (req, res) => {
     try {
-        const { name, price, stock, description, oldImage } = req.body;
-        const priceNum = parseFloat(price);
-        const stockNum = parseInt(stock, 10);
-
-        if (isNaN(priceNum) || isNaN(stockNum)) {
-            return res.status(400).send("โŒ Price and stock must be valid numbers.");
-        }
+        const { name, price, stock, description } = req.body;
+        let imagePath = req.body.oldImage; // เนƒเธŠเน‰เธฃเธนเธ›เน€เธ”เธดเธกเธ–เน‰เธฒเน„เธกเนˆเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เนƒเธซเธกเนˆ
 
-        // เนƒเธŠเน‰เธฃเธนเธ›เน€เธ”เธดเธก เธ–เน‰เธฒเน„เธกเนˆเน„เธ”เน‰เธญเธฑเธ›เน‚เธซเธฅเธ”เนƒเธซเธกเนˆ
-        let imagePath = oldImage || null;
+        // เธ–เน‰เธฒเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เธฃเธนเธ›เนƒเธซเธกเนˆ เนƒเธซเน‰เนƒเธŠเน‰เน„เธŸเธฅเนŒเนƒเธซเธกเนˆ
         if (req.file) {
             imagePath = "/uploads/" + req.file.filename;
         }
 
-        console.log("๐Ÿ›  Updating Product:", { name, priceNum, stockNum, description, imagePath, id: req.params.id });
+        console.log("Update values:", { name, price, stock, description, imagePath, id: req.params.id });
 
         await pool.execute(
             "UPDATE products SET name=?, price=?, stock=?, description=?, image_url=? WHERE id=?",
-            [name ?? null, priceNum ?? null, stockNum ?? null, description ?? null, imagePath, req.params.id]
+            [
+                name ?? null,
+                price ?? null,
+                stock ?? null,
+                description ?? null,
+                imagePath ?? null,
+                req.params.id ?? null
+            ]
         );
 
-        res.redirect("/products");
+        res.redirect("/products"); // เธเธฅเธฑเธšเน„เธ›เธขเธฑเธ‡เธซเธ™เน‰เธฒเธฃเธฒเธขเธเธฒเธฃเธชเธดเธ™เธ„เน‰เธฒ
     } catch (error) {
-        console.error("โŒ Error updating product:", error);
+        console.error("Error updating product:", error);
         res.status(500).send("Error updating product.");
     }
 });
 
+
 module.exports = router;
-- 
GitLab