From a3eff2063dcf247b679a003e160283ce1e98ae0b Mon Sep 17 00:00:00 2001
From: Atiwit Pattanapukdee <65160394@go.buu.ac.th>
Date: Thu, 20 Mar 2025 22:41:14 +0700
Subject: [PATCH] Project Round 9

---
 controllers/tourController.js | 34 +++++++++++++++-------------------
 models/tourModel.js           | 26 ++++++++++++++++----------
 2 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/controllers/tourController.js b/controllers/tourController.js
index a693e2f..e9e6f6e 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 ff10ee9..d64376b 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');
     }
   }
 }
-- 
GitLab