Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BorrowProject65160024-deleted-1917
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
This project is pending deletion, and will be deleted on
2025-07-22
. Repository and other project resources are read-only.
Show more breadcrumbs
65160024
BorrowProject65160024-deleted-1917
Commits
2e6a56d1
Commit
2e6a56d1
authored
4 months ago
by
65160024
Browse files
Options
Downloads
Patches
Plain Diff
update index.js
parent
e7677368
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
index.js
+30
-48
30 additions, 48 deletions
index.js
with
30 additions
and
48 deletions
index.js
+
30
−
48
View file @
2e6a56d1
...
...
@@ -157,47 +157,35 @@ app.get("/", isAuthenticated, (req, res) => {
});
// ยืมอุปกรณ์
// ยืมอุปกรณ์
app
.
post
(
"
/borrow
"
,
isAuthenticated
,
(
req
,
res
)
=>
{
const
{
equipment_id
,
quantity
}
=
req
.
body
;
// ตรวจสอบจำนวนที่ต้องการยืม
db
.
query
(
"
SELECT quantity FROM equipment WHERE id = ?
"
,
[
equipment_id
],
(
err
,
results
)
=>
{
db
.
query
(
"
SELECT quantity FROM equipment WHERE id = ?
"
,
[
equipment_id
],
(
err
,
results
)
=>
{
if
(
err
)
throw
err
;
const
availableQuantity
=
results
[
0
].
quantity
;
if
(
availableQuantity
>=
quantity
)
{
// ลดจำนวนอุปกรณ์ที่มีในระบบ
db
.
query
(
"
UPDATE equipment SET quantity = quantity - ? WHERE id = ?
"
,
[
quantity
,
equipment_id
],
(
err
)
=>
{
db
.
query
(
"
UPDATE equipment SET quantity = quantity - ? WHERE id = ?
"
,
[
quantity
,
equipment_id
],
(
err
)
=>
{
if
(
err
)
throw
err
;
// บันทึกข้อมูลการยืม (โดยใช้สถานะ 'pending' รอการยืนยัน)
db
.
query
(
"
INSERT INTO loans (equipment_id, user_id, quantity, status) VALUES (?, ?, ?, 'pending')
"
,
[
equipment_id
,
req
.
user
.
id
,
quantity
],
(
err
)
=>
{
db
.
query
(
"
INSERT INTO loans (equipment_id, user_id, quantity, status) VALUES (?, ?, ?, 'pending')
"
,
[
equipment_id
,
req
.
user
.
id
,
quantity
],
(
err
)
=>
{
if
(
err
)
throw
err
;
res
.
redirect
(
"
/loans
"
);
// เปลี่ยนเส้นทางไปยังหน้า 'รวมการยืม'
}
);
}
);
});
});
}
else
{
res
.
send
(
"
จำนวนอุปกรณ์ไม่เพียงพอ
"
);
}
}
);
});
});
// คืนอุปกรณ์
app
.
post
(
"
/return
"
,
isAuthenticated
,
(
req
,
res
)
=>
{
const
{
equipment_id
}
=
req
.
body
;
...
...
@@ -294,7 +282,7 @@ app.post("/delete-equipment", isAuthenticated, (req, res) => {
app
.
get
(
"
/loans
"
,
(
req
,
res
)
=>
{
if
(
req
.
user
&&
req
.
user
.
role
===
"
user
"
)
{
db
.
query
(
`SELECT loans.id, loans.quantity, loans.status, equipment.name AS equipment_name, users.name AS borrower_name
`SELECT loans.id, loans.quantity, loans.status, equipment.name AS equipment_name, users.
user
name AS borrower_name
FROM loans
JOIN equipment ON loans.equipment_id = equipment.id
JOIN users ON loans.user_id = users.id
...
...
@@ -313,15 +301,16 @@ app.get("/loans", (req, res) => {
}
});
// หน้าแสดงรายการการยืมสำหรับ Admin
app
.
get
(
"
/admin-loans
"
,
(
req
,
res
)
=>
{
if
(
req
.
isAuthenticated
()
&&
req
.
user
.
role
===
"
admin
"
)
{
// Query ดึงข้อมูลการยืม
db
.
query
(
'
SELECT loans.id, users.username, equipment.name AS equipment_name, loans.quantity, loans.status FROM loans JOIN users ON loans.user_id = users.id JOIN equipment ON loans.equipment_id = equipment.id WHERE loans.status = "pending"
'
,
`SELECT loans.id, users.username, equipment.name AS equipment_name, loans.quantity, loans.status
FROM loans
JOIN users ON loans.user_id = users.id
JOIN equipment ON loans.equipment_id = equipment.id
WHERE loans.status = "pending"`
,
(
err
,
loans
)
=>
{
if
(
err
)
throw
err
;
res
.
render
(
"
admin-loans
"
,
{
user
:
req
.
user
,
loans
:
loans
});
// ส่งข้อมูล user และ loans
...
...
@@ -332,27 +321,20 @@ app.get("/admin-loans", (req, res) => {
}
});
// ยืนยันการยืม (สำหรับ Admin)
// อนุมัติหรือปฏิเสธคำขอยืม (สำหรับ Admin)
app
.
post
(
"
/approve-loan
"
,
isAuthenticated
,
isAdmin
,
(
req
,
res
)
=>
{
const
{
loan_id
,
action
}
=
req
.
body
;
// กำหนดสถานะใหม่
let
newStatus
=
action
===
'
approve
'
?
'
approved
'
:
'
rejected
'
;
let
newStatus
=
action
===
"
approve
"
?
"
approved
"
:
"
rejected
"
;
// อัปเดตสถานะในฐานข้อมูล
db
.
query
(
"
UPDATE loans SET status = ? WHERE id = ?
"
,
[
newStatus
,
loan_id
],
(
err
)
=>
{
db
.
query
(
"
UPDATE loans SET status = ? WHERE id = ?
"
,
[
newStatus
,
loan_id
],
(
err
)
=>
{
if
(
err
)
throw
err
;
res
.
redirect
(
"
/admin-loans
"
);
// หลังจากอนุมัติหรือปฏิเสธเสร็จ, กลับไปที่หน้าการจัดการการยืม
}
);
});
});
// Route แสดงหน้าการยืนยันการยืม
app
.
post
(
"
/confirm-loan
"
,
(
req
,
res
)
=>
{
...
...
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