From c2bdb7933b78f68b67148b0005c7b0993df5a953 Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Tue, 18 Mar 2025 12:53:47 +0700 Subject: [PATCH] Project Round 3 --- controllers/tourController.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/controllers/tourController.js b/controllers/tourController.js index 3a84e6f..3cbf27d 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -81,25 +81,30 @@ exports.Logout = (req, res) => { }; //CRUD -// ฟังก์ชั่นสำหรับแสดงฟอร์มสร้างทัวร์ +// แสดงฟอร์มสร้างทัวร์ exports.getCreateTour = (req, res) => { - res.render('createtour', { session: req.session }); + res.render('createTour'); }; +// บันทึกทัวร์ใหม่ exports.createTour = async (req, res) => { - const { name, description, price, duration } = req.body; - - try { - if (!name || !description || !price || !duration) { - return res.render('createtour', { message: 'All fields are required!' }); - } + try { + const { name, location, price, description } = req.body; - await Tour.createTour({ name, description, price, duration }); // เพิ่มข้อมูลทัวร์ - res.redirect('/'); // หลังจากสร้างเสร็จให้กลับไปที่หน้าแรก - } catch (error) { - console.error('Error creating tour:', error); - res.render('createtour', { message: 'Error creating tour' }); + // ตรวจสอบว่าข้อมูลถูกต้อง + if (!name || !location || !price || !description) { + return res.status(400).send('กรุณากรอกข้อมูลให้ครบทุกช่อง'); } + + await pool.query( + 'INSERT INTO tours (name, location, price, description) VALUES (?, ?, ?, ?)', + [name, location, price, description] + ); + res.redirect('/'); + } catch (error) { + console.error('Error creating tour:', error); + res.status(500).send('เกิดข้อผิดพลาดในการสร้างทัวร์'); + } }; // ฟังก์ชั่นสำหรับสร้างทัวร์ exports.postCreateTour = async (req, res) => { -- GitLab