diff --git a/controllers/tourController.js b/controllers/tourController.js index ce74c9a4567dc641d7f865cdc1eb81dd30a8eacc..ab5979e19b1ea2f0188f844ff19d38f673f1f1f7 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -273,17 +273,10 @@ exports.searchTours = async (req, res) => { //จองทัวร์ - exports.getAllTours = async (req, res) => { try { - const tours = await Tour.getAllTours(); // เรียกใช้ฟังก์ชันดึงทัวร์ทั้งหมด - - // ตรวจสอบว่ามีทัวร์หรือไม่ - if (tours.length === 0) { - return res.render('booking', { tours: [], message: 'No tours available at the moment.' }); - } - - res.render('booking', { tours }); // ส่งทัวร์ทั้งหมดไปที่หน้า booking.ejs + const tours = await Tour.getAllTours(); // เรียกใช้ฟังก์ชันดึงทัวร์ทั้งหมด + res.render('booking', { tours }); } catch (error) { console.error('Error fetching tours:', error.message); res.status(500).send('Internal Server Error'); @@ -298,13 +291,24 @@ exports.getUserBookings = (req, res) => { }); }; -// สร้างการจอง -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.createBooking = async (req, res) => { + const userId = req.session.userId; // สมมติว่าใช้ session ในการจัดการการล็อกอิน + const tourId = req.body['tour_id']; // รับ tour_id จากฟอร์ม + + // ตรวจสอบว่ามีข้อมูล tour-id หรือไม่ + if (!tourId) { + return res.status(400).send('Tour ID is required.'); + } + + try { + // ทำการจองทัวร์ + await Booking.createBooking(userId, tourId); + res.redirect('/my-bookings'); + } catch (error) { + console.error('Error creating booking:', error.message); + res.status(500).send('Internal Server Error'); + } }; // ยกเลิกการจอง diff --git a/models/tourModel.js b/models/tourModel.js index b71d05f2020e45054e9bf0f55da1ae86c78472dd..a5f92121d2beed471805335c007aceb3672b4a56 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -164,10 +164,12 @@ class Booking { try { await pool.execute(query, [userId, tourId]); } catch (error) { - throw new Error('เกิดข้อผิดพลาดในการจองทัวร์'); + console.error('Error creating booking:', error); + throw new Error('Error creating booking'); } } + // ยกเลิกการจอง static async cancelBooking(userId, bookingId) { const query = 'DELETE FROM bookings WHERE id = ? AND user_id = ?'; diff --git a/views/booking.ejs b/views/booking.ejs index 6c83aa768629d78335037bae7c322b80c658b332..39b43bb726124a426112fbb48c5230d52ff9ba0a 100644 --- a/views/booking.ejs +++ b/views/booking.ejs @@ -29,8 +29,13 @@ <div class="booking-form"> <h2>Book a Tour</h2> <form action="/create-booking" method="POST"> - <label for="tour-id">Tour ID</label> - <input type="number" id="tour-id" name="tour-id" required> + <label for="tour_id">Select Tour</label> + <select id="tour_id" name="tour-id" required> + <option value="" disabled selected>Select a Tour</option> + <% tours.forEach(function(tour) { %> + <option value="<%= tour.id %>"><%= tour.name %></option> + <% }); %> + </select> <button type="submit">Book Now</button> </form>