Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
project-melon
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
65160381
project-melon
Commits
b29dcc51
Commit
b29dcc51
authored
1 month ago
by
65160381
Browse files
Options
Downloads
Patches
Plain Diff
9.9
parent
9e9aea6b
No related branches found
No related tags found
No related merge requests found
Pipeline
#628
passed with warnings
1 month ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app.js
+30
-0
30 additions, 0 deletions
app.js
public/cart.html
+29
-27
29 additions, 27 deletions
public/cart.html
with
59 additions
and
27 deletions
app.js
+
30
−
0
View file @
b29dcc51
...
@@ -210,6 +210,36 @@ app.put('/api/cart/:productId', (req, res) => {
...
@@ -210,6 +210,36 @@ app.put('/api/cart/:productId', (req, res) => {
}
}
});
});
// API ที่ใช้ดึงข้อมูลสินค้าจากตะกร้า
app
.
get
(
'
/api/cart
'
,
(
req
,
res
)
=>
{
const
cart
=
req
.
session
.
cart
||
[];
// ใช้ session เก็บข้อมูลตะกร้า
console
.
log
(
'
Cart:
'
,
cart
);
// ตรวจสอบข้อมูลในตะกร้า
res
.
json
(
cart
);
});
// API ที่ใช้เพิ่มสินค้าลงในตะกร้า
app
.
post
(
'
/api/cart
'
,
(
req
,
res
)
=>
{
const
{
product_id
,
product_name
,
product_price
,
product_img
}
=
req
.
body
;
const
cart
=
req
.
session
.
cart
||
[];
const
existingItem
=
cart
.
find
(
item
=>
item
.
product_id
===
product_id
);
if
(
existingItem
)
{
existingItem
.
quantity
+=
1
;
// เพิ่มจำนวนสินค้าที่มีอยู่
}
else
{
cart
.
push
({
product_id
,
product_name
,
product_price
,
product_img
,
quantity
:
1
,
total_price
:
product_price
,
// คำนวณราคาทั้งหมด
});
}
req
.
session
.
cart
=
cart
;
// อัพเดทข้อมูลตะกร้าใน session
res
.
status
(
200
).
send
(
'
Item added to cart
'
);
});
// DELETE: ลบสินค้าจากตะกร้า
// DELETE: ลบสินค้าจากตะกร้า
app
.
delete
(
'
/api/cart/:productId
'
,
(
req
,
res
)
=>
{
app
.
delete
(
'
/api/cart/:productId
'
,
(
req
,
res
)
=>
{
const
productId
=
req
.
params
.
productId
;
const
productId
=
req
.
params
.
productId
;
...
...
This diff is collapsed.
Click to expand it.
public/cart.html
+
29
−
27
View file @
b29dcc51
...
@@ -24,42 +24,44 @@
...
@@ -24,42 +24,44 @@
</footer>
</footer>
<script>
<script>
//
ฟังก์ชันดึงข้อมูลสินค้าจากตะกร้า
//
Load cart items from the API
async
function
loadCart
()
{
async
function
loadCart
()
{
try
{
try
{
// ดึงข้อมูลจาก API ตะกร้าสินค้า
const
response
=
await
fetch
(
'
/api/cart
'
);
const
response
=
await
fetch
(
'
/api/cart
'
);
if
(
!
response
.
ok
)
{
throw
new
Error
(
'
Failed to fetch cart
'
);
}
const
cart
=
await
response
.
json
();
const
cart
=
await
response
.
json
();
const
cartItemsContainer
=
document
.
getElementById
(
'
cart-items
'
);
const
cartItemsContainer
=
document
.
getElementById
(
'
cart-items
'
);
cartItemsContainer
.
innerHTML
=
''
;
// เคลียร์ข้อมูลที่มีอยู่เดิม
cartItemsContainer
.
innerHTML
=
''
;
if
(
cart
.
length
===
0
)
{
if
(
cart
.
length
===
0
)
{
cartItemsContainer
.
innerHTML
=
'
<p>Your cart is empty!</p>
'
;
cartItemsContainer
.
innerHTML
=
'
<p>Your cart is empty!</p>
'
;
}
else
{
cart
.
forEach
(
item
=>
{
const
itemDiv
=
document
.
createElement
(
'
div
'
);
itemDiv
.
classList
.
add
(
'
cart-item
'
);
itemDiv
.
innerHTML
=
`
<img src="
${
item
.
product_img
}
" alt="
${
item
.
product_name
}
">
<div>
<h3>
${
item
.
product_name
}
</h3>
<p>Price: $
${
item
.
product_price
}
</p>
<p>Total Price: $<span id="total-price-
${
item
.
product_id
}
">
${
item
.
total_price
}
</span></p>
</div>
<div>
<input type="number" value="
${
item
.
quantity
}
" min="1" onchange="updateQuantity(
${
item
.
product_id
}
, this.value)">
<button onclick="removeFromCart(
${
item
.
product_id
}
)">Remove</button>
</div>
`
;
cartItemsContainer
.
appendChild
(
itemDiv
);
});
}
}
cart
.
forEach
(
item
=>
{
const
itemDiv
=
document
.
createElement
(
'
div
'
);
itemDiv
.
classList
.
add
(
'
cart-item
'
);
itemDiv
.
innerHTML
=
`
<img src="
${
item
.
product_img
}
" alt="
${
item
.
product_name
}
">
<div>
<h3>
${
item
.
product_name
}
</h3>
<p>Price: $
${
item
.
product_price
}
</p>
<p>Total Price: $<span id="total-price-
${
item
.
product_id
}
">
${
item
.
total_price
}
</span></p>
</div>
<div>
<input type="number" value="
${
item
.
quantity
}
" min="1" onchange="updateQuantity(
${
item
.
product_id
}
, this.value)">
<button onclick="removeFromCart(
${
item
.
product_id
}
)">Remove</button>
</div>
`
;
cartItemsContainer
.
appendChild
(
itemDiv
);
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Error loading cart items:
'
,
error
);
console
.
error
(
'
Error loading cart items:
'
,
error
);
}
}
}
}
//
ฟังก์ชันอัพเดทจำนวนสินค้าในตะกร้า
//
Update quantity in cart
async
function
updateQuantity
(
productId
,
newQuantity
)
{
async
function
updateQuantity
(
productId
,
newQuantity
)
{
await
fetch
(
`/api/cart/
${
productId
}
`
,
{
await
fetch
(
`/api/cart/
${
productId
}
`
,
{
method
:
'
PUT
'
,
method
:
'
PUT
'
,
...
@@ -67,21 +69,21 @@
...
@@ -67,21 +69,21 @@
body
:
JSON
.
stringify
({
quantity
:
newQuantity
})
body
:
JSON
.
stringify
({
quantity
:
newQuantity
})
});
});
loadCart
();
//
รีโหลดตะกร้าหลังจากอัพเดท
loadCart
();
//
Reload the cart after updating
}
}
//
ฟังก์ชันลบสินค้าจากตะกร้า
//
Remove item from cart
async
function
removeFromCart
(
productId
)
{
async
function
removeFromCart
(
productId
)
{
await
fetch
(
`/api/cart/
${
productId
}
`
,
{
method
:
'
DELETE
'
});
await
fetch
(
`/api/cart/
${
productId
}
`
,
{
method
:
'
DELETE
'
});
loadCart
();
//
รีโหลดตะกร้าหลังจากลบสินค้า
loadCart
();
//
Reload the cart after removal
}
}
//
ฟังก์ชันสำหรับไปที่หน้าชำระเงิน
//
Redirect to checkout page
function
goToCheckout
()
{
function
goToCheckout
()
{
window
.
location
.
href
=
'
checkout.html
'
;
window
.
location
.
href
=
'
checkout.html
'
;
}
}
//
โหลดข้อมูลสินค้าจากตะกร้าทันทีที่หน้าเว็บโหลด
//
Load cart items when page loads
loadCart
();
loadCart
();
</script>
</script>
</body>
</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