diff --git a/controllers/tourController.js b/controllers/tourController.js index 3d648c5b1183037bfd27ae16e5955df515feb3a4..ec7dd93fd78f6a8b93cea3bb25b649450896e357 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -41,8 +41,8 @@ exports.postRegister = async (req, res) => { return res.render('register', { message: 'Email already in use' }); } - const id = await User.create(name, email, password); - req.session.id = id; + const userId = await User.create(name, email, password); + req.session.userId = userId; 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.id = user.email; // เก็บ email ใน session + req.session.userId = user.email; // เก็บ email ใน session req.session.userName = user.name; // เก็บชื่อผู้ใช้ใน session res.redirect('/'); // เมื่อ login สำเร็จให้ไปหน้า home } catch (err) { @@ -75,14 +75,18 @@ exports.postLogin = async (req, res) => { }; exports.Logout = (req, res) => { - req.session.destroy(() => { - res.redirect('/'); + req.session.destroy((err) => { + if (err) { + return res.status(500).send('เกิดข้อผิดพลาดในการออกจากระบบ'); + } + res.redirect('/login'); // หลังจาก logout สำเร็จ ให้ redirect ไปหน้า login }); }; + //Profile exports.getProfilePage = (req, res) => { - if (req.session.id) { + if (req.session.userId) { const user = req.session.userName; res.render('profile', { user, session: req.session }); } else { @@ -91,12 +95,12 @@ exports.getProfilePage = (req, res) => { }; exports.getEditProfilePage = async (req, res) => { - if (!req.session.id) { + if (!req.session.userId) { return res.redirect('/profile'); } try { - const user = await User.findById(req.session.id); + const user = await User.findById(req.session.userId); res.render('edit-profile', { user }); } catch (err) { console.error(err); @@ -112,7 +116,7 @@ exports.updateProfile = async (req, res) => { } try { - let user = await User.findById(req.session.id); + let user = await User.findById(req.session.userId); // อัปเดตชื่อและอีเมล user.name = name; @@ -146,10 +150,10 @@ exports.createTour = async (req, res) => { } try { - // เก็บ id ในฐานข้อมูลเมื่อสร้างทัวร์ใหม่ + // เก็บ userId ในฐานข้อมูลเมื่อสร้างทัวร์ใหม่ await pool.query( 'INSERT INTO tours (name, description, price, duration, id) VALUES (?, ?, ?, ?, ?)', - [name, description, price, duration, req.session.id] // ใช้ req.session.id เป็น owner + [name, description, price, duration, req.session.userId] // ใช้ req.session.userId เป็น owner ); res.redirect('/'); } catch (error) { @@ -177,7 +181,7 @@ exports.getEditTour = async (req, res) => { try { const tour = await Tour.getTourById(req.params.id); if (!tour) return res.status(404).send('ไม่พบข้อมูลทัวร์'); - if (tour.id !== req.session.id) { + if (tour.userId !== req.session.userId) { return res.status(403).send('คุณไม่มีสิทธิ์แก้ไขทัวร์นี้'); } res.render('edittour', { tour }); @@ -203,7 +207,7 @@ exports.deleteTour = async (req, res) => { try { const tour = await Tour.getTourById(req.params.id); if (!tour) return res.status(404).send('ไม่พบข้อมูลทัวร์'); - if (tour.id !== req.session.id) { + if (tour.userId !== req.session.userId) { return res.status(403).send('คุณไม่มีสิทธิ์ลบทัวร์นี้'); } await Tour.deleteTour(req.params.id); // ลบทัวร์ที่ผู้ใช้เป็นเจ้าของ @@ -235,7 +239,7 @@ exports.searchTours = async (req, res) => { //จองทัวร์ exports.getUserBookings = async (req, res) => { try { - const bookings = await Booking.getUserBookings(req.session.id); + const bookings = await Booking.getUserBookings(req.session.userId); res.render('myBookings', { bookings }); } catch (error) { res.status(500).json({ message: 'เกิดข้อผิดพลาดในการดึงข้อมูลการจอง' }); @@ -245,7 +249,7 @@ exports.getUserBookings = async (req, res) => { exports.createBooking = async (req, res) => { const { tourId } = req.body; try { - await Booking.createBooking(req.session.id, tourId); + await Booking.createBooking(req.session.userId, tourId); res.redirect('/bookings/my-bookings'); } catch (error) { res.status(500).json({ message: 'เกิดข้อผิดพลาดในการจองทัวร์' }); @@ -254,7 +258,7 @@ exports.createBooking = async (req, res) => { exports.cancelBooking = async (req, res) => { try { - const deleted = await Booking.cancelBooking(req.session.id, req.params.id); + const deleted = await Booking.cancelBooking(req.session.userId, 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 e4277b7b965d7c5896398e828c727302aa7e8717..c33e1a7dcfe43a222251a927c4fab0adf342c18a 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(id) { + static async getAllTours(userId) { try { - console.log('Fetching tours for user:', id); + console.log('Fetching tours for user:', userId); - // ดึงทัวร์ทั้งหมดที่ id ตรงกับผู้ใช้ที่ล็อกอิน - const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); + // ดึงทัวร์ทั้งหมดที่ userId ตรงกับผู้ใช้ที่ล็อกอิน + const [rows] = await pool.query('SELECT * FROM tours WHERE Userid = ?', [userId]); console.log('Tours fetched successfully:', rows); return rows; @@ -131,7 +131,7 @@ class User { class Booking { // ดึงรายการจองของผู้ใช้ - static async getUserBookings(id) { + static async getUserBookings(userId) { 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, [id]); + const [rows] = await pool.query(query, [userId]); return rows; } // ทำการจองทัวร์ - static async createBooking(id, tourId) { + static async createBooking(userId, tourId) { const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)'; - await pool.execute(query, [id, tourId]); + await pool.execute(query, [userId, tourId]); } // ยกเลิกการจอง - static async cancelBooking(id, bookingId) { + static async cancelBooking(userId, bookingId) { const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?'; - const [result] = await pool.execute(query, [bookingId, id]); + const [result] = await pool.execute(query, [bookingId, userId]); return result.affectedRows; } } diff --git a/views/index.ejs b/views/index.ejs index 91a70828d77b485de3307047e06310da0c7e0b06..48afbf28a04238863b51acdcc01f842deb11f8d8 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -19,7 +19,7 @@ <button type="submit">Search</button> </form> - <% if (session && session.id) { %> <!-- ตรวจสอบ session --> + <% if (session && session.userId) { %> <!-- ตรวจสอบ session --> <a href="/logout">Logout</a> <a href="/my-bookings">My-Booking</a> <% } else { %>