Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Project
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
65160394
Project
Commits
a3eff206
Commit
a3eff206
authored
2 weeks ago
by
65160394
Browse files
Options
Downloads
Patches
Plain Diff
Project Round 9
parent
0552a71b
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
controllers/tourController.js
+15
-19
15 additions, 19 deletions
controllers/tourController.js
models/tourModel.js
+16
-10
16 additions, 10 deletions
models/tourModel.js
with
31 additions
and
29 deletions
controllers/tourController.js
+
15
−
19
View file @
a3eff206
...
...
@@ -284,6 +284,7 @@ exports.getAllTours = async (req, res) => {
};
// ดึงข้อมูลการจองพร้อมชื่อผู้จอง
// ดึงรายการจองของผู้ใช้
exports
.
getUserBookings
=
async
(
req
,
res
)
=>
{
const
userId
=
req
.
session
.
userId
;
// สมมติว่าใช้ session สำหรับจัดการผู้ใช้
...
...
@@ -311,7 +312,6 @@ exports.getUserBookings = async (req, res) => {
}
};
// ฟังก์ชันสร้างการจอง
exports
.
createBooking
=
async
(
req
,
res
)
=>
{
const
userId
=
req
.
session
.
userId
;
// สมมติว่าใช้ session ในการจัดการการล็อกอิน
...
...
@@ -343,27 +343,23 @@ exports.createBooking = async (req, res) => {
}
};
// แก้ไขการจอง (แสดงฟอร์ม)
exports
.
getEditBooking
=
async
(
req
,
res
)
=>
{
const
bookingId
=
req
.
params
.
id
;
// ฟังก์ชันยกเลิกการจอง
exports
.
cancelBooking
=
async
(
req
,
res
)
=>
{
const
bookingId
=
req
.
params
.
id
;
// รับ bookingId จาก URL params
try
{
const
booking
=
await
Booking
.
findById
(
bookingId
);
if
(
!
booking
)
{
// ตรวจสอบว่ามีการจองนี้ในฐานข้อมูลหรือไม่
const
cancelQuery
=
'
DELETE FROM bookings WHERE id = ?
'
;
const
[
result
]
=
await
pool
.
execute
(
cancelQuery
,
[
bookingId
]);
// ถ้าลบไม่สำเร็จ ให้แจ้งว่าไม่พบการจอง
if
(
result
.
affectedRows
===
0
)
{
return
res
.
status
(
404
).
send
(
'
Booking not found
'
);
}
res
.
render
(
'
edit-booking
'
,
{
booking
});
}
catch
(
err
)
{
res
.
status
(
500
).
send
(
'
Error fetching booking details
'
);
}
};
// ลบการจอง
exports
.
deleteBooking
=
async
(
req
,
res
)
=>
{
const
bookingId
=
req
.
params
.
id
;
try
{
await
Booking
.
deleteOne
({
_id
:
bookingId
});
res
.
redirect
(
'
/bookings
'
);
}
catch
(
err
)
{
res
.
status
(
500
).
send
(
'
Error deleting booking
'
);
res
.
status
(
200
).
send
(
'
Booking canceled successfully
'
);
}
catch
(
error
)
{
console
.
error
(
'
Error canceling booking:
'
,
error
.
message
);
res
.
status
(
500
).
send
(
'
เกิดข้อผิดพลาดในการยกเลิกการจอง
'
);
}
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
models/tourModel.js
+
16
−
10
View file @
a3eff206
...
...
@@ -154,30 +154,36 @@ class Booking {
WHERE b.user_id = ?
ORDER BY b.id ASC
`
;
const
[
rows
]
=
await
pool
.
query
(
query
,
[
userId
]);
return
rows
;
try
{
const
[
rows
]
=
await
pool
.
query
(
query
,
[
userId
]);
return
rows
;
// คืนค่ารายการจองทั้งหมด
}
catch
(
error
)
{
console
.
error
(
'
Error fetching user bookings:
'
,
error
);
throw
new
Error
(
'
Unable to retrieve user bookings
'
);
}
}
// ทำการจองทัวร์
static
async
createBooking
(
userId
,
tourId
)
{
const
query
=
'
INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)
'
;
try
{
await
pool
.
execute
(
query
,
[
userId
,
tourId
]);
const
[
result
]
=
await
pool
.
execute
(
query
,
[
userId
,
tourId
]);
return
result
;
// คืนค่าผลลัพธ์จากการจอง
}
catch
(
error
)
{
console
.
error
(
'
Error creating booking:
'
,
error
);
throw
new
Error
(
'
Error creating booking
'
);
}
}
// ยกเลิกการจอง
static
async
cancelBooking
(
userId
,
bookingId
)
{
const
query
=
'
DELETE FROM bookings WHERE id = ? AND user_id = ?
'
;
// ลบการจอง
static
async
deleteBooking
(
bookingId
)
{
const
query
=
'
DELETE FROM bookings WHERE id = ?
'
;
try
{
const
[
result
]
=
await
pool
.
execute
(
query
,
[
bookingId
,
userId
]);
return
result
.
affectedRows
;
const
[
result
]
=
await
pool
.
execute
(
query
,
[
bookingId
]);
return
result
;
// คืนค่าผลลัพธ์จากการลบการจอง
}
catch
(
error
)
{
throw
new
Error
(
'
เกิดข้อผิดพลาดในการยกเลิกการจอง
'
);
console
.
error
(
'
Error deleting booking:
'
,
error
);
throw
new
Error
(
'
Error deleting booking
'
);
}
}
}
...
...
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