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
235daa25
Commit
235daa25
authored
2 months ago
by
65160381
Browse files
Options
Downloads
Patches
Plain Diff
9.8
parent
a7f9072a
No related branches found
No related tags found
No related merge requests found
Pipeline
#625
passed with warnings
2 months ago
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app.js
+5
-6
5 additions, 6 deletions
app.js
public/index.html
+24
-26
24 additions, 26 deletions
public/index.html
with
29 additions
and
32 deletions
app.js
+
5
−
6
View file @
235daa25
...
...
@@ -166,13 +166,12 @@ app.get('/api/products', async (req, res) => {
});
app
.
get
(
'
/api/getUser
'
,
(
req
,
res
)
=>
{
if
(
req
.
session
.
user
)
{
res
.
json
({
email
:
req
.
session
.
user
.
email
// ส่งข้อมูล email จาก session
});
}
else
{
res
.
status
(
401
).
send
(
'
User not logged in
'
);
if
(
!
req
.
session
.
user
)
{
// ตรวจสอบว่า user ได้ล็อกอินหรือยัง
return
res
.
status
(
401
).
send
(
'
User not logged in
'
);
}
const
user
=
req
.
session
.
user
;
// สมมติว่าใช้ session ในการจัดการการล็อกอิน
res
.
json
({
email
:
user
.
email
});
// ส่งกลับอีเมลของผู้ใช้
});
// Fetch products of logged-in user
...
...
This diff is collapsed.
Click to expand it.
public/index.html
+
24
−
26
View file @
235daa25
...
...
@@ -40,24 +40,23 @@
</footer>
<script>
//
Fetch and display products
//
ฟังก์ชันสำหรับดึงสินค้า
async
function
fetchProducts
(
searchQuery
=
''
)
{
try
{
const
response
=
await
fetch
(
`/api/products?search=
${
searchQuery
}
`
);
const
products
=
await
response
.
json
();
console
.
log
(
'
Fetched Products:
'
,
products
);
// เพิ่มการตรวจสอบข้อมูล
renderProducts
(
products
);
// เรียกฟังก์ชัน renderProducts เพื่อนำข้อมูลไปแสดง
renderProducts
(
products
);
}
catch
(
error
)
{
console
.
error
(
'
Error fetching products:
'
,
error
);
}
}
// ฟังก์ชัน
ที่ใช้
ในการแสดงสินค้าบนหน้าเว็บ
// ฟังก์ชันในการแสดงสินค้าบนหน้าเว็บ
function
renderProducts
(
products
)
{
const
productContainer
=
document
.
querySelector
(
'
.products
'
);
productContainer
.
innerHTML
=
''
;
// เคลียร์รายการสินค้าก่อน
productContainer
.
innerHTML
=
''
;
// วนลูปแสดงข้อมูลสินค้า
products
.
forEach
(
product
=>
{
const
productDiv
=
document
.
createElement
(
'
div
'
);
productDiv
.
classList
.
add
(
'
product
'
);
...
...
@@ -77,7 +76,7 @@
fetchProducts
(
searchQuery
);
}
// ฟังก์ชัน
ที่
เพิ่มสินค้าไปยังตะกร้า
// ฟังก์ชัน
ในการ
เพิ่มสินค้าไปยังตะกร้า
function
addToCart
(
productId
,
productName
,
productPrice
,
productImg
)
{
let
cart
=
JSON
.
parse
(
localStorage
.
getItem
(
'
cart
'
))
||
[];
const
existingItem
=
cart
.
find
(
item
=>
item
.
id
===
productId
);
...
...
@@ -99,36 +98,35 @@
alert
(
`
${
productName
}
added to cart!`
);
}
// ฟังก์ชันอัปเดตจำนวนสินค้าที่อยู่ในตะกร้า
function
updateCart
()
{
let
cart
=
JSON
.
parse
(
localStorage
.
getItem
(
'
cart
'
))
||
[];
const
cartItemCount
=
document
.
getElementById
(
'
cart-item-count
'
);
const
totalQuantity
=
cart
.
reduce
((
sum
,
item
)
=>
sum
+
item
.
quantity
,
0
);
cartItemCount
.
textContent
=
totalQuantity
;
}
// ฟังก์ชันแสดงข้อมูลผู้ใช้ (อีเมล)
async
function
displayUsername
()
{
try
{
const
response
=
await
fetch
(
'
/api/getUser
'
);
// Endpoint
to get the user's email
const
response
=
await
fetch
(
'
/api/getUser
'
);
// Endpoint
ที่จะดึงข้อมูลอีเมลของผู้ใช้
if
(
response
.
status
===
401
)
{
console
.
log
(
'
User not logged in
'
);
document
.
getElementById
(
'
welcome-message
'
).
innerText
=
'
Please log in
'
;
}
else
{
const
user
=
await
response
.
json
();
document
.
getElementById
(
'
welcome-message
'
).
innerText
=
`Welcome,
${
user
.
email
}
`
;
//
Display email here
document
.
getElementById
(
'
welcome-message
'
).
innerText
=
`Welcome,
${
user
.
email
}
`
;
//
แสดงอีเมลของผู้ใช้
}
}
catch
(
error
)
{
console
.
log
(
'
Error fetching user info:
'
,
error
);
}
}
// เรียกใช้ฟังก์ชันเมื่อหน้าเว็บโหลดเสร็จ
window
.
onload
=
displayUsername
;
// ฟังก์ชันอัปเดตจำนวนสินค้าที่อยู่ในตะกร้า
function
updateCart
()
{
let
cart
=
JSON
.
parse
(
localStorage
.
getItem
(
'
cart
'
))
||
[];
const
cartItemCount
=
document
.
getElementById
(
'
cart-item-count
'
);
const
totalQuantity
=
cart
.
reduce
((
sum
,
item
)
=>
sum
+
item
.
quantity
,
0
);
cartItemCount
.
textContent
=
totalQuantity
;
}
// เรียกใช้ฟังก์ชันเมื่อหน้าเว็บโหลดเสร็จ
window
.
onload
=
function
()
{
fetchProducts
();
// ดึงข้อมูลสินค้าทั้งหมด
updateCart
();
// อัปเดตจำนวนสินค้าในตะกร้า
displayUsername
();
fetchProducts
();
// Fetch all products initially
updateCart
();
// Update cart count when page loads
};
</script>
</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