Gitlab@Informatics

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

update-cart

parent 267629ef
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ const multer = require("multer"); ...@@ -4,6 +4,7 @@ const multer = require("multer");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const router = express.Router(); const router = express.Router();
// ตั้งค่าที่เก็บไฟล์รูป // ตั้งค่าที่เก็บไฟล์รูป
...@@ -18,7 +19,7 @@ const storage = multer.diskStorage({ ...@@ -18,7 +19,7 @@ const storage = multer.diskStorage({
cb(null, uploadDir); cb(null, uploadDir);
}, },
filename: (req, file, cb) => { filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname)); cb(null, Date.now() + path.extname(file.originalname)); // ตั้งชื่อไฟล์เป็น timestamp
} }
}); });
const upload = multer({ storage: storage }); const upload = multer({ storage: storage });
...@@ -29,7 +30,7 @@ router.get("/", async (req, res) => { ...@@ -29,7 +30,7 @@ router.get("/", async (req, res) => {
const [products] = await pool.execute("SELECT * FROM products"); const [products] = await pool.execute("SELECT * FROM products");
res.render("product", { products }); res.render("product", { products });
} catch (error) { } catch (error) {
console.error("Error fetching products:", error); console.error("Error fetching products:", error);
res.status(500).send("Error loading products."); res.status(500).send("Error loading products.");
} }
}); });
...@@ -41,7 +42,8 @@ router.get("/add", (req, res) => { ...@@ -41,7 +42,8 @@ router.get("/add", (req, res) => {
// เพิ่มสินค้าใหม่พร้อมรูป // เพิ่มสินค้าใหม่พร้อมรูป
router.post("/add", upload.single("image"), async (req, res) => { router.post("/add", upload.single("image"), async (req, res) => {
console.log("📸 Uploaded file:", req.file); console.log("Received Data:", req.body); // เช็คค่าที่ส่งมา
console.log("Uploaded file:", req.file); // เช็คว่า multer ได้ไฟล์ไหม
try { try {
const { name, price, stock, description } = req.body; const { name, price, stock, description } = req.body;
...@@ -49,9 +51,10 @@ router.post("/add", upload.single("image"), async (req, res) => { ...@@ -49,9 +51,10 @@ router.post("/add", upload.single("image"), async (req, res) => {
const stockNum = parseInt(stock, 10); const stockNum = parseInt(stock, 10);
if (isNaN(priceNum) || isNaN(stockNum)) { if (isNaN(priceNum) || isNaN(stockNum)) {
return res.status(400).send("Price and stock must be valid numbers."); return res.status(400).send("Price and stock must be valid numbers.");
} }
// ถ้าไม่มีการอัปโหลดไฟล์ ให้ imagePath เป็น null
const imagePath = req.file ? "/uploads/" + req.file.filename : null; const imagePath = req.file ? "/uploads/" + req.file.filename : null;
await pool.execute( await pool.execute(
...@@ -61,7 +64,7 @@ router.post("/add", upload.single("image"), async (req, res) => { ...@@ -61,7 +64,7 @@ router.post("/add", upload.single("image"), async (req, res) => {
res.redirect("/products"); res.redirect("/products");
} catch (error) { } catch (error) {
console.error("Error adding product:", error); console.error("Error adding product:", error);
res.status(500).send("Error adding product."); res.status(500).send("Error adding product.");
} }
}); });
...@@ -71,11 +74,11 @@ router.get("/edit/:id", async (req, res) => { ...@@ -71,11 +74,11 @@ router.get("/edit/:id", async (req, res) => {
try { try {
const [rows] = await pool.execute("SELECT * FROM products WHERE id = ?", [req.params.id]); const [rows] = await pool.execute("SELECT * FROM products WHERE id = ?", [req.params.id]);
if (rows.length === 0) { if (rows.length === 0) {
return res.status(404).send("ไม่พบสินค้า"); return res.status(404).send("ไม่พบสินค้า");
} }
res.render("product_edit", { product: rows[0] }); res.render("product_edit", { product: rows[0] });
} catch (error) { } catch (error) {
console.error("Error fetching product:", error); console.error("Error fetching product:", error);
res.status(500).send("Error loading product."); res.status(500).send("Error loading product.");
} }
}); });
...@@ -83,32 +86,34 @@ router.get("/edit/:id", async (req, res) => { ...@@ -83,32 +86,34 @@ router.get("/edit/:id", async (req, res) => {
// อัปเดตข้อมูลสินค้า // อัปเดตข้อมูลสินค้า
router.post("/edit/:id", upload.single("image"), async (req, res) => { router.post("/edit/:id", upload.single("image"), async (req, res) => {
try { try {
const { name, price, stock, description, oldImage } = req.body; const { name, price, stock, description } = req.body;
const priceNum = parseFloat(price); let imagePath = req.body.oldImage; // ใช้รูปเดิมถ้าไม่มีการอัปโหลดใหม่
const stockNum = parseInt(stock, 10);
if (isNaN(priceNum) || isNaN(stockNum)) {
return res.status(400).send("❌ Price and stock must be valid numbers.");
}
// ใช้รูปเดิม ถ้าไม่ได้อัปโหลดใหม่ // ถ้ามีการอัปโหลดรูปใหม่ ให้ใช้ไฟล์ใหม่
let imagePath = oldImage || null;
if (req.file) { if (req.file) {
imagePath = "/uploads/" + req.file.filename; imagePath = "/uploads/" + req.file.filename;
} }
console.log("🛠 Updating Product:", { name, priceNum, stockNum, description, imagePath, id: req.params.id }); console.log("Update values:", { name, price, stock, description, imagePath, id: req.params.id });
await pool.execute( await pool.execute(
"UPDATE products SET name=?, price=?, stock=?, description=?, image_url=? WHERE id=?", "UPDATE products SET name=?, price=?, stock=?, description=?, image_url=? WHERE id=?",
[name ?? null, priceNum ?? null, stockNum ?? null, description ?? null, 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) { } catch (error) {
console.error("Error updating product:", error); console.error("Error updating product:", error);
res.status(500).send("Error updating product."); res.status(500).send("Error updating product.");
} }
}); });
module.exports = router; module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment