Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prjcloud_65160270
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160270
prjcloud_65160270
Commits
11dcf971
Commit
11dcf971
authored
3 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-address
parent
134cc1ca
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
server.js
+2
-0
2 additions, 0 deletions
server.js
shop-routes/order.js
+94
-85
94 additions, 85 deletions
shop-routes/order.js
with
96 additions
and
85 deletions
server.js
+
2
−
0
View file @
11dcf971
...
@@ -38,6 +38,8 @@ app.use(express.urlencoded({ extended: true }));
...
@@ -38,6 +38,8 @@ app.use(express.urlencoded({ extended: true }));
// Middleware เช็ค Session
// Middleware เช็ค Session
app
.
use
((
req
,
res
,
next
)
=>
{
app
.
use
((
req
,
res
,
next
)
=>
{
console
.
log
(
"
Session Middleware Checked
"
);
console
.
log
(
"
Session Middleware Checked
"
);
console
.
log
(
"
🔹 Session ID:
"
,
req
.
sessionID
);
console
.
log
(
"
🔹 Session Data:
"
,
req
.
session
);
next
();
next
();
});
});
...
...
This diff is collapsed.
Click to expand it.
shop-routes/order.js
+
94
−
85
View file @
11dcf971
...
@@ -11,8 +11,17 @@ function isAuthenticated(req, res, next) {
...
@@ -11,8 +11,17 @@ function isAuthenticated(req, res, next) {
}
}
}
}
router
.
use
((
req
,
res
,
next
)
=>
{
console
.
log
(
"
🔹 Session ID:
"
,
req
.
session
.
id
);
console
.
log
(
"
🔹 Session Data:
"
,
req
.
session
);
next
();
});
// แสดงประวัติออเดอร์ (เฉพาะผู้ที่ Login)
// แสดงประวัติออเดอร์ (เฉพาะผู้ที่ Login)
router
.
get
(
'
/history
'
,
isAuthenticated
,
async
(
req
,
res
)
=>
{
router
.
get
(
'
/history
'
,
isAuthenticated
,
async
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
id
)
{
return
res
.
status
(
400
).
json
({
message
:
"
Session ID not found. Please login again.
"
});
}
try
{
try
{
const
[
orders
]
=
await
pool
.
query
(
const
[
orders
]
=
await
pool
.
query
(
`SELECT
`SELECT
...
@@ -22,9 +31,8 @@ router.get('/history', isAuthenticated, async (req, res) => {
...
@@ -22,9 +31,8 @@ router.get('/history', isAuthenticated, async (req, res) => {
orders.shipping_address,
orders.shipping_address,
orders.created_at,
orders.created_at,
GROUP_CONCAT(
GROUP_CONCAT(
CONCAT(
CONCAT(products.name, ' x ', order_items.quantity, ' (฿', order_items.price, ')')
products.name, ' x ', order_items.quantity, ' (฿', order_items.price, ')'
SEPARATOR ', '
) SEPARATOR ', '
) as items_detail
) as items_detail
FROM orders
FROM orders
JOIN order_items ON orders.id = order_items.order_id
JOIN order_items ON orders.id = order_items.order_id
...
@@ -45,8 +53,12 @@ router.get('/history', isAuthenticated, async (req, res) => {
...
@@ -45,8 +53,12 @@ router.get('/history', isAuthenticated, async (req, res) => {
// แสดงรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login)
// แสดงรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login)
router
.
get
(
'
/detail/:orderId
'
,
isAuthenticated
,
async
(
req
,
res
)
=>
{
router
.
get
(
'
/detail/:orderId
'
,
isAuthenticated
,
async
(
req
,
res
)
=>
{
try
{
try
{
if
(
!
req
.
session
.
id
)
{
return
res
.
status
(
400
).
json
({
message
:
"
Session ID not found. Please login again.
"
});
}
const
{
orderId
}
=
req
.
params
;
const
{
orderId
}
=
req
.
params
;
console
.
log
(
`Fetching order
details for order
ID:
${
orderId
}
, Session ID:
${
req
.
session
.
id
}
`
);
console
.
log
(
`
🔹
Fetching order ID:
${
orderId
}
, Session ID:
${
req
.
session
.
id
}
`
);
const
[
orderResults
]
=
await
pool
.
query
(
const
[
orderResults
]
=
await
pool
.
query
(
`SELECT id, total_amount, status, shipping_address, created_at
`SELECT id, total_amount, status, shipping_address, created_at
...
@@ -55,23 +67,14 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
...
@@ -55,23 +67,14 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
);
);
if
(
orderResults
.
length
===
0
)
{
if
(
orderResults
.
length
===
0
)
{
console
.
log
(
"
Order not found or
user has
no permission.
"
);
console
.
log
(
"
Order not found or no permission.
"
);
return
res
.
status
(
404
).
json
({
message
:
"
ไม่พบคำสั่งซื้อ หรือไม่มีสิทธิ์เข้าถึง
"
});
return
res
.
status
(
404
).
json
({
message
:
"
ไม่พบคำสั่งซื้อ หรือไม่มีสิทธิ์เข้าถึง
"
});
}
}
const
order
=
orderResults
[
0
];
console
.
log
(
"
Order found:
"
,
orderResults
[
0
]);
res
.
json
(
orderResults
[
0
]);
const
[
items
]
=
await
pool
.
query
(
`SELECT products.name, products.image_url, order_items.quantity, order_items.price
FROM order_items
JOIN products ON order_items.product_id = products.id
WHERE order_items.order_id = ?`
,
[
orderId
]
);
res
.
render
(
'
order-detail
'
,
{
order
,
items
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"
E
RROR
fetching order
details
:
"
,
error
);
// ดู Error Log
console
.
error
(
"
E
rror
fetching order:
"
,
error
);
res
.
status
(
500
).
json
({
message
:
"
Something went wrong.
"
,
error
:
error
.
message
});
res
.
status
(
500
).
json
({
message
:
"
Something went wrong.
"
,
error
:
error
.
message
});
}
}
});
});
...
@@ -132,6 +135,12 @@ router.get('/checkout', isAuthenticated, async (req, res) => {
...
@@ -132,6 +135,12 @@ router.get('/checkout', isAuthenticated, async (req, res) => {
// บันทึกออเดอร์
// บันทึกออเดอร์
router
.
post
(
'
/create
'
,
isAuthenticated
,
async
(
req
,
res
)
=>
{
router
.
post
(
'
/create
'
,
isAuthenticated
,
async
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
id
)
{
return
res
.
status
(
400
).
json
({
message
:
"
Session ID not found. Please login again.
"
});
}
console
.
log
(
"
🔹 Creating order with Session ID:
"
,
req
.
session
.
id
);
const
{
address
}
=
req
.
body
;
const
{
address
}
=
req
.
body
;
const
conn
=
await
pool
.
getConnection
();
const
conn
=
await
pool
.
getConnection
();
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