diff --git a/controllers/tourController.js b/controllers/tourController.js index 37a8791ba061ec0238a32c2b9ef5e386d38dc266..8be7fe3c63865542e4be48bb148fdcc4196d7851 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -1,4 +1,4 @@ -const { User, Tour } = require('../models/tourModel'); +const { User, Tour , Booking } = require('../models/tourModel'); const pool = require('../config/database'); exports.getTours = async (req, res) => { @@ -223,3 +223,33 @@ exports.searchTours = async (req, res) => { res.status(500).send("Error searching tours"); } }; + +//จองทัวร์ +exports.getUserBookings = async (req, res) => { + try { + const bookings = await Booking.getUserBookings(req.session.userId); + res.render('myBookings', { bookings }); + } catch (error) { + res.status(500).json({ message: 'เกิดข้อผิดพลาดในการดึงข้อมูลการจอง' }); + } +}; + +exports.createBooking = async (req, res) => { + const { tourId } = req.body; + try { + await Booking.createBooking(req.session.userId, tourId); + res.redirect('/bookings/my-bookings'); + } catch (error) { + res.status(500).json({ message: 'เกิดข้อผิดพลาดในการจองทัวร์' }); + } +}; + +exports.cancelBooking = async (req, res) => { + try { + 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) { + res.status(500).json({ message: 'เกิดข้อผิดพลาดในการยกเลิกการจอง' }); + } +}; \ No newline at end of file diff --git a/models/tourModel.js b/models/tourModel.js index ff73644f3db13172e1abb61f7e45470e356a4279..43b662a5ffe9479ea1ba048e5cacec1da6f37df3 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -126,4 +126,33 @@ class User { } } -module.exports = { User, Tour }; +class Booking { + // ดึงรายการจองของผู้ใช้ + static async getUserBookings(userId) { + const query = ` + SELECT b.id, t.name AS tour_name, t.price, b.booking_date + FROM bookings b + JOIN tours t ON b.tour_id = t.id + WHERE b.user_id = ? + ORDER BY b.booking_date DESC + `; + const [rows] = await pool.query(query, [userId]); + return rows; + } + + // ทำการจองทัวร์ + static async createBooking(userId, tourId) { + const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)'; + await pool.execute(query, [userId, tourId]); + } + + // ยกเลิกการจอง + static async cancelBooking(userId, bookingId) { + const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?'; + const [result] = await pool.execute(query, [bookingId, userId]); + return result.affectedRows; + } +} + + +module.exports = { User, Tour, Booking }; diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js index 1c39b6841701c837fc1c8df0eee03d1cdeb04f4b..2aeadb7156d686d67ab20168bb7b022741e7730d 100644 --- a/routes/tourRoutes.js +++ b/routes/tourRoutes.js @@ -32,4 +32,9 @@ router.get('/profile', tourController.getProfilePage);//หน้าโปรไ router.get('/edit-profile', tourController.getEditProfilePage);// อัปเดตข้อมูลโปรไฟล์ router.post('/edit-profile', tourController.updateProfile);// อัปเดตข้อมูลโปรไฟล์ +//จองทัวร์ +router.get('/my-bookings', tourController.getUserBookings); +router.post('/create', tourController.createBooking); +router.post('/cancel/:id', tourController.cancelBooking); + module.exports = router; diff --git a/server.js b/server.js index a0e53850387a43f1794cfb9415af75d2a2d9f1ab..9e1ad71cbe4258181da8f2aad788425edccdb6a9 100644 --- a/server.js +++ b/server.js @@ -26,6 +26,7 @@ app.use('/login', tourRoutes); app.use('/register', tourRoutes); app.use('/create', tourRoutes); app.use('/profile', tourRoutes); +app.use('/bookings', tourRoutes); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { diff --git a/views/index.ejs b/views/index.ejs index fb4b7f0475fead06993ffe3a092b924695082575..b1d8f2b0e622ba2708d3dc002232e63e23ea3813 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -18,7 +18,7 @@ <input type="text" name="query" placeholder="Search for tours..." required> <button type="submit">Search</button> </form> - <% if (session && session.userId) { %> <!-- ตรวจสอบว่า userId อยู่ใน session หรือไม่ --> + <% if (session && session.userId) { %> <!-- ตรวจสอบ session --> <a href="/logout">Logout</a> <% } else { %> <a href="/login" class="login-btn">Login</a> <!-- ถ้ายังไม่ได้ login --> @@ -33,7 +33,7 @@ <li> <a href="/tour/<%= tour.id %>"><%= tour.name %></a> - <%= tour.price %> THB - <% if (session.userId) { %> <!-- ตรวจสอบว่าเป็นผู้ใช้ที่ล็อกอินแล้วหรือไม่ --> + <% if (session.userId) { %> <a href="/edit/<%= tour.id %>" class="btn-edit">Edit</a> <!-- ปุ่มสำหรับแก้ไขทัวร์ --> <a href="/delete/<%= tour.id %>" class="btn-delete">Delete</a> <!-- ปุ่มสำหรับลบทัวร์ --> <% } %> diff --git a/views/myBookings.ejs b/views/myBookings.ejs new file mode 100644 index 0000000000000000000000000000000000000000..6fabbe29615cbc8357f4994a9895597f23a6c8e7 --- /dev/null +++ b/views/myBookings.ejs @@ -0,0 +1,23 @@ +<!DOCTYPE html> +<html lang="th"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>My Bookings</title> +</head> +<body> + <h1>รายการจองของฉัน</h1> + <ul> + <% bookings.forEach(booking => { %> + <li> + <strong><%= booking.tour_name %></strong> - <%= booking.price %> บาท + (จองเมื่อ <%= booking.booking_date.toISOString().split('T')[0] %>) + <form action="/bookings/cancel/<%= booking.id %>" method="POST" style="display:inline;"> + <button type="submit">ยกเลิกการจอง</button> + </form> + </li> + <% }); %> + </ul> + <a href="/tours/my-tours">กลับไปหน้าทัวร์</a> +</body> +</html>