diff --git a/controllers/tourController.js b/controllers/tourController.js index 03f8f77631c2631ca19888e47d3460d2d4e04d8a..154aefd01a9ce77e40e56a5a68f67fbe78c77c65 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -148,7 +148,13 @@ exports.createTour = async (req, res) => { if (!name || !description || !price || !duration) { return res.status(400).send('กรุณากรอกข้อมูลให้ครบทุกช่อง'); } + // แปลง userId ให้เป็นตัวเลข + const userId = parseInt(req.session.userId, 10); + if (isNaN(userId)) { + return res.status(400).send('User ID is not valid'); + } + try { // ส่ง userId ที่เก็บไว้ใน session ไปพร้อมกับข้อมูลทัวร์ await Tour.createTour(name, description, price, duration, req.session.userId); diff --git a/models/tourModel.js b/models/tourModel.js index e88fb991fce2911b25b303724c638de1e801e3fc..1440f841689eb9d068bd285e90bebbeac5638153 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -39,18 +39,23 @@ class Tour { if (!name || !price || !duration) { throw new Error('ข้อมูลไม่ครบถ้วน กรุณากรอกข้อมูลให้ครบถ้วน'); } - + + // ตรวจสอบว่า userId เป็นตัวเลขหรือไม่ + const validUserId = parseInt(userId, 10); + if (isNaN(validUserId)) { + throw new Error('User ID ไม่ถูกต้อง'); + } + const query = 'INSERT INTO tours (name, description, price, duration, userId) VALUES (?, ?, ?, ?, ?)'; try { // บันทึกทัวร์พร้อมกับ userId ที่สร้างทัวร์ - await pool.execute(query, [name, description, price, duration, userId]); + await pool.execute(query, [name, description, price, duration, validUserId]); } catch (error) { console.error('Error creating tour:', error); throw new Error('เกิดข้อผิดพลาดในการสร้างทัวร์'); } } - - + // อัปเดตทัวร์ static async updateTour(id, tourData) {