Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cloudupdate_prj
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160270
cloudupdate_prj
Commits
47771e81
Commit
47771e81
authored
2 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
5ce2bb44
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
shop-routes/product.js
+35
-20
35 additions, 20 deletions
shop-routes/product.js
with
35 additions
and
20 deletions
shop-routes/product.js
+
35
−
20
View file @
47771e81
...
...
@@ -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
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment