From 47771e818e60251c69d55967f506debfebf3350b Mon Sep 17 00:00:00 2001
From: 65160270 <65160270@go.buu.ac.th>
Date: Fri, 28 Mar 2025 22:40:47 +0700
Subject: [PATCH] update-cart

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

diff --git a/shop-routes/product.js b/shop-routes/product.js
index 9aec742..50870d0 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();
 
 // เธ•เธฑเน‰เธ‡เธ„เนˆเธฒเธ—เธตเนˆเน€เธเน‡เธšเน„เธŸเธฅเนŒเธฃเธนเธ›
@@ -23,6 +24,17 @@ const storage = multer.diskStorage({
 });
 const upload = multer({ storage: storage });
 
+// เธ”เธถเธ‡เธชเธดเธ™เธ„เน‰เธฒเธ—เธฑเน‰เธ‡เธซเธกเธ”
+router.get("/", async (req, res) => {
+    try {
+        const [products] = await pool.execute("SELECT * FROM products");
+        res.render("product", { products });
+    } catch (error) {
+        console.error("Error fetching products:", error);
+        res.status(500).send("Error loading products.");
+    }
+});
+
 // เนเธชเธ”เธ‡เธŸเธญเธฃเนŒเธกเน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒ
 router.get("/add", (req, res) => {
     res.render("product_add", { message: "" });
@@ -31,48 +43,43 @@ router.get("/add", (req, res) => {
 // เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเนƒเธซเธกเนˆเธžเธฃเน‰เธญเธกเธฃเธนเธ›
 router.post("/add", upload.single("image"), async (req, res) => {
     console.log("req.file:", req.file); // เธ•เธฃเธงเธˆเธชเธญเธšเธงเนˆเธฒ multer เน„เธ”เน‰เธฃเธฑเธšเน„เธŸเธฅเนŒเธซเธฃเธทเธญเน„เธกเนˆ
-    console.log("req.body:", req.body); // เธ•เธฃเธงเธˆเธชเธญเธšเธ„เนˆเธฒเธ—เธตเนˆเธชเนˆเธ‡เธกเธฒเนƒเธ™เธŸเธญเธฃเนŒเธก
 
     try {
-        const { name, price, stock, description } = req.body;
-        if (!name || !price || !stock || !description || !req.file) {
-            return res.status(400).json({ message: "เธเธฃเธธเธ“เธฒเธเธฃเธญเธเธ‚เน‰เธญเธกเธนเธฅเนƒเธซเน‰เธ„เธฃเธšเธ–เน‰เธงเธ™ เนเธฅเธฐเน€เธฅเธทเธญเธเธฃเธนเธ›เธ เธฒเธž" });
+        const { name, price, stock } = req.body;
+        if (!name || !price || !stock || !req.file) {
+            return res.status(400).send("เธเธฃเธธเธ“เธฒเธเธฃเธญเธเธ‚เน‰เธญเธกเธนเธฅเนƒเธซเน‰เธ„เธฃเธšเธ–เน‰เธงเธ™ เนเธฅเธฐเน€เธฅเธทเธญเธเธฃเธนเธ›เธ เธฒเธž");
         }
 
         const imagePath = "/uploads/" + req.file.filename;
 
-        const sql = "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)";
-        const values = [name, price, stock, description, imagePath];
-
-        await pool.execute(sql, values);
+        // เนเธเน‰เธˆเธฒเธ 'image' เน€เธ›เน‡เธ™ 'image_url'
+        await pool.execute("INSERT INTO products (name, price, stock, image_url) VALUES (?, ?, ?, ?)", 
+            [name, price, stock, imagePath]);
 
-        console.log("โœ… Product added successfully!");
         res.redirect("/products");
     } catch (error) {
-        console.error("โŒ Error adding product:", error);
-        res.status(500).json({ message: "Internal Server Error", error: error.message });
+        console.error("Error adding product:", error);
+        res.status(500).send("Error adding product.");
     }
 });
 
-// ๐Ÿ“Œ เธ”เธถเธ‡เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒเธ•เธฒเธก ID เนเธฅเธฐเนเธชเธ”เธ‡เธซเธ™เน‰เธฒเนเธเน‰เน„เธ‚
+// เธ”เธถเธ‡เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒเธ•เธฒเธก ID เนเธฅเธฐเนเธชเธ”เธ‡เธซเธ™เน‰เธฒเนเธเน‰เน„เธ‚
 router.get("/edit/:id", async (req, res) => {
     try {
-        console.log("๐Ÿ”ฅ [DEBUG] GET /products/edit/:id called with ID:", req.params.id);
         const [rows] = await pool.execute("SELECT * FROM products WHERE id = ?", [req.params.id]);
         if (rows.length === 0) {
             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.");
     }
 });
 
-// ๐Ÿ“Œ เธญเธฑเธ›เน€เธ”เธ•เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒ
+// เธญเธฑเธ›เน€เธ”เธ•เธ‚เน‰เธญเธกเธนเธฅเธชเธดเธ™เธ„เน‰เธฒ
 router.post("/edit/:id", upload.single("image"), async (req, res) => {
     try {
-        console.log("๐Ÿ”ฅ [DEBUG] POST /products/edit/:id called with ID:", req.params.id);
         const { name, price, stock, description } = req.body;
         let imagePath = req.body.oldImage; // เนƒเธŠเน‰เธฃเธนเธ›เน€เธ”เธดเธกเธ–เน‰เธฒเน„เธกเนˆเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เนƒเธซเธกเนˆ
 
@@ -81,18 +88,26 @@ router.post("/edit/:id", upload.single("image"), async (req, res) => {
             imagePath = "/uploads/" + req.file.filename;
         }
 
-        console.log("๐Ÿ“Œ Update values:", { name, price, stock, description, imagePath });
+        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, price, stock, description, 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