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
e90ebba2
Commit
e90ebba2
authored
2 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
16342ded
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
-2
2 additions, 2 deletions
server.js
shop-routes/cart.js
+23
-14
23 additions, 14 deletions
shop-routes/cart.js
with
25 additions
and
16 deletions
server.js
+
2
−
2
View file @
e90ebba2
...
...
@@ -23,7 +23,7 @@ app.use(session({
cookie
:
{
maxAge
:
24
*
60
*
60
*
1000
,
secure
:
false
,
// เปลี่ยนเป็น true ถ้าใช้ HTTPS
httpOnly
:
tru
e
// เปลี่ยนจาก true เป็น false เพื่อลองดูว่า cookie เก็บค่าหรือไม่
httpOnly
:
fals
e
// เปลี่ยนจาก true เป็น false เพื่อลองดูว่า cookie เก็บค่าหรือไม่
}
}));
...
...
@@ -33,7 +33,7 @@ app.use((req, res, next) => {
});
console
.
log
(
"
✅
Session Middleware Initialized
"
);
console
.
log
(
"
Session Middleware Initialized
"
);
// Middleware
app
.
use
(
express
.
static
(
path
.
join
(
__dirname
,
"
public
"
)));
...
...
This diff is collapsed.
Click to expand it.
shop-routes/cart.js
+
23
−
14
View file @
e90ebba2
...
...
@@ -3,13 +3,13 @@ const router = express.Router();
const
pool
=
require
(
'
../config/database
'
);
// ฟังก์ชันช่วยคำนวณราคารวม
async
function
calculateTotal
(
se
ssion
Id
)
{
async
function
calculateTotal
(
u
se
r
Id
)
{
const
[
cartItems
]
=
await
pool
.
query
(
`SELECT cart_items.*, products.price
FROM cart_items
JOIN products ON cart_items.product_id = products.id
WHERE cart_items.se
ssion
_id = ?`
,
[
se
ssion
Id
]
WHERE cart_items.
u
se
r
_id = ?`
,
[
u
se
r
Id
]
);
return
cartItems
.
reduce
((
sum
,
item
)
=>
sum
+
(
item
.
price
*
item
.
quantity
),
0
);
}
...
...
@@ -34,7 +34,14 @@ router.get('/', async (req, res) => {
// เพิ่มสินค้าลงตะกร้า
router
.
post
(
'
/add
'
,
async
(
req
,
res
)
=>
{
console
.
log
(
"
Session User:
"
,
req
.
session
.
user
);
// ดูค่า session
console
.
log
(
"
Received Data:
"
,
req
.
body
);
// ดูค่าที่ส่งมา
const
{
productId
,
quantity
}
=
req
.
body
;
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
Unauthorized
'
);
}
try
{
const
[[
product
]]
=
await
pool
.
query
(
'
SELECT stock FROM products WHERE id = ?
'
,
[
productId
]
...
...
@@ -48,14 +55,13 @@ router.post('/add', async (req, res) => {
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
}
// ตรวจสอบว่าสินค้า
นี้มีอยู่
ในตะกร้าหรือ
ยัง
// ตรวจสอบว่า
มี
สินค้าในตะกร้า
อยู่แล้ว
หรือ
ไม่
const
[[
existingItem
]]
=
await
pool
.
query
(
'
SELECT id, quantity FROM cart_items WHERE se
ssion
_id = ? AND product_id = ?
'
,
[
req
.
session
.
id
,
productId
]
'
SELECT id, quantity FROM cart_items WHERE
u
se
r
_id = ? AND product_id = ?
'
,
[
req
.
session
.
user
.
id
,
productId
]
);
if
(
existingItem
)
{
// อัปเดตจำนวนสินค้า
const
newQuantity
=
existingItem
.
quantity
+
parseInt
(
quantity
);
if
(
newQuantity
>
product
.
stock
)
{
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
...
...
@@ -65,31 +71,34 @@ router.post('/add', async (req, res) => {
[
newQuantity
,
existingItem
.
id
]
);
}
else
{
// เพิ่มสินค้าใหม่
await
pool
.
query
(
'
INSERT INTO cart_items (se
ssion
_id, product_id, quantity) VALUES (?, ?, ?)
'
,
[
req
.
session
.
id
,
productId
,
parseInt
(
quantity
)]
'
INSERT INTO cart_items (
u
se
r
_id, product_id, quantity) VALUES (?, ?, ?)
'
,
[
req
.
session
.
user
.
id
,
productId
,
parseInt
(
quantity
)]
);
}
res
.
redirect
(
'
/cart
'
);
}
catch
(
error
)
{
console
.
error
(
error
);
res
.
status
(
500
).
send
(
'
Error adding to cart
'
);
console
.
error
(
"
Error adding to cart:
"
,
error
);
res
.
status
(
500
).
send
(
'
Error adding to cart
.
'
);
}
});
// อัพเดทจำนวนสินค้าในตะกร้า
router
.
post
(
'
/update
'
,
async
(
req
,
res
)
=>
{
console
.
log
(
"
Received Data:
"
,
req
.
body
);
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
Unauthorized
'
);
}
const
{
cartItemId
,
quantity
}
=
req
.
body
;
try
{
const
[[
cartItem
]]
=
await
pool
.
query
(
`SELECT cart_items.*, products.price, products.stock
FROM cart_items
JOIN products ON cart_items.product_id = products.id
WHERE cart_items.id = ? AND cart_items.user_id = ?`
,
// เปลี่ยนจาก session_id เป็น user_id
[
cartItemId
,
req
.
session
.
user
.
id
]
WHERE cart_items.id = ? AND cart_items.user_id = ?`
,
[
cartItemId
,
req
.
session
.
user
.
id
]
// ใช้ user_id
);
if
(
!
cartItem
)
{
...
...
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