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
4a5cf9c5
Commit
4a5cf9c5
authored
3 weeks ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-cart
parent
ac519176
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
shop-routes/cart.js
+14
-24
14 additions, 24 deletions
shop-routes/cart.js
views/product_add.ejs
+3
-0
3 additions, 0 deletions
views/product_add.ejs
with
17 additions
and
24 deletions
shop-routes/cart.js
+
14
−
24
View file @
4a5cf9c5
...
@@ -34,13 +34,6 @@ router.get('/', async (req, res) => {
...
@@ -34,13 +34,6 @@ router.get('/', async (req, res) => {
// เพิ่มสินค้าลงตะกร้า
// เพิ่มสินค้าลงตะกร้า
router
.
post
(
'
/add
'
,
async
(
req
,
res
)
=>
{
router
.
post
(
'
/add
'
,
async
(
req
,
res
)
=>
{
console
.
log
(
"
Session User:
"
,
req
.
session
.
user
);
console
.
log
(
"
Received Data:
"
,
req
.
body
);
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
Unauthorized
'
);
}
const
{
productId
,
quantity
}
=
req
.
body
;
const
{
productId
,
quantity
}
=
req
.
body
;
try
{
try
{
const
[[
product
]]
=
await
pool
.
query
(
const
[[
product
]]
=
await
pool
.
query
(
...
@@ -55,12 +48,14 @@ router.post('/add', async (req, res) => {
...
@@ -55,12 +48,14 @@ router.post('/add', async (req, res) => {
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
}
}
// ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง
const
[[
existingItem
]]
=
await
pool
.
query
(
const
[[
existingItem
]]
=
await
pool
.
query
(
'
SELECT id, quantity FROM cart_items WHERE
u
se
r
_id = ? AND product_id = ?
'
,
'
SELECT id, quantity FROM cart_items WHERE se
ssion
_id = ? AND product_id = ?
'
,
[
req
.
session
.
user
.
id
,
productId
]
[
req
.
session
.
id
,
productId
]
);
);
if
(
existingItem
)
{
if
(
existingItem
)
{
// อัปเดตจำนวนสินค้า
const
newQuantity
=
existingItem
.
quantity
+
parseInt
(
quantity
);
const
newQuantity
=
existingItem
.
quantity
+
parseInt
(
quantity
);
if
(
newQuantity
>
product
.
stock
)
{
if
(
newQuantity
>
product
.
stock
)
{
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
return
res
.
status
(
400
).
send
(
'
สินค้ามีจำนวนไม่เพียงพอ
'
);
...
@@ -70,35 +65,31 @@ router.post('/add', async (req, res) => {
...
@@ -70,35 +65,31 @@ router.post('/add', async (req, res) => {
[
newQuantity
,
existingItem
.
id
]
[
newQuantity
,
existingItem
.
id
]
);
);
}
else
{
}
else
{
// เพิ่มสินค้าใหม่
await
pool
.
query
(
await
pool
.
query
(
'
INSERT INTO cart_items (
u
se
r
_id, product_id, quantity) VALUES (?, ?, ?)
'
,
'
INSERT INTO cart_items (se
ssion
_id, product_id, quantity) VALUES (?, ?, ?)
'
,
[
req
.
session
.
user
.
id
,
productId
,
parseInt
(
quantity
)]
[
req
.
session
.
id
,
productId
,
parseInt
(
quantity
)]
);
);
}
}
res
.
redirect
(
'
/cart
'
);
res
.
redirect
(
'
/cart
'
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"
Error adding to cart:
"
,
error
);
console
.
error
(
error
);
res
.
status
(
500
).
send
(
'
Error adding to cart
.
'
);
res
.
status
(
500
).
send
(
'
Error adding to cart
'
);
}
}
});
});
// อัพเดทจำนวนสินค้าในตะกร้า
// อัพเดทจำนวนสินค้าในตะกร้า
router
.
post
(
'
/update
'
,
async
(
req
,
res
)
=>
{
router
.
post
(
'
/update
'
,
async
(
req
,
res
)
=>
{
console
.
log
(
"
Received Data:
"
,
req
.
body
);
console
.
log
(
"
Received Data:
"
,
req
.
body
);
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
Unauthorized
'
);
}
const
{
cartItemId
,
quantity
}
=
req
.
body
;
const
{
cartItemId
,
quantity
}
=
req
.
body
;
try
{
try
{
const
[[
cartItem
]]
=
await
pool
.
query
(
const
[[
cartItem
]]
=
await
pool
.
query
(
`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.
u
se
r
_id = ?`
,
WHERE cart_items.id = ? AND cart_items.se
ssion
_id = ?`
,
[
cartItemId
,
req
.
session
.
user
.
id
]
// ใช้ user_
id
[
cartItemId
,
req
.
session
.
id
]
);
);
if
(
!
cartItem
)
{
if
(
!
cartItem
)
{
...
@@ -110,11 +101,11 @@ router.post('/update', async (req, res) => {
...
@@ -110,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
);
...
@@ -122,7 +113,6 @@ router.post('/update', async (req, res) => {
...
@@ -122,7 +113,6 @@ router.post('/update', async (req, res) => {
}
}
});
});
// ลบสินค้าออกจากตะกร้า
// ลบสินค้าออกจากตะกร้า
router
.
post
(
'
/remove
'
,
async
(
req
,
res
)
=>
{
router
.
post
(
'
/remove
'
,
async
(
req
,
res
)
=>
{
const
{
cartItemId
}
=
req
.
body
;
const
{
cartItemId
}
=
req
.
body
;
...
...
This diff is collapsed.
Click to expand it.
views/product_add.ejs
+
3
−
0
View file @
4a5cf9c5
...
@@ -16,6 +16,9 @@
...
@@ -16,6 +16,9 @@
<label>
จำนวนสินค้า:
</label>
<label>
จำนวนสินค้า:
</label>
<input
type=
"number"
name=
"stock"
required
>
<input
type=
"number"
name=
"stock"
required
>
<br>
<br>
<label>
คำบรรยายสินค้า:
</label>
<textarea
name=
"description"
required
><
%=
product.description
%
></textarea>
<br>
<label>
เลือกไฟล์รูปสินค้า:
</label>
<label>
เลือกไฟล์รูปสินค้า:
</label>
<input
type=
"file"
name=
"image"
required
>
<input
type=
"file"
name=
"image"
required
>
<br>
<br>
...
...
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