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
5ce2bb44
Commit
5ce2bb44
authored
3 weeks ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
99c7127f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
shop-routes/product.js
+13
-33
13 additions, 33 deletions
shop-routes/product.js
with
13 additions
and
33 deletions
shop-routes/product.js
+
13
−
33
View file @
5ce2bb44
...
...
@@ -6,11 +6,10 @@ const fs = require("fs");
const
router
=
express
.
Router
();
// ต
รวจสอบว่ามีโฟลเดอร์อัปโหลดหรือยัง
// ต
ั้งค่าที่เก็บไฟล์รูป
const
uploadDir
=
"
public/uploads
"
;
if
(
!
fs
.
existsSync
(
uploadDir
))
{
fs
.
mkdirSync
(
uploadDir
,
{
recursive
:
true
});
console
.
log
(
"
✅ Created upload directory:
"
,
uploadDir
);
}
// ตั้งค่า Multer สำหรับอัปโหลดรูป
...
...
@@ -24,53 +23,34 @@ const storage = multer.diskStorage({
});
const
upload
=
multer
({
storage
:
storage
});
// 🔹 Debug: ตรวจสอบว่าไฟล์นี้ถูกโหลดหรือไม่
console
.
log
(
"
✅ product.js loaded!
"
);
// 📌 ดึงสินค้าทั้งหมด
router
.
get
(
"
/
"
,
async
(
req
,
res
)
=>
{
try
{
console
.
log
(
"
🔥 [DEBUG] GET /products called
"
);
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
)
=>
{
console
.
log
(
"
🔥 [DEBUG] GET /products/add called
"
);
res
.
render
(
"
product_add
"
,
{
message
:
""
});
});
//
📌
เพิ่มสินค้าใหม่พร้อมรูป
// เพิ่มสินค้าใหม่พร้อมรูป
router
.
post
(
"
/add
"
,
upload
.
single
(
"
image
"
),
async
(
req
,
res
)
=>
{
console
.
log
(
"
🔥 [DEBUG] POST /products/add called
"
);
console
.
log
(
"
📂 Uploaded file:
"
,
req
.
file
);
console
.
log
(
"
📦 Request body:
"
,
req
.
body
);
console
.
log
(
"
req.file:
"
,
req
.
file
);
// ตรวจสอบว่า multer ได้รับไฟล์หรือไม่
console
.
log
(
"
req.body:
"
,
req
.
body
);
// ตรวจสอบค่าที่ส่งมาในฟอร์ม
try
{
const
{
name
,
price
,
stock
}
=
req
.
body
;
if
(
!
name
||
!
price
||
!
stock
||
!
req
.
file
)
{
return
res
.
status
(
400
).
send
(
"
กรุณากรอกข้อมูลให้ครบถ้วน และเลือกรูปภาพ
"
);
const
{
name
,
price
,
stock
,
description
}
=
req
.
body
;
if
(
!
name
||
!
price
||
!
stock
||
!
description
||
!
req
.
file
)
{
return
res
.
status
(
400
).
json
({
message
:
"
กรุณากรอกข้อมูลให้ครบถ้วน และเลือกรูปภาพ
"
}
);
}
const
imagePath
=
"
/uploads/
"
+
req
.
file
.
filename
;
// 🔹 Debug: แสดงค่าที่จะใส่เข้า DB
cons
ole
.
log
(
"
📌 Inserting product:
"
,
{
name
,
price
,
stock
,
imagePath
})
;
const
sql
=
"
INSERT INTO products (name, price, stock, description, image_url) VALUES (?, ?, ?, ?, ?)
"
;
cons
t
values
=
[
name
,
price
,
stock
,
description
,
imagePath
]
;
await
pool
.
execute
(
"
INSERT INTO products (name, price, stock, image_url) VALUES (?, ?, ?, ?)
"
,
[
name
,
price
,
stock
,
imagePath
]
);
await
pool
.
execute
(
sql
,
values
);
console
.
log
(
"
✅ Product added successfully!
"
);
res
.
redirect
(
"
/products
"
);
}
catch
(
error
)
{
console
.
error
(
"
❌ Error adding product:
"
,
error
);
res
.
status
(
500
).
send
(
"
Error adding product.
"
);
res
.
status
(
500
).
json
({
message
:
"
Internal Server Error
"
,
error
:
error
.
message
}
);
}
});
...
...
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