From 0fd8f55afb5697348e1e068ad20ca226e815b858 Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Thu, 20 Mar 2025 22:28:52 +0700 Subject: [PATCH] Project Round 9 --- models/tourModel.js | 26 ++++++++++---------------- routes/tourRoutes.js | 3 +-- views/bookinglist.ejs | 13 +++++-------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/models/tourModel.js b/models/tourModel.js index d64376b..ff10ee9 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -154,36 +154,30 @@ class Booking { WHERE b.user_id = ? ORDER BY b.id ASC `; - 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'); - } + const [rows] = await pool.query(query, [userId]); + return rows; } // ทำการจองทัวร์ static async createBooking(userId, tourId) { const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)'; try { - const [result] = await pool.execute(query, [userId, tourId]); - return result; // คืนค่าผลลัพธ์จากการจอง + await pool.execute(query, [userId, tourId]); } catch (error) { console.error('Error creating booking:', error); throw new Error('Error creating booking'); } } - // ลบการจอง - static async deleteBooking(bookingId) { - const query = 'DELETE FROM bookings WHERE id = ?'; + + // ยกเลิกการจอง + static async cancelBooking(userId, bookingId) { + const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?'; try { - const [result] = await pool.execute(query, [bookingId]); - return result; // คืนค่าผลลัพธ์จากการลบการจอง + const [result] = await pool.execute(query, [bookingId, userId]); + return result.affectedRows; } catch (error) { - console.error('Error deleting booking:', error); - throw new Error('Error deleting booking'); + throw new Error('เกิดข้อผิดพลาดในการยกเลิกการจอง'); } } } diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js index ef22629..daf4929 100644 --- a/routes/tourRoutes.js +++ b/routes/tourRoutes.js @@ -37,7 +37,6 @@ router.get('/bookinglist', tourController.getUserBookings); router.get('/create-booking', tourController.getAllTours); router.post('/create-booking', tourController.createBooking); router.post('/cancel/:id', tourController.cancelBooking); -router.get('/edit-booking/:id', tourController.getEditBooking); -router.get('/delete-booking/:id', tourController.deleteBooking); + module.exports = router; diff --git a/views/bookinglist.ejs b/views/bookinglist.ejs index 90ea529..594d7f2 100644 --- a/views/bookinglist.ejs +++ b/views/bookinglist.ejs @@ -3,32 +3,29 @@ <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>Booking List</title> + <title>bookinglist</title> <link rel="stylesheet" href="/css/booklist.css"> </head> <body> - <h1>Booking List</h1> + <h1>bookinglist</h1> <table border="1"> <thead> <tr> + <th>Booking ID</th> <th>Booking User</th> <th>Tour Title</th> - <th>Actions</th> </tr> </thead> <tbody> <% bookings.forEach(booking => { %> <tr> + <td><%= booking.booking_id %></td> <!-- booking_id --> <td><%= booking.user_name %></td> <!-- ชื่อผู้จอง --> <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> <% }) %> </tbody> </table> - <a href="/" class="btn-back">Back</a> + <a href="/" class="btn-back">ย้อนกลับ</a> </body> </html> -- GitLab