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
99c7127f
Commit
99c7127f
authored
1 month ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
af93c7c0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
config/database.js
+2
-6
2 additions, 6 deletions
config/database.js
shop-routes/product.js
+31
-26
31 additions, 26 deletions
shop-routes/product.js
with
33 additions
and
32 deletions
config/database.js
+
2
−
6
View file @
99c7127f
...
...
@@ -11,7 +11,7 @@ const pool = mysql.createPool({
queueLimit
:
0
});
// ทดสอบการเชื่อมต่อ
//
🔹
ทดสอบการเชื่อมต่อ
ฐานข้อมูล
(
async
()
=>
{
try
{
const
connection
=
await
pool
.
getConnection
();
...
...
@@ -22,8 +22,4 @@ const pool = mysql.createPool({
}
})();
pool
.
getConnection
()
.
then
(()
=>
console
.
log
(
"
Database connected!
"
))
.
catch
((
err
)
=>
console
.
error
(
"
Database connection error:
"
,
err
));
module
.
exports
=
pool
;
\ No newline at end of file
module
.
exports
=
pool
;
This diff is collapsed.
Click to expand it.
shop-routes/product.js
+
31
−
26
View file @
99c7127f
...
...
@@ -4,13 +4,13 @@ const multer = require("multer");
const
path
=
require
(
"
path
"
);
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,25 +24,32 @@ 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
);
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
(
"
req.file:
"
,
req
.
file
);
// ตรวจสอบว่า multer ได้รับไฟล์หรือไม่
console
.
log
(
"
🔥 [DEBUG] POST /products/add called
"
);
console
.
log
(
"
📂 Uploaded file:
"
,
req
.
file
);
console
.
log
(
"
📦 Request body:
"
,
req
.
body
);
try
{
const
{
name
,
price
,
stock
}
=
req
.
body
;
...
...
@@ -52,34 +59,40 @@ router.post("/add", upload.single("image"), async (req, res) => {
const
imagePath
=
"
/uploads/
"
+
req
.
file
.
filename
;
// แก้จาก 'image' เป็น 'image_url'
await
pool
.
execute
(
"
INSERT INTO products (name, price, stock, image_url) VALUES (?, ?, ?, ?)
"
,
[
name
,
price
,
stock
,
imagePath
]);
// 🔹 Debug: แสดงค่าที่จะใส่เข้า DB
console
.
log
(
"
📌 Inserting product:
"
,
{
name
,
price
,
stock
,
imagePath
});
await
pool
.
execute
(
"
INSERT INTO products (name, price, stock, image_url) VALUES (?, ?, ?, ?)
"
,
[
name
,
price
,
stock
,
imagePath
]
);
res
.
redirect
(
"
/products
"
);
}
catch
(
error
)
{
console
.
error
(
"
Error adding product:
"
,
error
);
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
;
// ใช้รูปเดิมถ้าไม่มีการอัปโหลดใหม่
...
...
@@ -88,26 +101,18 @@ router.post("/edit/:id", upload.single("image"), async (req, res) => {
imagePath
=
"
/uploads/
"
+
req
.
file
.
filename
;
}
console
.
log
(
"
Update values:
"
,
{
name
,
price
,
stock
,
description
,
imagePath
,
id
:
req
.
params
.
id
});
console
.
log
(
"
📌
Update values:
"
,
{
name
,
price
,
stock
,
description
,
imagePath
});
await
pool
.
execute
(
"
UPDATE products SET name=?, price=?, stock=?, description=?, image_url=? WHERE id=?
"
,
[
name
??
null
,
price
??
null
,
stock
??
null
,
description
??
null
,
imagePath
??
null
,
req
.
params
.
id
??
null
]
[
name
,
price
,
stock
,
description
,
imagePath
,
req
.
params
.
id
]
);
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