From 4b707dd4a24469db979ea1aa7ba3ec197ddb2159 Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Thu, 20 Mar 2025 21:50:24 +0700 Subject: [PATCH] Project Round 9 --- controllers/tourController.js | 16 +++++----- public/css/booklist.css | 55 +++++++++++++++++++++++++++++++++++ views/bookinglist.ejs | 7 +++-- 3 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 public/css/booklist.css diff --git a/controllers/tourController.js b/controllers/tourController.js index e46c6dc..ada9213 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -283,29 +283,29 @@ exports.getAllTours = async (req, res) => { } }; +// ดึงข้อมูลการจองพร้อมชื่อผู้จอง exports.getUserBookings = async (req, res) => { - const userId = req.session.userId; // ตรวจสอบการใช้งาน session สำหรับ userId + const userId = req.session.userId; // สมมติว่าใช้ session สำหรับจัดการผู้ใช้ - // ตรวจสอบว่า userId ถูกต้องหรือไม่ if (!userId) { - return res.status(400).send('User not logged in.'); + return res.redirect('/login'); // ถ้าไม่ได้ล็อกอิน ให้ไปหน้า login } try { - // ดึงข้อมูลการจองของผู้ใช้จากฐานข้อมูล const query = ` - SELECT b.id, t.name AS tour_name, t.price, b.booking_date + SELECT b.id AS booking_id, t.name AS tour_name, b.status, u.name AS user_name FROM bookings b JOIN tours t ON b.tour_id = t.id + JOIN users u ON b.user_id = u.id WHERE b.user_id = ? ORDER BY b.booking_date DESC `; const [bookings] = await pool.query(query, [userId]); - // ส่งข้อมูลการจองกลับไปยังผู้ใช้ - res.render('bookinglist', { bookings }); + // ส่งข้อมูลการจองไปยังหน้า EJS + res.render('myBookings', { bookings: bookings }); } catch (error) { - console.error('Error fetching user bookings:', error.message); + console.error('Error fetching bookings:', error.message); res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูลการจอง'); } }; diff --git a/public/css/booklist.css b/public/css/booklist.css new file mode 100644 index 0000000..b3224fe --- /dev/null +++ b/public/css/booklist.css @@ -0,0 +1,55 @@ +/* booking.css */ +body { + font-family: Arial, sans-serif; + background-color: #f4f4f9; + margin: 20px; + padding: 0; + text-align: center; +} + +h1 { + color: #333; + margin-bottom: 20px; +} + +table { + width: 80%; + margin: 0 auto; + border-collapse: collapse; + background-color: white; + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); + border-radius: 8px; + overflow: hidden; +} + +th, td { + padding: 12px; + border: 1px solid #ddd; + text-align: center; +} + +th { + background-color: #007bff; + color: white; +} + +tr:nth-child(even) { + background-color: #f2f2f2; +} + +tr:hover { + background-color: #ddd; + cursor: pointer; +} + +/* Responsive */ +@media (max-width: 768px) { + table { + width: 100%; + } + + th, td { + padding: 8px; + font-size: 14px; + } +} diff --git a/views/bookinglist.ejs b/views/bookinglist.ejs index e03ac49..794511b 100644 --- a/views/bookinglist.ejs +++ b/views/bookinglist.ejs @@ -4,6 +4,7 @@ <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Bookings</title> + <link rel="stylesheet" href="/css/booklist.css"> </head> <body> <h1>Your Bookings</h1> @@ -11,16 +12,16 @@ <thead> <tr> <th>Booking ID</th> + <th>Booking User</th> <th>Tour Title</th> - <th>Status</th> </tr> </thead> <tbody> <% bookings.forEach(booking => { %> <tr> - <td><%= booking.id %></td> <!-- booking_id --> + <td><%= booking.id %></td> <!-- booking_id --> + <td><%= booking.user_name %></td> <!-- ชื่อผู้จอง --> <td><%= booking.tour_name %></td> <!-- tour_title --> - <td><%= booking.status %></td> </tr> <% }) %> </tbody> -- GitLab