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
74faf970
Commit
74faf970
authored
1 month ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
Edit server.js
parent
bb7c6a64
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.js
+7
-7
7 additions, 7 deletions
server.js
with
7 additions
and
7 deletions
server.js
+
7
−
7
View file @
74faf970
...
...
@@ -23,7 +23,7 @@ app.use(session({
cookie
:
{
maxAge
:
24
*
60
*
60
*
1000
,
secure
:
false
,
// เปลี่ยนเป็น true ถ้าใช้ HTTPS
httpOnly
:
false
// เปลี่ยนจาก true เป็น false เพื่อลองดูว่า cookie เก็บค่า
หรือไม่
httpOnly
:
false
// เปลี่ยนจาก true เป็น false เพื่อลองดูว่า cookie เก็บค่า
}
}));
...
...
@@ -72,7 +72,7 @@ const isLoggedIn = (req, res, next) => {
}
};
//Register
app
.
get
(
'
/register
'
,
(
req
,
res
)
=>
{
res
.
render
(
'
register
'
);
});
...
...
@@ -143,7 +143,7 @@ app.get("/search", async (req, res) => {
app
.
get
(
"
/cart
"
,
isLoggedIn
,
async
(
req
,
res
)
=>
{
try
{
//
ดึงรายการสินค้าในตะกร้าจากฐานข้อมูล
//ดึงรายการสินค้าในตะกร้าจากฐานข้อมูล
const
[
cartItems
]
=
await
pool
.
execute
(
`
SELECT c.id, p.name, p.price, c.quantity, p.stock, p.image
FROM cart_items c
...
...
@@ -151,7 +151,7 @@ app.get("/cart", isLoggedIn, async (req, res) => {
WHERE c.user_id = ?
`
,
[
req
.
session
.
user
.
id
]);
//
คำนวณยอดรวม
//คำนวณยอดรวม
const
total
=
cartItems
.
reduce
((
sum
,
item
)
=>
sum
+
item
.
price
*
item
.
quantity
,
0
);
res
.
render
(
"
cart
"
,
{
cartItems
,
total
});
...
...
@@ -170,20 +170,20 @@ app.post("/cart/add", async (req, res) => {
const
userId
=
req
.
session
.
user
.
id
;
try
{
//
เช็คว่าสินค้ามีอยู่ในตะกร้าหรือยัง
//เช็คว่าสินค้ามีอยู่ในตะกร้าหรือยัง
const
[
existingCart
]
=
await
pool
.
execute
(
"
SELECT * FROM cart_items WHERE user_id = ? AND product_id = ?
"
,
[
userId
,
productId
]
);
if
(
existingCart
.
length
>
0
)
{
//
ถ้ามีสินค้าอยู่แล้ว ให้เพิ่มจำนวน
//ถ้ามีสินค้าอยู่แล้ว ให้เพิ่มจำนวน
await
pool
.
execute
(
"
UPDATE cart_items SET quantity = quantity + ? WHERE user_id = ? AND product_id = ?
"
,
[
quantity
,
userId
,
productId
]
);
}
else
{
//
ถ้าไม่มีสินค้า ให้เพิ่มใหม่
//ถ้าไม่มีสินค้า ให้เพิ่มใหม่
await
pool
.
execute
(
"
INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)
"
,
[
userId
,
productId
,
quantity
]
...
...
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