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
77edd60e
Commit
77edd60e
authored
2 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
e1b10100
Branches
Branches containing commit
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
+11
-21
11 additions, 21 deletions
shop-routes/product.js
with
11 additions
and
21 deletions
shop-routes/product.js
+
11
−
21
View file @
77edd60e
...
@@ -41,36 +41,26 @@ router.get("/add", (req, res) => {
...
@@ -41,36 +41,26 @@ router.get("/add", (req, res) => {
});
});
// เพิ่มสินค้าใหม่พร้อมรูป
// เพิ่มสินค้าใหม่พร้อมรูป
router
.
post
(
"
/add
"
,
upload
.
single
(
"
image
"
),
async
(
req
,
res
)
=>
{
router
.
post
(
"
/add
"
,
upload
.
single
(
"
image
"
),
async
(
req
,
res
)
=>
{
// เปลี่ยนจาก "images" เป็น "image" ให้ตรงกับ name ของ input file
try
{
console
.
log
(
"
req.file:
"
,
req
.
file
);
// ตรวจสอบว่า multer ได้รับไฟล์หรือไม่
console
.
log
(
"
Request received:
"
,
req
.
body
);
console
.
log
(
"
Uploaded file:
"
,
req
.
file
);
if
(
!
req
.
file
)
{
try
{
console
.
error
(
"
Error: No file uploaded!
"
);
const
{
name
,
price
,
stock
,
description
}
=
req
.
body
;
// เพิ่ม description
return
res
.
status
(
400
).
json
({
message
:
"
File upload failed
"
});
}
const
{
name
,
price
,
stock
,
description
}
=
req
.
body
;
const
imagePath
=
"
/uploads/
"
+
req
.
file
.
filename
;
const
imagePath
=
"
/uploads/
"
+
req
.
file
.
filename
;
const
sql
=
"
INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)
"
;
await
pool
.
execute
(
const
values
=
[
name
,
price
,
stock
,
description
,
imagePath
];
"
INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)
"
,
[
name
,
price
,
stock
,
description
,
imagePath
]
// เพิ่ม description
console
.
log
(
"
SQL Query:
"
,
sql
);
);
console
.
log
(
"
Values:
"
,
values
);
await
pool
.
execute
(
sql
,
values
);
res
.
status
(
201
).
json
({
success
:
true
,
message
:
"
Product added successfully!
"
});
res
.
redirect
(
"
/products
"
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"
Server Error
:
"
,
error
);
console
.
error
(
"
Error adding product
:
"
,
error
);
res
.
status
(
500
).
json
({
message
:
"
Internal Server Error
"
,
error
:
error
.
message
}
);
res
.
status
(
500
).
send
(
"
Error adding product.
"
);
}
}
});
});
// ดึงข้อมูลสินค้าตาม ID และแสดงหน้าแก้ไข
// ดึงข้อมูลสินค้าตาม ID และแสดงหน้าแก้ไข
router
.
get
(
"
/edit/:id
"
,
async
(
req
,
res
)
=>
{
router
.
get
(
"
/edit/:id
"
,
async
(
req
,
res
)
=>
{
try
{
try
{
...
...
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