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
78405a3d
Commit
78405a3d
authored
1 month ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
e90ebba2
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/cart.js
+13
-17
13 additions, 17 deletions
shop-routes/cart.js
with
13 additions
and
17 deletions
shop-routes/cart.js
+
13
−
17
View file @
78405a3d
...
...
@@ -3,13 +3,13 @@ const router = express.Router();
const
pool
=
require
(
'
../config/database
'
);
// ฟังก์ชันช่วยคำนวณราคารวม
async
function
calculateTotal
(
u
se
r
Id
)
{
async
function
calculateTotal
(
se
ssion
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.
u
se
r
_id = ?`
,
[
u
se
r
Id
]
WHERE cart_items.se
ssion
_id = ?`
,
[
se
ssion
Id
]
);
return
cartItems
.
reduce
((
sum
,
item
)
=>
sum
+
(
item
.
price
*
item
.
quantity
),
0
);
}
...
...
@@ -34,14 +34,7 @@ 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
]
...
...
@@ -55,13 +48,14 @@ router.post('/add', async (req, res) => {
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
}
// ตรวจสอบว่า
มี
สินค้าในตะกร้า
อยู่แล้ว
หรือ
ไม่
// ตรวจสอบว่าสินค้า
นี้มีอยู่
ในตะกร้าหรือ
ยัง
const
[[
existingItem
]]
=
await
pool
.
query
(
'
SELECT id, quantity FROM cart_items WHERE
u
se
r
_id = ? AND product_id = ?
'
,
[
req
.
session
.
user
.
id
,
productId
]
'
SELECT id, quantity FROM cart_items WHERE se
ssion
_id = ? AND product_id = ?
'
,
[
req
.
session
.
id
,
productId
]
);
if
(
existingItem
)
{
// อัปเดตจำนวนสินค้า
const
newQuantity
=
existingItem
.
quantity
+
parseInt
(
quantity
);
if
(
newQuantity
>
product
.
stock
)
{
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
...
...
@@ -71,16 +65,17 @@ router.post('/add', async (req, res) => {
[
newQuantity
,
existingItem
.
id
]
);
}
else
{
// เพิ่มสินค้าใหม่
await
pool
.
query
(
'
INSERT INTO cart_items (
u
se
r
_id, product_id, quantity) VALUES (?, ?, ?)
'
,
[
req
.
session
.
user
.
id
,
productId
,
parseInt
(
quantity
)]
'
INSERT INTO cart_items (se
ssion
_id, product_id, quantity) VALUES (?, ?, ?)
'
,
[
req
.
session
.
id
,
productId
,
parseInt
(
quantity
)]
);
}
res
.
redirect
(
'
/cart
'
);
}
catch
(
error
)
{
console
.
error
(
"
Error adding to cart:
"
,
error
);
res
.
status
(
500
).
send
(
'
Error adding to cart
.
'
);
console
.
error
(
error
);
res
.
status
(
500
).
send
(
'
Error adding to cart
'
);
}
});
...
...
@@ -122,6 +117,7 @@ router.post('/update', async (req, res) => {
}
});
// ลบสินค้าออกจากตะกร้า
router
.
post
(
'
/remove
'
,
async
(
req
,
res
)
=>
{
const
{
cartItemId
}
=
req
.
body
;
...
...
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