diff --git a/controllers/tourController.js b/controllers/tourController.js index 43cc262bc2c12821b24f33d89839fb6971633acc..ad28754cb84bdcbfeca38ce9f747615496245f31 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -1,26 +1,22 @@ const { User, Tour } = require('../models/tourModel'); exports.getTours = async (req, res) => { - try { - const tours = await Tour.getAllTours(); - res.render('index', { - tours: tours, - session: req.session // ส่ง session ไปยัง ejs - }); - } catch (error) { - res.status(500).send('เกิดข้อผิดพลาด'); - } -}; - -exports.getTourDetails = async (req, res) => { - try { - const tour = await Tour.getTourById(req.params.id); - if (!tour) return res.status(404).send('ไม่พบข้อมูล'); - res.render('tour-details', { tour }); - } catch (error) { - res.status(500).send('เกิดข้อผิดพลาด'); - } -}; + try { + const tours = await Tour.getAllTours(); + res.render('index', { tours }); + } 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) => { @@ -85,19 +81,37 @@ exports.Logout = (req, res) => { }; //CRUD -exports.getTour = (req, res) => { - res.render('createtour', { message: null }); +// ฟังก์ชั่นสำหรับแสดงฟอร์มสร้างทัวร์ +exports.getCreateTour = (req, res) => { + res.render('createtour', { message: null }); // แสดงฟอร์มสร้างทัวร์ }; -// ฟังก์ชันสำหรับการสร้างทัวร์ + + exports.createTour = async (req, res) => { + const { name, description, price, duration } = req.body; + + try { + // สร้างทัวร์ใหม่ในฐานข้อมูล + await Tour.createTour({ name, description, price, duration }); + res.redirect('/'); // ไปยังหน้าหลักหลังจากสร้างทัวร์เสร็จ + } catch (error) { + console.error(error); + res.render('createtour', { message: error.message }); // แสดงข้อความข้อผิดพลาด + } + }; +// ฟังก์ชั่นสำหรับสร้างทัวร์ exports.postCreateTour = async (req, res) => { const { name, description, price, duration } = req.body; - const tourData = { name, description, price, duration }; + + if (!name || !price || !duration) { + return res.render('createtour', { message: 'ข้อมูลไม่ครบถ้วน' }); + } try { - await Tour.createTour(tourData); - res.redirect('/'); // หลังจากสร้างทัวร์เสร็จ ให้กลับไปหน้า home + await Tour.create(name, description, price, duration); // สร้างทัวร์ + res.redirect('/'); // กลับไปหน้าแรกหลังจากสร้างเสร็จ } catch (error) { - res.status(500).send('เกิดข้อผิดพลาดในการสร้างทัวร์'); + console.error(error); + res.render('createtour', { message: 'เกิดข้อผิดพลาดในการสร้างทัวร์' }); } }; exports.getEditTour = async (req, res) => { diff --git a/models/tourModel.js b/models/tourModel.js index ccae065cf3599a2d47323bc0b712306114fdde53..ef33ee89079bf51e703a28d810cf82bbeee05035 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -3,37 +3,76 @@ const bcrypt = require('bcryptjs'); class Tour { // ดึงทัวร์ทั้งหมด static async getAllTours() { - const [rows] = await pool.query('SELECT * FROM tours'); - return rows; + try { + const [rows] = await pool.query('SELECT * FROM tours'); + return rows; + } catch (error) { + console.error('Error fetching tours:', error); + throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์'); + } } // ดึงทัวร์ตาม ID static async getTourById(id) { - const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); - return rows[0]; + 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) { const { name, description, price, duration } = tourData; + + // ตรวจสอบข้อมูล + if (!name || !price || !duration) { + throw new Error('ข้อมูลไม่ครบถ้วน กรุณากรอกข้อมูลให้ครบถ้วน'); + } + const query = 'INSERT INTO tours (name, description, price, duration) VALUES (?, ?, ?, ?)'; - await pool.execute(query, [name, description, price, duration]); + try { + await pool.execute(query, [name, description, price, duration]); + } catch (error) { + console.error('Error creating tour:', error); + throw new Error('เกิดข้อผิดพลาดในการสร้างทัวร์'); + } } // อัปเดตทัวร์ static async updateTour(id, tourData) { const { name, description, price, duration } = tourData; + + // ตรวจสอบข้อมูล + if (!name || !price || !duration) { + throw new Error('ข้อมูลไม่ครบถ้วน กรุณากรอกข้อมูลให้ครบถ้วน'); + } + const query = 'UPDATE tours SET name = ?, description = ?, price = ?, duration = ? WHERE id = ?'; - await pool.execute(query, [name, description, price, duration, id]); + try { + await pool.execute(query, [name, description, price, duration, id]); + } catch (error) { + console.error('Error updating tour:', error); + throw new Error('เกิดข้อผิดพลาดในการอัปเดตทัวร์'); + } } // ลบทัวร์ static async deleteTour(id) { const query = 'DELETE FROM tours WHERE id = ?'; - await pool.execute(query, [id]); + try { + await pool.execute(query, [id]); + } catch (error) { + console.error('Error deleting tour:', error); + throw new Error('เกิดข้อผิดพลาดในการลบทัวร์'); + } } } - class User { static async findOne(email) { diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js index 59a0c8ba99b0f698b4c1453e5d04c323832331d5..c16a3882c9bdad5146bc5974c68a7b356d1590c7 100644 --- a/routes/tourRoutes.js +++ b/routes/tourRoutes.js @@ -14,10 +14,7 @@ router.post('/login', tourController.postLogin); // ฟอร์มเข้า router.get('/logout', tourController.Logout); // ออกจากระบบ // สร้างทัวร์ -router.get('/tour/create', tourController.getTour); // หน้าสร้างทัวร์ -// เพิ่ม route สำหรับการสร้างทัวร์โดยการ POST -router.post('/tour/create', async (req, res) => { - // logic สำหรับสร้างทัวร์ใหม่ -}); +router.get('/tour/create', tourController.getCreateTour); // หน้าสร้างทัวร์ +router.post('/tour/create', tourController.createTour); // เพิ่ม route สำหรับการสร้างทัวร์โดยการ POST module.exports = router;