From 28ad9d57750b92edf089ebe30b55e11b9c136d83 Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Thu, 20 Mar 2025 22:49:19 +0700 Subject: [PATCH] Project Round 9 --- controllers/tourController.js | 34 ++++++++++++++++++++++++---------- models/tourModel.js | 29 +++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/controllers/tourController.js b/controllers/tourController.js index e9e6f6e..e098cd7 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -201,15 +201,31 @@ exports.postCreateTour = async (req, res) => { exports.getEditTour = async (req, res) => { try { const tour = await Tour.getTourById(req.params.id); - if (!tour) return res.status(404).send('ไม่พบข้อมูลทัวร์'); - if (tour.userId !== req.session.userId) { + + // ตรวจสอบค่าที่ได้จากฐานข้อมูล + console.log("Tour data:", tour); + console.log("Session user ID:", req.session.userId); + + if (!tour) { + return res.status(404).send('ไม่พบข้อมูลทัวร์'); + } + + // ตรวจสอบว่ามี userId ใน tour หรือไม่ + if (!tour.user_id) { + return res.status(500).send('ข้อมูลทัวร์ไม่สมบูรณ์'); + } + + if (tour.user_id !== req.session.userId) { return res.status(403).send('คุณไม่มีสิทธิ์แก้ไขทัวร์นี้'); } + res.render('edittour', { tour }); } catch (error) { + console.error("Error fetching tour:", error); res.status(500).send('เกิดข้อผิดพลาด'); } }; + exports.postEditTour = async (req, res) => { const { name, description, price, duration } = req.body; @@ -240,18 +256,16 @@ exports.postEditTour = async (req, res) => { exports.deleteTour = async (req, res) => { try { - const tour = await Tour.getTourById(req.params.userId); - if (!tour) return res.status(404).send('ไม่พบข้อมูลทัวร์'); - if (tour.userId !== req.session.userId) { - return res.status(403).send('คุณไม่มีสิทธิ์ลบทัวร์นี้'); - } - await Tour.deleteTour(req.params.userId); // ลบทัวร์ที่ผู้ใช้เป็นเจ้าของ - res.redirect('/'); // ไปที่หน้า home + console.log("Tour ID ที่ต้องการลบ:", req.params.id); + await Tour.deleteTour(req.params.id); + res.redirect('/tours'); // กลับไปที่หน้ารายการทัวร์ } catch (error) { - res.status(500).send('เกิดข้อผิดพลาดในการลบทัวร์'); + console.error(error.message); + res.status(500).send(error.message); } }; + //ค้นหาทัวร์ exports.searchTours = async (req, res) => { try { diff --git a/models/tourModel.js b/models/tourModel.js index d64376b..20924e3 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -84,31 +84,48 @@ class Tour { // ดึงทัวร์ตาม ID static async getTourById(tourId) { - const query = 'SELECT * FROM tours WHERE id = ?'; + const query = 'SELECT id, name, description, price, user_id FROM tours WHERE id = ?'; try { const [results] = await pool.execute(query, [tourId]); if (results.length === 0) { throw new Error(`ไม่พบทัวร์ที่มี ID ${tourId}`); } - return results[0]; // คืนค่าผลลัพธ์ที่พบ + return results[0]; // คืนค่าทัวร์ที่พบ } catch (error) { console.error('Error fetching tour by ID:', error); - throw error; // ให้โยนข้อผิดพลาดกลับไปที่ controller + throw error; // ส่งข้อผิดพลาดกลับไปที่ controller } } + // ลบทัวร์ static async deleteTour(id) { - const query = 'DELETE FROM tours WHERE id = ?'; try { - await pool.execute(query, [id]); + // ตรวจสอบก่อนว่ามีทัวร์อยู่หรือไม่ + const checkQuery = 'SELECT * FROM tours WHERE id = ?'; + const [tours] = await pool.execute(checkQuery, [id]); + + if (tours.length === 0) { + throw new Error(`ไม่พบทัวร์ที่ต้องการลบ ID: ${id}`); + } + + // ลบ bookings ที่เกี่ยวข้องก่อน + const deleteBookingsQuery = 'DELETE FROM bookings WHERE tour_id = ?'; + await pool.execute(deleteBookingsQuery, [id]); + + // ลบ tour หลังจาก bookings ถูกลบแล้ว + const deleteTourQuery = 'DELETE FROM tours WHERE id = ?'; + const [result] = await pool.execute(deleteTourQuery, [id]); + + console.log("Delete result:", result); + return result; } catch (error) { console.error('Error deleting tour:', error); throw new Error('เกิดข้อผิดพลาดในการลบทัวร์'); } } -} +} class User { static async findOne(email) { -- GitLab