From 930ff118a578745c93b84f93f6a6156b4b34d2e1 Mon Sep 17 00:00:00 2001
From: Atiwit Pattanapukdee <65160394@go.buu.ac.th>
Date: Thu, 20 Mar 2025 21:38:39 +0700
Subject: [PATCH] Project Round 9

---
 controllers/tourController.js | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/controllers/tourController.js b/controllers/tourController.js
index 0e025d1..35f8d9d 100644
--- a/controllers/tourController.js
+++ b/controllers/tourController.js
@@ -283,14 +283,34 @@ exports.getAllTours = async (req, res) => {
   }
 };
 
-exports.getUserBookings = (req, res) => {
-  const userId = req.user.id; // สมมติว่า req.user เก็บข้อมูลผู้ใช้ที่ล็อกอินแล้ว
-  bookingModel.getBookings(userId, (err, bookings) => {
-    if (err) return res.status(500).json({ error: err.message });
-    res.render('bookingView', { bookings });
-  });
+exports.getUserBookings = async (req, res) => {
+  const userId = req.session.userId;  // ตรวจสอบการใช้งาน session สำหรับ userId
+
+  // ตรวจสอบว่า userId ถูกต้องหรือไม่
+  if (!userId) {
+    return res.status(400).send('User not logged in.');
+  }
+
+  try {
+    // ดึงข้อมูลการจองของผู้ใช้จากฐานข้อมูล
+    const query = `
+      SELECT b.id, t.name AS tour_name, t.price, b.booking_date
+      FROM bookings b
+      JOIN tours t ON b.tour_id = t.id
+      WHERE b.user_id = ?
+      ORDER BY b.booking_date DESC
+    `;
+    const [bookings] = await pool.query(query, [userId]);
+
+    // ส่งข้อมูลการจองกลับไปยังผู้ใช้
+    res.render('myBookings', { bookings });
+  } catch (error) {
+    console.error('Error fetching user bookings:', error.message);
+    res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูลการจอง');
+  }
 };
 
+
 // ฟังก์ชันสร้างการจอง
 exports.createBooking = async (req, res) => {
   const userId = req.session.userId;  // สมมติว่าใช้ session ในการจัดการการล็อกอิน
-- 
GitLab