diff --git a/controllers/tourController.js b/controllers/tourController.js index db5b3d40c13d08d08cee25ea8b1fbc164a083c0a..3cbf27d9faa35d43eadb45188fa8de0d1b6f9c91 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -1,22 +1,22 @@ const { User, Tour } = require('../models/tourModel'); -// exports.getTours = async (req, res) => { -// try { -// const tours = await Tour.getAllTours(); -// res.render('index', { tours,session: req.session }); -// } catch (error) { -// res.status(500).send(error.message); -// } -// }; +exports.getTours = async (req, res) => { + try { + const tours = await Tour.getAllTours(); + res.render('index', { tours,session: req.session }); + } catch (error) { + res.status(500).send(error.message); + } + }; -// exports.getTourDetails = async (req, res) => { -// try { -// const tour = await Tour.getTourById(req.params.id); -// res.render('tour-details', { tour }); -// } catch (error) { -// res.status(500).send(error.message); -// } -// }; + exports.getTourDetails = async (req, res) => { + try { + const tour = await Tour.getTourById(req.params.id); + res.render('tour-details', { tour }); + } catch (error) { + res.status(500).send(error.message); + } + }; //User// exports.getLogin = (req, res) => { diff --git a/models/tourModel.js b/models/tourModel.js index b51b1a526afc5b4e8f9a09a4e3ff9d02787cef86..e19e47e00b50b6724c765e28d945a8408fb29994 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -1,32 +1,32 @@ const pool = require('../config/database'); const bcrypt = require('bcryptjs'); class Tour { - // // ดึงทัวร์ทั้งหมด - // static async getAllTours() { - // try { - // console.log('Fetching all tours...'); - // const [rows] = await pool.query('SELECT * FROM tours'); - // console.log('Tours fetched successfully:', rows); - // return rows; - // } catch (error) { - // console.error('Error fetching tours:', error.message); - // throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์: ' + error.message); - // } - // } + // ดึงทัวร์ทั้งหมด + static async getAllTours() { + try { + console.log('Fetching all tours...'); + const [rows] = await pool.query('SELECT * FROM tours'); + console.log('Tours fetched successfully:', rows); + return rows; + } catch (error) { + console.error('Error fetching tours:', error.message); + throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์: ' + error.message); + } + } - // // ดึงทัวร์ตาม ID - // static async getTourById(id) { - // try { - // const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); - // if (rows.length === 0) { - // throw new Error('ไม่พบทัวร์ที่มี ID นี้'); - // } - // return rows[0]; - // } catch (error) { - // console.error('Error fetching tour by ID:', error); - // throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์'); - // } - // } + // ดึงทัวร์ตาม ID + static async getTourById(id) { + try { + const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); + if (rows.length === 0) { + throw new Error('ไม่พบทัวร์ที่มี ID นี้'); + } + return rows[0]; + } catch (error) { + console.error('Error fetching tour by ID:', error); + throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์'); + } + } // สร้างทัวร์ static async createTour(tourData) { diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js index c16a3882c9bdad5146bc5974c68a7b356d1590c7..3887eb931f780338fe356f5185e6996f68554daa 100644 --- a/routes/tourRoutes.js +++ b/routes/tourRoutes.js @@ -14,7 +14,7 @@ router.post('/login', tourController.postLogin); // ฟอร์มเข้า router.get('/logout', tourController.Logout); // ออกจากระบบ // สร้างทัวร์ -router.get('/tour/create', tourController.getCreateTour); // หน้าสร้างทัวร์ -router.post('/tour/create', tourController.createTour); // เพิ่ม route สำหรับการสร้างทัวร์โดยการ POST +router.get('/create', tourController.getCreateTour); // หน้าสร้างทัวร์ +router.post('/create', tourController.createTour); // เพิ่ม route สำหรับการสร้างทัวร์โดยการ POST module.exports = router;