Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 9029f4d5 authored by 65160394's avatar 65160394
Browse files

Project Round 9

parent 77dd6230
Branches
No related tags found
No related merge requests found
...@@ -293,7 +293,7 @@ exports.getUserBookings = async (req, res) => { ...@@ -293,7 +293,7 @@ exports.getUserBookings = async (req, res) => {
console.log("Fetching bookings for user:", userId); console.log("Fetching bookings for user:", userId);
try { try {
const query = ` const query = `
SELECT b.id AS booking_id, t.name AS tour_name, u.name AS user_name SELECT t.name AS tour_name, u.name AS user_name
FROM bookings b FROM bookings b
JOIN tours t ON b.tour_id = t.id JOIN tours t ON b.tour_id = t.id
JOIN users u ON b.user_id = u.id JOIN users u ON b.user_id = u.id
...@@ -343,14 +343,27 @@ exports.createBooking = async (req, res) => { ...@@ -343,14 +343,27 @@ exports.createBooking = async (req, res) => {
} }
}; };
// ยกเลิกการจอง // แก้ไขการจอง (แสดงฟอร์ม)
exports.cancelBooking = (req, res) => { exports.getEditBooking = async (req, res) => {
const bookingId = req.params.id; const bookingId = req.params.id;
bookingModel.cancelBooking(bookingId, (err, result) => { try {
if (err) return res.status(500).json({ error: err.message }); const booking = await Booking.findById(bookingId);
if (result.affectedRows === 0) { if (!booking) {
return res.status(404).json({ message: 'Booking not found' }); return res.status(404).send('Booking not found');
} }
res.status(200).json({ message: 'Booking canceled successfully' }); 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');
}
}; };
\ No newline at end of file
...@@ -154,30 +154,36 @@ class Booking { ...@@ -154,30 +154,36 @@ class Booking {
WHERE b.user_id = ? WHERE b.user_id = ?
ORDER BY b.id ASC ORDER BY b.id ASC
`; `;
const [rows] = await pool.query(query, [userId]); try {
return rows; 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) { static async createBooking(userId, tourId) {
const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)'; const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)';
try { try {
await pool.execute(query, [userId, tourId]); const [result] = await pool.execute(query, [userId, tourId]);
return result; // คืนค่าผลลัพธ์จากการจอง
} catch (error) { } catch (error) {
console.error('Error creating booking:', error); console.error('Error creating booking:', error);
throw new Error('Error creating booking'); throw new Error('Error creating booking');
} }
} }
// ลบการจอง
// ยกเลิกการจอง static async deleteBooking(bookingId) {
static async cancelBooking(userId, bookingId) { const query = 'DELETE FROM bookings WHERE id = ?';
const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?';
try { try {
const [result] = await pool.execute(query, [bookingId, userId]); const [result] = await pool.execute(query, [bookingId]);
return result.affectedRows; return result; // คืนค่าผลลัพธ์จากการลบการจอง
} catch (error) { } catch (error) {
throw new Error('เกิดข้อผิดพลาดในการยกเลิกการจอง'); console.error('Error deleting booking:', error);
throw new Error('Error deleting booking');
} }
} }
} }
......
...@@ -39,23 +39,49 @@ body { ...@@ -39,23 +39,49 @@ body {
background-color: #f1f1f1; background-color: #f1f1f1;
} }
.btn-edit { /* ปุ่ม Edit */
.btn-edit {
display: inline-block; display: inline-block;
margin-top: 20px; padding: 8px 15px;
padding: 10px 20px; background-color: #17a2b8; /* สีฟ้า */
background: #17a2b8; /* สีฟ้าอ่อน */
color: white; color: white;
text-decoration: none; text-decoration: none;
border-radius: 5px; border-radius: 5px;
font-weight: bold; font-weight: bold;
font-size: 16px;
} }
.btn-edit:hover { .btn-edit:hover {
background: #138496; /* สีฟ้าเข้มเมื่อ hover */ background-color: #138496;
}
/* ปุ่ม Delete */
.btn-delete {
display: inline-block;
padding: 8px 15px;
background-color: #dc3545; /* สีแดง */
color: white;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
}
.btn-delete:hover {
background-color: #c82333;
}
/* ปุ่มย้อนกลับ */
.btn-back {
display: inline-block;
margin-top: 20px;
padding: 10px 20px;
background: #6c757d; /* สีเทา */
color: white;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
} }
.btn-edit:active { .btn-back:hover {
background: #117a8b; /* สีฟ้าเข้มสุดเมื่อคลิก */ background: #5a6368;
} }
\ No newline at end of file
...@@ -37,5 +37,7 @@ router.get('/bookinglist', tourController.getUserBookings); ...@@ -37,5 +37,7 @@ router.get('/bookinglist', tourController.getUserBookings);
router.get('/create-booking', tourController.getAllTours); router.get('/create-booking', tourController.getAllTours);
router.post('/create-booking', tourController.createBooking); router.post('/create-booking', tourController.createBooking);
router.post('/cancel/:id', tourController.cancelBooking); router.post('/cancel/:id', tourController.cancelBooking);
router.get('/edit-booking/:id', tourController.getEditBooking);
router.get('/delete-booking/:id', tourController.deleteBooking);
module.exports = router; module.exports = router;
...@@ -3,29 +3,32 @@ ...@@ -3,29 +3,32 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>bookinglist</title> <title>Booking List</title>
<link rel="stylesheet" href="/css/booklist.css"> <link rel="stylesheet" href="/css/booklist.css">
</head> </head>
<body> <body>
<h1>bookinglist</h1> <h1>Booking List</h1>
<table border="1"> <table border="1">
<thead> <thead>
<tr> <tr>
<th>Booking ID</th>
<th>Booking User</th> <th>Booking User</th>
<th>Tour Title</th> <th>Tour Title</th>
<th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% bookings.forEach(booking => { %> <% bookings.forEach(booking => { %>
<tr> <tr>
<td><%= booking.booking_id %></td> <!-- booking_id -->
<td><%= booking.user_name %></td> <!-- ชื่อผู้จอง --> <td><%= booking.user_name %></td> <!-- ชื่อผู้จอง -->
<td><%= booking.tour_name %></td> <!-- tour_title --> <td><%= booking.tour_name %></td> <!-- tour_title -->
<td>
<a href="/edit-booking/<%= booking.booking_id %>" class="btn-edit">Edit</a> <!-- ปุ่มแก้ไข -->
<a href="/delete-booking/<%= booking.booking_id %>" class="btn-delete">Delete</a> <!-- ปุ่มลบ -->
</td>
</tr> </tr>
<% }) %> <% }) %>
</tbody> </tbody>
</table> </table>
<a href="/" class="btn-back">ย้อนกลับ</a> <a href="/" class="btn-back">Back</a>
</body> </body>
</html> </html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment