From 8d9a744958b2eb9ee679d4ec6e7296ebbcc2741f Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Thu, 20 Mar 2025 18:16:50 +0700 Subject: [PATCH] Project Round 8 --- controllers/tourController.js | 34 ++++++++++++++++------------------ models/tourModel.js | 8 +++++--- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/controllers/tourController.js b/controllers/tourController.js index ec7dd93..03f8f77 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -150,12 +150,9 @@ exports.createTour = async (req, res) => { } try { - // เก็บ userId ในฐานข้อมูลเมื่อสร้างทัวร์ใหม่ - await pool.query( - 'INSERT INTO tours (name, description, price, duration, id) VALUES (?, ?, ?, ?, ?)', - [name, description, price, duration, req.session.userId] // ใช้ req.session.userId เป็น owner - ); - res.redirect('/'); + // ส่ง userId ที่เก็บไว้ใน session ไปพร้อมกับข้อมูลทัวร์ + await Tour.createTour(name, description, price, duration, req.session.userId); + res.redirect('/'); // กลับไปหน้าแรกหลังจากสร้างทัวร์ } catch (error) { console.error('Error creating tour:', error); res.status(500).send('เกิดข้อผิดพลาดในการสร้างทัวร์'); @@ -163,19 +160,20 @@ exports.createTour = async (req, res) => { }; // ฟังก์ชั่นสำหรับสร้างทัวร์ exports.postCreateTour = async (req, res) => { - const { name, description, price, duration } = req.body; - - if (!name || !price || !duration) { - return res.render('createtour', { message: 'ข้อมูลไม่ครบถ้วน' }); - } + 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: 'เกิดข้อผิดพลาดในการสร้างทัวร์' }); - } + if (!name || !price || !duration) { + return res.render('createtour', { message: 'ข้อมูลไม่ครบถ้วน' }); + } + + try { + // ใช้ฟังก์ชันจากโมเดลในการสร้างทัวร์ + await Tour.createTour(name, description, price, duration, req.session.userId); + res.redirect('/'); // กลับไปหน้าแรกหลังจากสร้างเสร็จ + } catch (error) { + console.error(error); + res.render('createtour', { message: 'เกิดข้อผิดพลาดในการสร้างทัวร์' }); + } }; exports.getEditTour = async (req, res) => { try { diff --git a/models/tourModel.js b/models/tourModel.js index c33e1a7..e88fb99 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -34,21 +34,23 @@ class Tour { } // สร้างทัวร์ - static async createTour(name, description, price, duration) { + static async createTour(name, description, price, duration, userId) { // ตรวจสอบข้อมูล if (!name || !price || !duration) { throw new Error('ข้อมูลไม่ครบถ้วน กรุณากรอกข้อมูลให้ครบถ้วน'); } - const query = 'INSERT INTO tours (name, description, price, duration) VALUES (?, ?, ?, ?)'; + const query = 'INSERT INTO tours (name, description, price, duration, userId) VALUES (?, ?, ?, ?, ?)'; try { - await pool.execute(query, [name, description, price, duration]); + // บันทึกทัวร์พร้อมกับ userId ที่สร้างทัวร์ + await pool.execute(query, [name, description, price, duration, userId]); } catch (error) { console.error('Error creating tour:', error); throw new Error('เกิดข้อผิดพลาดในการสร้างทัวร์'); } } + // อัปเดตทัวร์ static async updateTour(id, tourData) { -- GitLab