diff --git a/controllers/tourController.js b/controllers/tourController.js index 8bea61a4b40ca4712b27022e4d03d59a3b559563..074ea6a0f0507df8dcf8179f99304dd9218bde77 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -11,14 +11,24 @@ exports.getTours = async (req, res) => { }; exports.getTourDetails = async (req, res) => { + const tourId = req.params.id; + try { - const tour = await Tour.getTourById(req.params.userId); - res.render('tour-details', { tour }); + const tour = await Tour.getTourById(tourId); // ค้นหาทัวร์จาก ID + + // ถ้าทัวร์ไม่พบ จะส่งข้อความกลับไป + if (!tour) { + return res.status(404).send('ไม่พบข้อมูลทัวร์ที่ต้องการ'); + } + + res.render('tourDetails', { tour }); } catch (error) { - res.status(500).send(error.message); + console.error('Error fetching tour by ID:', error); + res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์'); } }; + //User// exports.getLogin = (req, res) => { res.render('login', { message: null }); diff --git a/models/tourModel.js b/models/tourModel.js index 095f098dcb5c0a11c575b83ca2b6451fd3662631..c241d8fde1a862e6343147202acd31dc9c38e523 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -83,17 +83,18 @@ class Tour { // ดึงทัวร์ตาม ID - static async getTourById(id) { + static async getTourById(tourId) { + const query = 'SELECT * FROM tours WHERE id = ?'; + try { - console.log('Searching for tour with ID:', id); // เพิ่มบรรทัดนี้ - const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); - if (rows.length === 0) { - throw new Error('ไม่พบทัวร์ที่มี ID นี้'); + const [results] = await pool.execute(query, [tourId]); + if (results.length === 0) { + throw new Error(`ไม่พบทัวร์ที่มี ID ${tourId}`); } - return rows[0]; + return results[0]; // คืนค่าผลลัพธ์ที่พบ } catch (error) { console.error('Error fetching tour by ID:', error); - throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์'); + throw error; // ให้โยนข้อผิดพลาดกลับไปที่ controller } }