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