diff --git a/shop-routes/product.js b/shop-routes/product.js index 9aec74274c82b59b75058bc9bc36ab594cfe97f3..50870d0819ccf369348859a9ca83a7de90c8b0ce 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;