From e223d6a9437bad27ae1f034a4ba5ce8bd0cdc01f Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Thu, 20 Mar 2025 21:02:27 +0700 Subject: [PATCH] Project Round 8 --- controllers/tourController.js | 47 ++++++++++++++++++----------------- models/tourModel.js | 30 ++++++++++++++-------- views/myBookings.ejs | 42 ++++++++++++++++++------------- 3 files changed, 68 insertions(+), 51 deletions(-) diff --git a/controllers/tourController.js b/controllers/tourController.js index 05c42a9..04244f4 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -271,32 +271,33 @@ exports.searchTours = async (req, res) => { } }; + //จองทัวร์ -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.getUserBookings = (req, res) => { + const userId = req.user.id; // สมมติว่า req.user เก็บข้อมูลผู้ใช้ที่ล็อกอินแล้ว + bookingModel.getBookings(userId, (err, bookings) => { + if (err) return res.status(500).json({ error: err.message }); + res.render('bookingView', { bookings }); + }); }; -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.createBooking = (req, res) => { + const { userId, tourId } = req.body; + bookingModel.createBooking(userId, tourId, (err, result) => { + if (err) return res.status(500).json({ error: err.message }); + res.status(201).json({ message: 'Booking created successfully' }); + }); }; -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: 'เกิดข้อผิดพลาดในการยกเลิกการจอง' }); - } +// ยกเลิกการจอง +exports.cancelBooking = (req, res) => { + const bookingId = req.params.id; + bookingModel.cancelBooking(bookingId, (err, result) => { + if (err) return res.status(500).json({ error: err.message }); + if (result.affectedRows === 0) { + return res.status(404).json({ message: 'Booking not found' }); + } + res.status(200).json({ message: 'Booking canceled successfully' }); + }); }; \ No newline at end of file diff --git a/models/tourModel.js b/models/tourModel.js index c241d8f..9dfa46d 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -147,28 +147,36 @@ class User { 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; + 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 (?, ?)'; + const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)'; + try { await pool.execute(query, [userId, tourId]); + } catch (error) { + throw new Error('เกิดข้อผิดพลาดในการจองทัวร์'); + } } // ยกเลิกการจอง static async cancelBooking(userId, bookingId) { - const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?'; + const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?'; + try { const [result] = await pool.execute(query, [bookingId, userId]); return result.affectedRows; + } catch (error) { + throw new Error('เกิดข้อผิดพลาดในการยกเลิกการจอง'); + } } } diff --git a/views/myBookings.ejs b/views/myBookings.ejs index 0d32fad..0f82223 100644 --- a/views/myBookings.ejs +++ b/views/myBookings.ejs @@ -1,23 +1,31 @@ <!DOCTYPE html> -<html lang="th"> +<html lang="en"> <head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>My Bookings</title> + <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="/">กลับไปหน้าทัวร์</a> + <h1>Your Bookings</h1> + <table> + <thead> + <tr> + <th>Booking ID</th> + <th>Tour Title</th> + <th>Booking Date</th> + <th>Status</th> + </tr> + </thead> + <tbody> + <% bookings.forEach(booking => { %> + <tr> + <td><%= booking.booking_id %></td> + <td><%= booking.tour_title %></td> + <td><%= booking.booking_date %></td> + <td><%= booking.status %></td> + </tr> + <% }) %> + </tbody> + </table> </body> </html> -- GitLab