Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prjcloud_65160270
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
65160270
prjcloud_65160270
Commits
2873a823
Commit
2873a823
authored
3 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-edit
parent
94644f99
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
public/css/style.css
+1
-0
1 addition, 0 deletions
public/css/style.css
shop-routes/cart.js
+6
-0
6 additions, 0 deletions
shop-routes/cart.js
shop-routes/index.js
+44
-9
44 additions, 9 deletions
shop-routes/index.js
views/cart.ejs
+6
-1
6 additions, 1 deletion
views/cart.ejs
with
57 additions
and
10 deletions
public/css/style.css
+
1
−
0
View file @
2873a823
...
@@ -443,6 +443,7 @@ footer {
...
@@ -443,6 +443,7 @@ footer {
text-decoration
:
none
;
text-decoration
:
none
;
border-radius
:
5px
;
border-radius
:
5px
;
border
:
1px
solid
#ffc107
;
border
:
1px
solid
#ffc107
;
cursor
:
pointer
;
}
}
.edit-btn
:hover
{
.edit-btn
:hover
{
...
...
This diff is collapsed.
Click to expand it.
shop-routes/cart.js
+
6
−
0
View file @
2873a823
...
@@ -127,4 +127,10 @@ router.post('/remove', async (req, res) => {
...
@@ -127,4 +127,10 @@ router.post('/remove', async (req, res) => {
}
}
});
});
router
.
post
(
'
/edit
'
,
(
req
,
res
)
=>
{
const
{
cartItemId
}
=
req
.
body
;
req
.
session
.
editingItemId
=
cartItemId
;
// เก็บ ID ไว้ชั่วคราว
res
.
redirect
(
'
/
'
);
// กลับไปเลือกสินค้าใหม่
});
module
.
exports
=
router
;
module
.
exports
=
router
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
shop-routes/index.js
+
44
−
9
View file @
2873a823
require
(
'
dotenv
'
).
config
();
//โหลดตัวแปร .env ก่อน
require
(
'
dotenv
'
).
config
();
const
express
=
require
(
'
express
'
);
const
express
=
require
(
'
express
'
);
const
router
=
express
.
Router
();
const
router
=
express
.
Router
();
const
pool
=
require
(
'
../config/database
'
);
//Import database หลังจากโหลด .env
const
pool
=
require
(
'
../config/database
'
);
const
isLoggedIn
=
(
req
,
res
,
next
)
=>
{
if
(
req
.
session
.
userId
)
{
next
();
// ผู้ใช้ Login แล้ว
}
else
{
res
.
redirect
(
'
/login
'
);
// ยังไม่ Login
}
};
console
.
log
(
"
Database Host:
"
,
process
.
env
.
DB_HOST
);
console
.
log
(
"
Database Host:
"
,
process
.
env
.
DB_HOST
);
// แสดงสินค้าในหน้าแรก
router
.
get
(
'
/
'
,
async
(
req
,
res
)
=>
{
router
.
get
(
'
/
'
,
async
(
req
,
res
)
=>
{
try
{
try
{
const
[
products
]
=
await
pool
.
query
(
'
SELECT * FROM products WHERE stock > 0
'
);
const
[
products
]
=
await
pool
.
query
(
'
SELECT * FROM products WHERE stock > 0
'
);
...
@@ -22,4 +16,45 @@ router.get('/', async (req, res) => {
...
@@ -22,4 +16,45 @@ router.get('/', async (req, res) => {
}
}
});
});
// เพิ่มสินค้าในตะกร้า (รองรับระบบ Edit)
router
.
post
(
'
/add-to-cart
'
,
async
(
req
,
res
)
=>
{
const
{
productId
,
quantity
}
=
req
.
body
;
try
{
const
[
productResult
]
=
await
pool
.
query
(
'
SELECT * FROM products WHERE id = ? AND stock > 0
'
,
[
productId
]);
if
(
productResult
.
length
===
0
)
{
return
res
.
status
(
404
).
send
(
'
Product not found or out of stock
'
);
}
const
product
=
productResult
[
0
];
if
(
!
req
.
session
.
cart
)
{
req
.
session
.
cart
=
[];
}
// ถ้ากำลังแก้ไขสินค้า
if
(
req
.
session
.
editingItemId
)
{
req
.
session
.
cart
=
req
.
session
.
cart
.
map
(
item
=>
item
.
id
===
parseInt
(
req
.
session
.
editingItemId
)
?
{
id
:
product
.
id
,
name
:
product
.
name
,
price
:
product
.
price
,
quantity
:
parseInt
(
quantity
),
stock
:
product
.
stock
}
:
item
);
req
.
session
.
editingItemId
=
null
;
// ล้างค่า
}
else
{
// เพิ่มสินค้าปกติ
req
.
session
.
cart
.
push
({
id
:
product
.
id
,
name
:
product
.
name
,
price
:
product
.
price
,
quantity
:
parseInt
(
quantity
),
stock
:
product
.
stock
});
}
res
.
redirect
(
'
/cart
'
);
// กลับไปหน้าตะกร้า
}
catch
(
error
)
{
console
.
error
(
error
);
res
.
status
(
500
).
send
(
'
Error adding product to cart
'
);
}
});
module
.
exports
=
router
;
module
.
exports
=
router
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
views/cart.ejs
+
6
−
1
View file @
2873a823
...
@@ -41,7 +41,12 @@
...
@@ -41,7 +41,12 @@
Remove
Remove
</button>
</button>
</form>
</form>
<a href="/" class="Edit-btn">Edit</a> <!-- ปุ่ม Edit -->
<form action="/cart/edit" method="POST">
<input type="hidden" name="cartItemId" value="<%= item.id %>">
<button type="submit" class="edit-btn">
Edit
</button>
</form>
</div>
</div>
<% }); %>
<% }); %>
</div>
</div>
...
...
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