From 5ce2bb44ddf1047f84bf5606f965211309a1fee4 Mon Sep 17 00:00:00 2001 From: 65160270 <65160270@go.buu.ac.th> Date: Fri, 28 Mar 2025 22:38:29 +0700 Subject: [PATCH] update-cart --- shop-routes/product.js | 46 ++++++++++++------------------------------ 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/shop-routes/product.js b/shop-routes/product.js index 3b9d77a..9aec742 100644 --- a/shop-routes/product.js +++ b/shop-routes/product.js @@ -6,11 +6,10 @@ const fs = require("fs"); const router = express.Router(); -// เธ•เธฃเธงเธเธชเธญเธเธงเนเธฒเธกเธตเนเธเธฅเน€เธ”เธญเธฃเนเธญเธฑเธเนเธซเธฅเธ”เธซเธฃเธทเธญเธขเธฑเธ +// เธ•เธฑเนเธเธเนเธฒเธ—เธตเนเน€เธเนเธเนเธเธฅเนเธฃเธนเธ const uploadDir = "public/uploads"; if (!fs.existsSync(uploadDir)) { fs.mkdirSync(uploadDir, { recursive: true }); - console.log("โ… Created upload directory:", uploadDir); } // เธ•เธฑเนเธเธเนเธฒ Multer เธชเธณเธซเธฃเธฑเธเธญเธฑเธเนเธซเธฅเธ”เธฃเธนเธ @@ -24,53 +23,34 @@ const storage = multer.diskStorage({ }); const upload = multer({ storage: storage }); -// ๐”น Debug: เธ•เธฃเธงเธเธชเธญเธเธงเนเธฒเนเธเธฅเนเธเธตเนเธ–เธนเธเนเธซเธฅเธ”เธซเธฃเธทเธญเนเธกเน -console.log("โ… product.js loaded!"); - -// ๐“ เธ”เธถเธเธชเธดเธเธเนเธฒเธ—เธฑเนเธเธซเธกเธ” -router.get("/", async (req, res) => { - try { - console.log("๐”ฅ [DEBUG] GET /products called"); - 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) => { - console.log("๐”ฅ [DEBUG] GET /products/add called"); res.render("product_add", { message: "" }); }); -// ๐“ เน€เธเธดเนเธกเธชเธดเธเธเนเธฒเนเธซเธกเนเธเธฃเนเธญเธกเธฃเธนเธ +// เน€เธเธดเนเธกเธชเธดเธเธเนเธฒเนเธซเธกเนเธเธฃเนเธญเธกเธฃเธนเธ router.post("/add", upload.single("image"), async (req, res) => { - console.log("๐”ฅ [DEBUG] POST /products/add called"); - console.log("๐“ Uploaded file:", req.file); - console.log("๐“ฆ Request body:", req.body); + console.log("req.file:", req.file); // เธ•เธฃเธงเธเธชเธญเธเธงเนเธฒ multer เนเธ”เนเธฃเธฑเธเนเธเธฅเนเธซเธฃเธทเธญเนเธกเน + console.log("req.body:", req.body); // เธ•เธฃเธงเธเธชเธญเธเธเนเธฒเธ—เธตเนเธชเนเธเธกเธฒเนเธเธเธญเธฃเนเธก try { - const { name, price, stock } = req.body; - if (!name || !price || !stock || !req.file) { - return res.status(400).send("เธเธฃเธธเธ“เธฒเธเธฃเธญเธเธเนเธญเธกเธนเธฅเนเธซเนเธเธฃเธเธ–เนเธงเธ เนเธฅเธฐเน€เธฅเธทเธญเธเธฃเธนเธเธ เธฒเธ"); + const { name, price, stock, description } = req.body; + if (!name || !price || !stock || !description || !req.file) { + return res.status(400).json({ message: "เธเธฃเธธเธ“เธฒเธเธฃเธญเธเธเนเธญเธกเธนเธฅเนเธซเนเธเธฃเธเธ–เนเธงเธ เนเธฅเธฐเน€เธฅเธทเธญเธเธฃเธนเธเธ เธฒเธ" }); } const imagePath = "/uploads/" + req.file.filename; - // ๐”น Debug: เนเธชเธ”เธเธเนเธฒเธ—เธตเนเธเธฐเนเธชเนเน€เธเนเธฒ DB - console.log("๐“ Inserting product:", { name, price, stock, imagePath }); + const sql = "INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)"; + const values = [name, price, stock, description, imagePath]; - await pool.execute( - "INSERT INTO products (name, price, stock, image_url) VALUES (?, ?, ?, ?)", - [name, price, stock, imagePath] - ); + await pool.execute(sql, values); + console.log("โ… Product added successfully!"); res.redirect("/products"); } catch (error) { console.error("โ Error adding product:", error); - res.status(500).send("Error adding product."); + res.status(500).json({ message: "Internal Server Error", error: error.message }); } }); -- GitLab