From cc74b930d5b153393150affefef3553ab288c9d1 Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Thu, 20 Mar 2025 21:19:55 +0700 Subject: [PATCH] Project Round 9 --- controllers/tourController.js | 15 ++++++++++----- models/tourModel.js | 6 +++--- routes/tourRoutes.js | 2 +- views/index.ejs | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/controllers/tourController.js b/controllers/tourController.js index ae73475..ce74c9a 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 9dfa46d..b71d05f 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 a323443..bf70451 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 0fb1e3b..562cd90 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> <% } %> -- GitLab