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
58df9a63
Commit
58df9a63
authored
2 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
bf3e439e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
shop-routes/cart.js
+16
-17
16 additions, 17 deletions
shop-routes/cart.js
with
16 additions
and
17 deletions
shop-routes/cart.js
+
16
−
17
View file @
58df9a63
...
@@ -3,13 +3,13 @@ const router = express.Router();
...
@@ -3,13 +3,13 @@ const router = express.Router();
const
pool
=
require
(
'
../config/database
'
);
const
pool
=
require
(
'
../config/database
'
);
// ฟังก์ชันช่วยคำนวณราคารวม
// ฟังก์ชันช่วยคำนวณราคารวม
async
function
calculateTotal
(
u
se
r
Id
)
{
async
function
calculateTotal
(
se
ssion
Id
)
{
const
[
cartItems
]
=
await
pool
.
query
(
const
[
cartItems
]
=
await
pool
.
query
(
`SELECT cart_items.*, products.price
`SELECT cart_items.*, products.price
FROM cart_items
FROM cart_items
JOIN products ON cart_items.product_id = products.id
JOIN products ON cart_items.product_id = products.id
WHERE cart_items.
user_id = ?`
,
// เปลี่ยนจาก session_id เป็น user_id
WHERE cart_items.
session_id = ?`
,
[
u
se
r
Id
]
[
se
ssion
Id
]
);
);
return
cartItems
.
reduce
((
sum
,
item
)
=>
sum
+
(
item
.
price
*
item
.
quantity
),
0
);
return
cartItems
.
reduce
((
sum
,
item
)
=>
sum
+
(
item
.
price
*
item
.
quantity
),
0
);
}
}
...
@@ -21,10 +21,10 @@ router.get('/', async (req, res) => {
...
@@ -21,10 +21,10 @@ router.get('/', async (req, res) => {
`SELECT cart_items.*, products.name, products.price, products.stock
`SELECT cart_items.*, products.name, products.price, products.stock
FROM cart_items
FROM cart_items
JOIN products ON cart_items.product_id = products.id
JOIN products ON cart_items.product_id = products.id
WHERE
user_id = ?`
,
// เปลี่ยนจาก session_id เป็น user_id
WHERE
session_id = ?`
,
[
req
.
session
.
user
.
id
]
[
req
.
session
.
id
]
);
);
const
total
=
await
calculateTotal
(
req
.
session
.
user
.
id
);
const
total
=
await
calculateTotal
(
req
.
session
.
id
);
res
.
render
(
'
cart
'
,
{
cartItems
,
total
});
res
.
render
(
'
cart
'
,
{
cartItems
,
total
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
error
);
console
.
error
(
error
);
...
@@ -32,7 +32,6 @@ router.get('/', async (req, res) => {
...
@@ -32,7 +32,6 @@ router.get('/', async (req, res) => {
}
}
});
});
// เพิ่มสินค้าลงตะกร้า
// เพิ่มสินค้าลงตะกร้า
// เพิ่มสินค้าลงตะกร้า
router
.
post
(
'
/add
'
,
async
(
req
,
res
)
=>
{
router
.
post
(
'
/add
'
,
async
(
req
,
res
)
=>
{
const
{
productId
,
quantity
}
=
req
.
body
;
const
{
productId
,
quantity
}
=
req
.
body
;
...
@@ -52,7 +51,7 @@ router.post('/add', async (req, res) => {
...
@@ -52,7 +51,7 @@ router.post('/add', async (req, res) => {
// ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง
// ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง
const
[[
existingItem
]]
=
await
pool
.
query
(
const
[[
existingItem
]]
=
await
pool
.
query
(
'
SELECT id, quantity FROM cart_items WHERE session_id = ? AND product_id = ?
'
,
'
SELECT id, quantity FROM cart_items WHERE session_id = ? AND product_id = ?
'
,
[
req
.
session
.
user
.
id
,
productId
]
[
req
.
session
.
id
,
productId
]
);
);
if
(
existingItem
)
{
if
(
existingItem
)
{
...
@@ -89,8 +88,8 @@ router.post('/update', async (req, res) => {
...
@@ -89,8 +88,8 @@ router.post('/update', async (req, res) => {
`SELECT cart_items.*, products.price, products.stock
`SELECT cart_items.*, products.price, products.stock
FROM cart_items
FROM cart_items
JOIN products ON cart_items.product_id = products.id
JOIN products ON cart_items.product_id = products.id
WHERE cart_items.id = ? AND cart_items.
user_id = ?`
,
// เปลี่ยนจาก session_id เป็น user_id
WHERE cart_items.id = ? AND cart_items.
session_id = ?`
,
[
cartItemId
,
req
.
session
.
user
.
id
]
[
cartItemId
,
req
.
session
.
id
]
);
);
if
(
!
cartItem
)
{
if
(
!
cartItem
)
{
...
@@ -102,11 +101,11 @@ router.post('/update', async (req, res) => {
...
@@ -102,11 +101,11 @@ router.post('/update', async (req, res) => {
}
}
await
pool
.
query
(
await
pool
.
query
(
'
UPDATE cart_items SET quantity = ? WHERE id = ? AND
u
se
r
_id = ?
'
,
'
UPDATE cart_items SET quantity = ? WHERE id = ? AND se
ssion
_id = ?
'
,
[
parseInt
(
quantity
),
cartItemId
,
req
.
session
.
user
.
id
]
[
parseInt
(
quantity
),
cartItemId
,
req
.
session
.
id
]
);
);
const
total
=
await
calculateTotal
(
req
.
session
.
user
.
id
);
const
total
=
await
calculateTotal
(
req
.
session
.
id
);
res
.
json
({
total
});
res
.
json
({
total
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
error
);
console
.
error
(
error
);
...
@@ -119,8 +118,8 @@ router.post('/remove', async (req, res) => {
...
@@ -119,8 +118,8 @@ router.post('/remove', async (req, res) => {
const
{
cartItemId
}
=
req
.
body
;
const
{
cartItemId
}
=
req
.
body
;
try
{
try
{
await
pool
.
query
(
await
pool
.
query
(
'
DELETE FROM cart_items WHERE id = ? AND
u
se
r
_id = ?
'
,
'
DELETE FROM cart_items WHERE id = ? AND se
ssion
_id = ?
'
,
[
cartItemId
,
req
.
session
.
user
.
id
]
// ใช้ user_id แทน session_
id
[
cartItemId
,
req
.
session
.
id
]
);
);
res
.
redirect
(
'
/cart
'
);
res
.
redirect
(
'
/cart
'
);
}
catch
(
error
)
{
}
catch
(
error
)
{
...
@@ -131,9 +130,9 @@ router.post('/remove', async (req, res) => {
...
@@ -131,9 +130,9 @@ router.post('/remove', async (req, res) => {
router
.
get
(
'
/cart
'
,
(
req
,
res
)
=>
{
router
.
get
(
'
/cart
'
,
(
req
,
res
)
=>
{
const
cartItems
=
req
.
session
.
cart
||
[];
const
cartItems
=
req
.
session
.
cart
||
[];
const
editingItemId
=
req
.
session
.
editingItemId
||
null
;
const
editingItemId
=
req
.
session
.
editingItemId
||
null
;
// ถ้าไม่มีการแก้ไขก็เป็น null
res
.
render
(
'
cart
'
,
{
cartItems
,
total
:
calculateTotal
(
cartItems
),
editingItemId
});
res
.
render
(
'
cart
'
,
{
cartItems
,
total
:
calculateTotal
(
cartItems
),
editingItemId
});
});
});
module
.
exports
=
router
;
module
.
exports
=
router
;
\ No newline at end of file
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