From 6494da1e837786aba4c84bb046da7acda8a39e3e Mon Sep 17 00:00:00 2001
From: Atiwit Pattanapukdee <65160394@go.buu.ac.th>
Date: Thu, 20 Mar 2025 17:54:13 +0700
Subject: [PATCH] Project Round 8

---
 controllers/tourController.js | 32 ++++++++++++++++----------------
 models/tourModel.js           | 20 ++++++++++----------
 2 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/controllers/tourController.js b/controllers/tourController.js
index 2ef88b9..3d648c5 100644
--- a/controllers/tourController.js
+++ b/controllers/tourController.js
@@ -3,7 +3,7 @@ const pool = require('../config/database');
 
 exports.getTours = async (req, res) => {
   try {
-    const tours = await Tour.getAllTours(req.session.userId); // ดึงทัวร์ที่สร้างโดยผู้ใช้ที่ล็อกอิน
+    const tours = await Tour.getAllTours(req.session.id); // ดึงทัวร์ที่สร้างโดยผู้ใช้ที่ล็อกอิน
     res.render('index', { tours, session: req.session });
   } catch (error) {
     res.status(500).send(error.message);
@@ -41,8 +41,8 @@ exports.postRegister = async (req, res) => {
           return res.render('register', { message: 'Email already in use' });
       }
 
-      const userId = await User.create(name, email, password);
-      req.session.userId = userId;
+      const id = await User.create(name, email, password);
+      req.session.id = id;
       res.redirect('/login');
   } catch (err) {
       console.error(err);
@@ -65,7 +65,7 @@ exports.postLogin = async (req, res) => {
           return res.render('login', { message: 'Invalid email or password' });
       }
 
-      req.session.userId = user.email; // เก็บ email ใน session
+      req.session.id = user.email; // เก็บ email ใน session
       req.session.userName = user.name; // เก็บชื่อผู้ใช้ใน session
       res.redirect('/'); // เมื่อ login สำเร็จให้ไปหน้า home
   } catch (err) {
@@ -82,7 +82,7 @@ exports.Logout = (req, res) => {
 
 //Profile
 exports.getProfilePage = (req, res) => {
-  if (req.session.userId) {
+  if (req.session.id) {
     const user = req.session.userName;
     res.render('profile', { user, session: req.session });
   } else {
@@ -91,12 +91,12 @@ exports.getProfilePage = (req, res) => {
 };
 
 exports.getEditProfilePage = async (req, res) => {
-  if (!req.session.userId) {
+  if (!req.session.id) {
     return res.redirect('/profile');
   }
 
   try {
-    const user = await User.findById(req.session.userId);
+    const user = await User.findById(req.session.id);
     res.render('edit-profile', { user });
   } catch (err) {
     console.error(err);
@@ -112,7 +112,7 @@ exports.updateProfile = async (req, res) => {
   }
 
   try {
-    let user = await User.findById(req.session.userId);
+    let user = await User.findById(req.session.id);
 
     // อัปเดตชื่อและอีเมล
     user.name = name;
@@ -146,10 +146,10 @@ exports.createTour = async (req, res) => {
   }
 
   try {
-    // เก็บ userId ในฐานข้อมูลเมื่อสร้างทัวร์ใหม่
+    // เก็บ id ในฐานข้อมูลเมื่อสร้างทัวร์ใหม่
     await pool.query(
-      'INSERT INTO tours (name, description, price, duration, userId) VALUES (?, ?, ?, ?, ?)',
-      [name, description, price, duration, req.session.userId] // ใช้ req.session.userId เป็น owner
+      'INSERT INTO tours (name, description, price, duration, id) VALUES (?, ?, ?, ?, ?)',
+      [name, description, price, duration, req.session.id] // ใช้ req.session.id เป็น owner
     );
     res.redirect('/'); 
   } catch (error) {
@@ -177,7 +177,7 @@ 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) {
+    if (tour.id !== req.session.id) {
       return res.status(403).send('คุณไม่มีสิทธิ์แก้ไขทัวร์นี้');
     }
     res.render('edittour', { tour });
@@ -203,7 +203,7 @@ exports.deleteTour = 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) {
+    if (tour.id !== req.session.id) {
       return res.status(403).send('คุณไม่มีสิทธิ์ลบทัวร์นี้');
     }
     await Tour.deleteTour(req.params.id); // ลบทัวร์ที่ผู้ใช้เป็นเจ้าของ
@@ -235,7 +235,7 @@ exports.searchTours = async (req, res) => {
 //จองทัวร์
 exports.getUserBookings = async (req, res) => {
   try {
-      const bookings = await Booking.getUserBookings(req.session.userId);
+      const bookings = await Booking.getUserBookings(req.session.id);
       res.render('myBookings', { bookings });
   } catch (error) {
       res.status(500).json({ message: 'เกิดข้อผิดพลาดในการดึงข้อมูลการจอง' });
@@ -245,7 +245,7 @@ exports.getUserBookings = async (req, res) => {
 exports.createBooking = async (req, res) => {
   const { tourId } = req.body;
   try {
-      await Booking.createBooking(req.session.userId, tourId);
+      await Booking.createBooking(req.session.id, tourId);
       res.redirect('/bookings/my-bookings');
   } catch (error) {
       res.status(500).json({ message: 'เกิดข้อผิดพลาดในการจองทัวร์' });
@@ -254,7 +254,7 @@ exports.createBooking = async (req, res) => {
 
 exports.cancelBooking = async (req, res) => {
   try {
-      const deleted = await Booking.cancelBooking(req.session.userId, req.params.id);
+      const deleted = await Booking.cancelBooking(req.session.id, req.params.id);
       if (deleted === 0) return res.status(403).json({ message: 'ไม่มีสิทธิ์ยกเลิกการจองนี้' });
       res.redirect('/bookings/my-bookings');
   } catch (error) {
diff --git a/models/tourModel.js b/models/tourModel.js
index 4502b33..e4277b7 100644
--- a/models/tourModel.js
+++ b/models/tourModel.js
@@ -2,12 +2,12 @@ const pool = require('../config/database');
 const bcrypt = require('bcryptjs');
 class Tour {
     // ดึงทัวร์ทั้งหมด
-    static async getAllTours(userId) {
+    static async getAllTours(id) {
       try {
-        console.log('Fetching tours for user:', userId);
+        console.log('Fetching tours for user:', id);
     
-        // ดึงทัวร์ทั้งหมดที่ userId ตรงกับผู้ใช้ที่ล็อกอิน
-        const [rows] = await pool.query('SELECT * FROM tours WHERE userId = ?', [userId]);
+        // ดึงทัวร์ทั้งหมดที่ id ตรงกับผู้ใช้ที่ล็อกอิน
+        const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]);
     
         console.log('Tours fetched successfully:', rows);
         return rows;
@@ -131,7 +131,7 @@ class User {
 
 class Booking {
   // ดึงรายการจองของผู้ใช้
-  static async getUserBookings(userId) {
+  static async getUserBookings(id) {
       const query = `
           SELECT b.id, t.name AS tour_name, t.price, b.booking_date
           FROM bookings b
@@ -139,20 +139,20 @@ class Booking {
           WHERE b.user_id = ?
           ORDER BY b.booking_date DESC
       `;
-      const [rows] = await pool.query(query, [userId]);
+      const [rows] = await pool.query(query, [id]);
       return rows;
   }
 
   // ทำการจองทัวร์
-  static async createBooking(userId, tourId) {
+  static async createBooking(id, tourId) {
       const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)';
-      await pool.execute(query, [userId, tourId]);
+      await pool.execute(query, [id, tourId]);
   }
 
   // ยกเลิกการจอง
-  static async cancelBooking(userId, bookingId) {
+  static async cancelBooking(id, bookingId) {
       const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?';
-      const [result] = await pool.execute(query, [bookingId, userId]);
+      const [result] = await pool.execute(query, [bookingId, id]);
       return result.affectedRows;
   }
 }
-- 
GitLab