diff --git a/controllers/tourController.js b/controllers/tourController.js index a3c579021e618d82804f923f11bedd9d13d22b2d..8bea61a4b40ca4712b27022e4d03d59a3b559563 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -203,31 +203,31 @@ exports.getEditTour = async (req, res) => { exports.postEditTour = async (req, res) => { const { name, description, price, duration } = req.body; + const tourId = req.params.id; // ดึง id จาก URL parameters + console.log('📥 Data received:', req.body); // ตรวจสอบข้อมูลที่ได้รับจากฟอร์ม - // ตรวจสอบว่ามีค่าครบถ้วนหรือไม่ + if (!tourId) { + return res.status(400).send('ไม่พบข้อมูลทัวร์'); + } + if (!name || !description || !price || !duration) { return res.status(400).send('กรุณากรอกข้อมูลให้ครบทุกช่อง'); } const tourData = { name, description, price, duration }; - const tourId = req.params.id; - - console.log('Tour ID from URL:', tourId); - - if (!tourId) { - return res.status(400).send('ไม่พบข้อมูลทัวร์'); - } try { - await Tour.updateTour(tourId, tourData); - res.redirect('/tour/' + tourId); // ไปที่หน้ารายละเอียดทัวร์ + const result = await Tour.updateTour(tourId, tourData); // ส่ง tourId และ tourData + console.log('Tour updated:', result); + res.redirect('/tour/' + tourId); // ไปที่หน้ารายละเอียดทัวร์ } catch (error) { - console.error('Error updating tour:', error); // ข้อผิดพลาด + console.error('Error updating tour:', error); res.status(500).send('เกิดข้อผิดพลาดในการแก้ไขทัวร์'); } }; + exports.deleteTour = async (req, res) => { try { const tour = await Tour.getTourById(req.params.userId); diff --git a/models/tourModel.js b/models/tourModel.js index 79d0d819d1a2d664bf394bc7465c497ab2e925b0..095f098dcb5c0a11c575b83ca2b6451fd3662631 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -58,17 +58,20 @@ class Tour { // อัปเดตทัวร์ - static async updateTour(tourData) { + static async updateTour(tourId, tourData) { const { name, description, price, duration } = tourData; + // ตรวจสอบข้อมูลให้ครบถ้วน if (!name || !description || !price || !duration) { throw new Error('ข้อมูลไม่ครบถ้วน'); } + // สร้างคำสั่ง SQL สำหรับการอัปเดตข้อมูล const query = 'UPDATE tours SET name = ?, description = ?, price = ?, duration = ? WHERE id = ?'; try { - const [results] = await pool.execute(query, [name, description, price, duration]); + // ใช้ tourId ที่รับมาในการอัปเดต + const [results] = await pool.execute(query, [name, description, price, duration, tourId]); console.log('✅ Update results:', results); return results; } catch (error) { @@ -77,6 +80,7 @@ class Tour { } } + // ดึงทัวร์ตาม ID static async getTourById(id) {