diff --git a/controllers/tourController.js b/controllers/tourController.js index 0e025d111dff402542da3134a26d227e8064d405..35f8d9df55e7c26186c434e822700fe056b4ee81 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 ในการจัดการการล็อกอิน