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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160024
BorrowProject65160024
Commits
fdb39e3d
Commit
fdb39e3d
authored
3 months ago
by
65160024
Browse files
Options
Downloads
Patches
Plain Diff
update submit loan
parent
c486147d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
index.js
+5
-20
5 additions, 20 deletions
index.js
views/confirm-loan.ejs
+45
-42
45 additions, 42 deletions
views/confirm-loan.ejs
with
50 additions
and
62 deletions
index.js
+
5
−
20
View file @
fdb39e3d
...
@@ -378,48 +378,33 @@ app.post("/confirm-loan", (req, res) => {
...
@@ -378,48 +378,33 @@ app.post("/confirm-loan", (req, res) => {
// Route สำหรับยืนยันการยืม
// Route สำหรับยืนยันการยืม
app
.
post
(
'
/submit-loan
'
,
(
req
,
res
)
=>
{
app
.
post
(
'
/submit-loan
'
,
(
req
,
res
)
=>
{
if
(
!
req
.
user
||
!
req
.
user
.
id
)
{
return
res
.
status
(
401
).
send
(
'
กรุณาล็อกอินก่อนการยืมอุปกรณ์
'
);
}
const
user_id
=
req
.
user
.
id
;
// สมมติว่าผู้ใช้ล็อกอินแล้ว
const
user_id
=
req
.
user
.
id
;
// สมมติว่าผู้ใช้ล็อกอินแล้ว
const
equipment_id
=
req
.
body
.
equipment_id
;
const
equipment_id
=
req
.
body
.
equipment_id
;
const
quantity
=
req
.
body
.
quantity
;
const
quantity
=
req
.
body
.
quantity
;
// ตรวจสอบว่าอุปกรณ์มีจำนวนเพียงพอหรือไม่
// ตรวจสอบว่าอุปกรณ์มีจำนวนเพียงพอหรือไม่
db
.
query
(
'
SELECT * FROM equipment WHERE id = ?
'
,
[
equipment_id
],
(
err
,
result
)
=>
{
db
.
query
(
'
SELECT * FROM equipment WHERE id = ?
'
,
[
equipment_id
],
(
err
,
result
)
=>
{
if
(
err
)
{
if
(
err
)
throw
err
;
return
res
.
status
(
500
).
send
(
'
เกิดข้อผิดพลาดในการดึงข้อมูล
'
);
}
const
equipment
=
result
[
0
];
const
equipment
=
result
[
0
];
if
(
!
equipment
)
{
return
res
.
status
(
404
).
send
(
'
ไม่พบอุปกรณ์นี้ในระบบ
'
);
}
if
(
equipment
.
quantity
>=
quantity
)
{
if
(
equipment
.
quantity
>=
quantity
)
{
// บันทึกข้อมูลการยืมลงในตาราง loans
// บันทึกข้อมูลการยืมลงในตาราง loans
db
.
query
(
'
INSERT INTO loans (user_id, equipment_id, quantity, status) VALUES (?, ?, ?, "pending")
'
,
[
user_id
,
equipment_id
,
quantity
],
(
err
)
=>
{
db
.
query
(
'
INSERT INTO loans (user_id, equipment_id, quantity, status) VALUES (?, ?, ?, "pending")
'
,
[
user_id
,
equipment_id
,
quantity
],
(
err
)
=>
{
if
(
err
)
{
if
(
err
)
throw
err
;
return
res
.
status
(
500
).
send
(
'
เกิดข้อผิดพลาดในการบันทึกการยืม
'
);
}
// ปรับจำนวนอุปกรณ์ในตาราง equipment
// ปรับจำนวนอุปกรณ์ในตาราง equipment
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
)
{
if
(
err
)
throw
err
;
return
res
.
status
(
500
).
send
(
'
เกิดข้อผิดพลาดในการอัปเดตข้อมูลอุปกรณ์
'
);
}
res
.
redirect
(
'
/loans
'
);
// เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์"
res
.
redirect
(
'
/loans
'
);
// เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์"
});
});
});
});
}
else
{
}
else
{
res
.
status
(
400
).
send
(
'
จำนวนอุปกรณ์ไม่เพียงพอ
'
);
res
.
send
(
'
จำนวนอุปกรณ์ไม่เพียงพอ
'
);
}
}
});
});
});
});
// การยกเลิกการยืม (Backend)
// การยกเลิกการยืม (Backend)
app
.
post
(
"
/cancel-loan
"
,
(
req
,
res
)
=>
{
app
.
post
(
"
/cancel-loan
"
,
(
req
,
res
)
=>
{
const
loanId
=
req
.
body
.
loan_id
;
const
loanId
=
req
.
body
.
loan_id
;
...
...
This diff is collapsed.
Click to expand it.
views/confirm-loan.ejs
+
45
−
42
View file @
fdb39e3d
...
@@ -43,13 +43,15 @@
...
@@ -43,13 +43,15 @@
<div
class=
"flex justify-center space-x-4"
>
<div
class=
"flex justify-center space-x-4"
>
<!-- ฟอร์มยืนยันการยืม -->
<!-- ฟอร์มยืนยันการยืม -->
<button
id=
"confirmButton"
class=
"bg-blue-600 text-white px-6 py-3 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400"
>
<button
id=
"confirmButton"
class=
"bg-blue-600 text-white px-6 py-3 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400"
>
ยืนยันการยืม
ยืนยันการยืม
</button>
</button>
<!-- ฟอร์มยกเลิกการยืม -->
<!-- ฟอร์มยกเลิกการยืม -->
<form
action=
"/cancel-loan"
method=
"GET"
class=
"flex space-x-2"
>
<form
action=
"/cancel-loan"
method=
"GET"
class=
"flex space-x-2"
>
<button
type=
"submit"
class=
"bg-gray-300 text-gray-800 px-6 py-3 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-400"
>
ยกเลิก
</button>
<button
type=
"submit"
class=
"bg-gray-300 text-gray-800 px-6 py-3 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-400"
>
ยกเลิก
</button>
</form>
</form>
</div>
</div>
</div>
</div>
...
@@ -70,7 +72,7 @@
...
@@ -70,7 +72,7 @@
fetch
(
'
/submit-loan
'
,
{
fetch
(
'
/submit-loan
'
,
{
method
:
'
POST
'
,
method
:
'
POST
'
,
headers
:
{
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
// ตั้งค่า header ให้เป็น application/json
},
},
body
:
JSON
.
stringify
({
body
:
JSON
.
stringify
({
equipment_id
:
equipment_id
,
equipment_id
:
equipment_id
,
...
@@ -88,6 +90,7 @@
...
@@ -88,6 +90,7 @@
.
catch
(
error
=>
{
.
catch
(
error
=>
{
alert
(
'
เกิดข้อผิดพลาด:
'
+
error
);
alert
(
'
เกิดข้อผิดพลาด:
'
+
error
);
});
});
});
});
</script>
</script>
...
...
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