diff --git a/controllers/tourController.js b/controllers/tourController.js index ae73475cf2279766234f9f75d328309cc22d825b..ce74c9a4567dc641d7f865cdc1eb81dd30a8eacc 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -276,12 +276,17 @@ exports.searchTours = async (req, res) => { exports.getAllTours = async (req, res) => { try { - const tours = await Tour.getAllTours(); // ดึงข้อมูลทัวร์จากฐานข้อมูล - console.log(tours); // ตรวจสอบว่าได้ข้อมูลหรือไม่ใน console - res.render('booking', { tours }); // ส่งข้อมูลทัวร์ไปยังหน้า booking.ejs + const tours = await Tour.getAllTours(); // เรียกใช้ฟังก์ชันดึงทัวร์ทั้งหมด + + // ตรวจสอบว่ามีทัวร์หรือไม่ + if (tours.length === 0) { + return res.render('booking', { tours: [], message: 'No tours available at the moment.' }); + } + + res.render('booking', { tours }); // ส่งทัวร์ทั้งหมดไปที่หน้า booking.ejs } catch (error) { - console.error('Error fetching tours:', error.message); - res.status(500).send('Internal Server Error'); + console.error('Error fetching tours:', error.message); + res.status(500).send('Internal Server Error'); } }; diff --git a/models/tourModel.js b/models/tourModel.js index 9dfa46d6b205516aa460bbe21ccaf5691dae8725..b71d05f2020e45054e9bf0f55da1ae86c78472dd 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -4,10 +4,10 @@ class Tour { // ดึงทัวร์ทั้งหมด static async getAllTours(userId) { try { - console.log('Fetching tours for user:', userId); - + console.log('Fetching all tours'); + // ดึงทัวร์ทั้งหมดที่ userId ตรงกับผู้ใช้ที่ล็อกอิน - const [rows] = await pool.query('SELECT * FROM tours WHERE Userid = ?', [userId]); + const [rows] = await pool.query('SELECT * FROM tours'); console.log('Tours fetched successfully:', rows); return rows; diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js index a323443e92b1c0feaf71f2aabe0df93b0f5c39f7..bf70451e97f53aba27968e09005bafec487c1c16 100644 --- a/routes/tourRoutes.js +++ b/routes/tourRoutes.js @@ -35,7 +35,7 @@ router.post('/edit-profile', tourController.updateProfile);// อัปเดต //จองทัวร์ router.get('/my-bookings', tourController.getUserBookings); router.get('/createbooking', tourController.getAllTours); -router.post('/createbooking', tourController.createBooking); +router.post('/create-booking', tourController.createBooking); router.post('/cancel/:id', tourController.cancelBooking); module.exports = router; diff --git a/views/index.ejs b/views/index.ejs index 0fb1e3bdec3573dabd4839ca0088c8d480988217..562cd905519f6dd01f7258e8ad358defafdf241f 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -11,7 +11,7 @@ <header> <h1>Welcome to JourneyHub</h1> <nav> - <% if (session.id) { %> + <% if (session && session.id) { %> <a href="/create" class="btn-create">Create Tour</a> <a href="/createbooking" class="btn-create">Create Booking</a> <% } %>