Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BorrowProject65160024
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
65160024
BorrowProject65160024
Commits
4dc6eac6
Commit
4dc6eac6
authored
2 weeks ago
by
65160024
Browse files
Options
Downloads
Patches
Plain Diff
update index.js
parent
99bae5c3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
index.js
+25
-17
25 additions, 17 deletions
index.js
views/index.ejs
+5
-6
5 additions, 6 deletions
views/index.ejs
with
30 additions
and
23 deletions
index.js
+
25
−
17
View file @
4dc6eac6
...
...
@@ -337,26 +337,34 @@ app.post("/approve-loan", isAuthenticated, isAdmin, (req, res) => {
// Route แสดงหน้าการยืนยันการยืม
app
.
post
(
"
/confirm-loan
"
,
(
req
,
res
)
=>
{
if
(
!
req
.
user
)
{
return
res
.
redirect
(
"
/login
"
);
// หากไม่ได้ล็อกอิน ให้กลับไปที่หน้า login
}
router
.
post
(
'
/confirm-loan
'
,
(
req
,
res
)
=>
{
const
{
equipment_id
,
quantity
}
=
req
.
body
;
const
equipment_id
=
req
.
body
.
equipment_id
;
const
quantity
=
req
.
body
.
quantity
;
// คำสั่ง SQL เพื่อลงข้อมูลการยืม
const
query
=
`
INSERT INTO loans (equipment_id, quantity, user_id, loan_date)
VALUES (?, ?, ?, NOW())`
;
// ดึงข้อมูลอุปกรณ์จากฐานข้อมูล
db
.
query
(
"
SELECT * FROM equipment WHERE id = ?
"
,
[
equipment_id
],
(
err
,
result
)
=>
{
if
(
err
)
throw
err
;
const
equipment
=
result
[
0
];
// รับ user_id จาก session หรือจาก JWT token (ถ้าใช้ระบบล็อกอิน)
const
user_id
=
req
.
session
.
userId
;
// สมมติว่าเก็บ user_id ไว้ใน session
// ส่งข้อมูล user ไปยัง confirm-loan.ejs
res
.
render
(
"
confirm-loan
"
,
{
equipment
,
quantity
,
user
:
req
.
user
});
}
);
db
.
query
(
query
,
[
equipment_id
,
quantity
,
user_id
],
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
error
(
'
Error inserting loan data:
'
,
err
);
return
res
.
status
(
500
).
send
(
'
เกิดข้อผิดพลาดในการยืมอุปกรณ์
'
);
}
// ปรับสถานะของอุปกรณ์หลังการยืม
const
updateQuery
=
'
UPDATE equipment SET quantity = quantity - ? WHERE id = ?
'
;
db
.
query
(
updateQuery
,
[
quantity
,
equipment_id
],
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
Error updating equipment quantity:
'
,
err
);
return
res
.
status
(
500
).
send
(
'
เกิดข้อผิดพลาดในการปรับปรุงข้อมูลอุปกรณ์
'
);
}
res
.
redirect
(
'
/loans
'
);
// ไปยังหน้ารวมการยืมอุปกรณ์
});
});
});
...
...
This diff is collapsed.
Click to expand it.
views/index.ejs
+
5
−
6
View file @
4dc6eac6
...
...
@@ -84,13 +84,12 @@
<form
action=
"/confirm-loan"
method=
"POST"
class=
"flex space-x-2"
>
<input
type=
"hidden"
name=
"equipment_id"
value=
"<%= item.id %>"
>
<label
for=
"quantity"
class=
"text-sm text-gray-700"
>
จำนวน:
</label>
<input
type=
"number"
name=
"quantity"
min=
"1"
max=
"<%= item.quantity %>"
class=
"p-2 border border-gray-300 rounded-md"
required
>
<button
type=
"submit"
class=
"bg-blue-600 text-white p-2 rounded-md hover:bg-blue-700"
>
ยืมอุปกรณ์
<input
type=
"number"
name=
"quantity"
min=
"1"
max=
"<%= item.quantity %>"
class=
"p-2 border border-gray-300 rounded-md"
required
>
<button
type=
"submit"
class=
"bg-blue-600 text-white p-2 rounded-md hover:bg-blue-700"
>
ยืมอุปกรณ์
</button>
</form>
</form>
<
%
}
else
{
%
>
<span
class=
"text-gray-400"
>
ไม่สามารถยืมได้
</span>
...
...
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