diff --git a/controllers/tourController.js b/controllers/tourController.js index a693e2fda41197783cabb7f9266b2f20c2079f50..e9e6f6ec8f5bb5bf730dd3a26e88943912115870 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -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 diff --git a/models/tourModel.js b/models/tourModel.js index ff10ee924d6fd49b13a238b15b72e86d4b22ea07..d64376b09ec17552d2709d8acecd9109e1b7f6f7 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -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'); } } }