Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 47771e81 authored by 65160270's avatar 65160270
Browse files

update-cart

parent 5ce2bb44
No related branches found
No related tags found
No related merge requests found
......@@ -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;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment