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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160381
project-melon
Commits
9b9f1fee
Commit
9b9f1fee
authored
3 months ago
by
65160381
Browse files
Options
Downloads
Patches
Plain Diff
8.4
parent
d0f51468
No related branches found
No related tags found
No related merge requests found
Pipeline
#605
passed with warnings
3 months ago
Stage: test
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app.js
+70
-0
70 additions, 0 deletions
app.js
public/cart.html
+81
-0
81 additions, 0 deletions
public/cart.html
public/profile.html
+54
-0
54 additions, 0 deletions
public/profile.html
with
205 additions
and
0 deletions
app.js
+
70
−
0
View file @
9b9f1fee
...
...
@@ -169,6 +169,76 @@ app.post('/login', async (req, res) => {
}
});
// เพิ่มสินค้าใหม่
app
.
get
(
'
/api/user/products
'
,
async
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
User not logged in
'
);
}
const
userId
=
req
.
session
.
user
.
id
;
// ใช้ user_id จาก session
try
{
const
connection
=
await
pool
.
getConnection
();
const
[
products
]
=
await
connection
.
query
(
'
SELECT * FROM products WHERE user_id = ?
'
,
[
userId
]
);
connection
.
release
();
res
.
json
(
products
);
}
catch
(
err
)
{
console
.
error
(
'
Error fetching user products:
'
,
err
);
res
.
status
(
500
).
send
(
'
Error fetching user products
'
);
}
});
// เพิ่มสินค้าใหม่
app
.
post
(
'
/api/products
'
,
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
User not logged in
'
);
}
const
{
productName
,
productPrice
,
productImg
}
=
req
.
body
;
const
userId
=
req
.
session
.
user
.
id
;
// user_id จาก session
pool
.
query
(
`
INSERT INTO products (product_name, product_price, product_img, user_id)
VALUES (?, ?, ?, ?)
`
,
[
productName
,
productPrice
,
productImg
,
userId
],
(
err
,
results
)
=>
{
if
(
err
)
{
return
res
.
status
(
500
).
send
(
'
Error adding product
'
);
}
res
.
send
(
'
Product added successfully
'
);
});
});
// ดึงสินค้าของผู้ใช้
app
.
get
(
'
/api/user/products
'
,
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
user
)
{
return
res
.
status
(
401
).
send
(
'
User not logged in
'
);
}
const
userId
=
req
.
session
.
user
.
id
;
pool
.
query
(
`
SELECT * FROM products WHERE user_id = ?
`
,
[
userId
],
(
err
,
results
)
=>
{
if
(
err
)
{
return
res
.
status
(
500
).
send
(
'
Error fetching user products
'
);
}
res
.
json
(
results
);
});
});
// ดึงสินค้าทั้งหมด
app
.
get
(
'
/api/products
'
,
(
req
,
res
)
=>
{
pool
.
query
(
'
SELECT * FROM products
'
,
(
err
,
results
)
=>
{
if
(
err
)
{
return
res
.
status
(
500
).
send
(
'
Error fetching products
'
);
}
res
.
json
(
results
);
});
});
app
.
use
(
express
.
json
());
// สำหรับการ parse ข้อมูลแบบ JSON
app
.
use
(
express
.
urlencoded
({
extended
:
true
}));
// สำหรับการ parse ข้อมูลแบบ URL-encoded
...
...
This diff is collapsed.
Click to expand it.
public/cart.html
0 → 100644
+
81
−
0
View file @
9b9f1fee
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<title>
Cart ASA
</title>
<link
rel=
"stylesheet"
href=
"styles/cart.css"
>
</head>
<body>
<header>
<h1>
ASA Cart
</h1>
</header>
<div
class=
"cart-items"
id=
"cart-items"
>
<!-- Cart items will be dynamically loaded here -->
</div>
<footer>
<p>
©
2025 ASA
</p>
<div
class=
"checkout"
onclick=
"goToCheckout()"
>
Checkout
</div>
</footer>
<script>
// Load cart items from the API
async
function
loadCart
()
{
try
{
const
response
=
await
fetch
(
'
/api/cart
'
);
const
cart
=
await
response
.
json
();
const
cartItemsContainer
=
document
.
getElementById
(
'
cart-items
'
);
cartItemsContainer
.
innerHTML
=
''
;
if
(
cart
.
length
===
0
)
{
cartItemsContainer
.
innerHTML
=
'
<p>Your cart is empty!</p>
'
;
}
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
)
{
console
.
error
(
'
Error loading cart items:
'
,
error
);
}
}
async
function
updateQuantity
(
productId
,
newQuantity
)
{
await
fetch
(
`/api/cart/
${
productId
}
`
,
{
method
:
'
PUT
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
body
:
JSON
.
stringify
({
quantity
:
newQuantity
})
});
loadCart
();
// Reload the cart after updating
}
async
function
removeFromCart
(
productId
)
{
await
fetch
(
`/api/cart/
${
productId
}
`
,
{
method
:
'
DELETE
'
});
loadCart
();
// Reload the cart after removal
}
function
goToCheckout
()
{
window
.
location
.
href
=
'
checkout.html
'
;
}
// Load cart items when page loads
loadCart
();
</script>
</body>
</html>
This diff is collapsed.
Click to expand it.
public/profile.html
0 → 100644
+
54
−
0
View file @
9b9f1fee
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<title>
Your Products
</title>
<link
rel=
"stylesheet"
href=
"styles/home.css"
>
</head>
<body>
<header>
<h1>
Your Products
</h1>
</header>
<div
class=
"products"
id=
"user-products"
>
<!-- User's products will be dynamically loaded here -->
</div>
<footer>
<p>
©
2025 ASA
</p>
</footer>
<script>
// Function to load products posted by the logged-in user
async
function
loadUserProducts
()
{
try
{
const
response
=
await
fetch
(
'
/api/user/products
'
);
const
products
=
await
response
.
json
();
const
productContainer
=
document
.
getElementById
(
'
user-products
'
);
productContainer
.
innerHTML
=
''
;
// Clear the container
if
(
products
.
length
===
0
)
{
productContainer
.
innerHTML
=
'
<p>You have no products listed.</p>
'
;
}
products
.
forEach
(
product
=>
{
const
productDiv
=
document
.
createElement
(
'
div
'
);
productDiv
.
classList
.
add
(
'
product
'
);
productDiv
.
innerHTML
=
`
<img src="
${
product
.
product_img
}
" alt="
${
product
.
product_name
}
">
<h3>
${
product
.
product_name
}
</h3>
<p>Price: $
${
product
.
product_price
}
</p>
`
;
productContainer
.
appendChild
(
productDiv
);
});
}
catch
(
error
)
{
console
.
error
(
'
Error loading user products:
'
,
error
);
}
}
// Call function to load user products when page loads
loadUserProducts
();
</script>
</body>
</html>
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