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

---
 config/database.js     |  4 ++++
 shop-routes/product.js | 30 +++++++++++++++++-------------
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/config/database.js b/config/database.js
index 30e3a74..bb4c10f 100644
--- a/config/database.js
+++ b/config/database.js
@@ -22,4 +22,8 @@ const pool = mysql.createPool({
     }
 })();
 
+pool.getConnection()
+    .then(() => console.log("Database connected!"))
+    .catch((err) => console.error("Database connection error:", err));
+
 module.exports = pool;
\ No newline at end of file
diff --git a/shop-routes/product.js b/shop-routes/product.js
index b1ddf20..bb5acd6 100644
--- a/shop-routes/product.js
+++ b/shop-routes/product.js
@@ -42,25 +42,29 @@ router.get("/add", (req, res) => {
 
 // เน€เธžเธดเนˆเธกเธชเธดเธ™เธ„เน‰เธฒเนƒเธซเธกเนˆเธžเธฃเน‰เธญเธกเธฃเธนเธ›
 router.post("/add", upload.single("image"), async (req, res) => {
-    console.log("Received Data:", req.body); // เน€เธŠเน‡เธ„เธ„เนˆเธฒเธ—เธตเนˆเธชเนˆเธ‡เธกเธฒ
-    console.log("Uploaded file:", req.file); // เน€เธŠเน‡เธ„เธงเนˆเธฒ multer เน„เธ”เน‰เน„เธŸเธฅเนŒเน„เธซเธก
+    console.log("req.file:", req.file);  // เธ”เธนเธงเนˆเธฒ multer เน„เธ”เน‰เธฃเธฑเธšเน„เธŸเธฅเนŒเน„เธซเธก
+    console.log("req.body:", req.body);  // เธ”เธนเธ„เนˆเธฒเธ—เธตเนˆเธชเนˆเธ‡เธกเธฒเธˆเธฒเธเธŸเธญเธฃเนŒเธก
 
     try {
         const { name, price, stock, description } = 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.");
+        if (!req.file) {
+            console.error("Error: No file uploaded!");
+            return res.status(400).send("File upload failed");
         }
 
-        // เธ–เน‰เธฒเน„เธกเนˆเธกเธตเธเธฒเธฃเธญเธฑเธ›เน‚เธซเธฅเธ”เน„เธŸเธฅเนŒ เนƒเธซเน‰ imagePath เน€เธ›เน‡เธ™ null
-        const imagePath = req.file ? "/uploads/" + req.file.filename : null;
-
-        await pool.execute(
-            "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)",
-            [name, priceNum, stockNum, description, imagePath]
-        );
+        const imagePath = "/uploads/" + req.file.filename;
+        console.log("imagePath:", imagePath);  // เธ•เธฃเธงเธˆเธชเธญเธšเธžเธฒเธ˜เน„เธŸเธฅเนŒเธฃเธนเธ›
+
+        try {
+            await pool.execute(
+                "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)",
+                [name, price, stock, description, imagePath]
+            );
+        } catch (error) {
+            console.error("Database Error:", error);
+            return res.status(500).json({ error: "Database Error", details: error });
+        }
 
         res.redirect("/products");
     } catch (error) {
-- 
GitLab