Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit a3eff206 authored by 65160394's avatar 65160394
Browse files

Project Round 9

parent 0552a71b
No related branches found
No related tags found
No related merge requests found
......@@ -284,6 +284,7 @@ exports.getAllTours = async (req, res) => {
};
// ดึงข้อมูลการจองพร้อมชื่อผู้จอง
// ดึงรายการจองของผู้ใช้
exports.getUserBookings = async (req, res) => {
const userId = req.session.userId; // สมมติว่าใช้ session สำหรับจัดการผู้ใช้
......@@ -311,7 +312,6 @@ exports.getUserBookings = async (req, res) => {
}
};
// ฟังก์ชันสร้างการจอง
exports.createBooking = async (req, res) => {
const userId = req.session.userId; // สมมติว่าใช้ session ในการจัดการการล็อกอิน
......@@ -343,27 +343,23 @@ exports.createBooking = async (req, res) => {
}
};
// แก้ไขการจอง (แสดงฟอร์ม)
exports.getEditBooking = async (req, res) => {
const bookingId = req.params.id;
// ฟังก์ชันยกเลิกการจอง
exports.cancelBooking = async (req, res) => {
const bookingId = req.params.id; // รับ bookingId จาก URL params
try {
const booking = await Booking.findById(bookingId);
if (!booking) {
// ตรวจสอบว่ามีการจองนี้ในฐานข้อมูลหรือไม่
const cancelQuery = 'DELETE FROM bookings WHERE id = ?';
const [result] = await pool.execute(cancelQuery, [bookingId]);
// ถ้าลบไม่สำเร็จ ให้แจ้งว่าไม่พบการจอง
if (result.affectedRows === 0) {
return res.status(404).send('Booking not found');
}
res.render('edit-booking', { booking });
} catch (err) {
res.status(500).send('Error fetching booking details');
}
};
// ลบการจอง
exports.deleteBooking = async (req, res) => {
const bookingId = req.params.id;
try {
await Booking.deleteOne({ _id: bookingId });
res.redirect('/bookings');
} catch (err) {
res.status(500).send('Error deleting booking');
res.status(200).send('Booking canceled successfully');
} catch (error) {
console.error('Error canceling booking:', error.message);
res.status(500).send('เกิดข้อผิดพลาดในการยกเลิกการจอง');
}
};
\ No newline at end of file
......@@ -154,30 +154,36 @@ class Booking {
WHERE b.user_id = ?
ORDER BY b.id ASC
`;
const [rows] = await pool.query(query, [userId]);
return rows;
try {
const [rows] = await pool.query(query, [userId]);
return rows; // คืนค่ารายการจองทั้งหมด
} catch (error) {
console.error('Error fetching user bookings:', error);
throw new Error('Unable to retrieve user bookings');
}
}
// ทำการจองทัวร์
static async createBooking(userId, tourId) {
const query = 'INSERT INTO bookings (user_id, tour_id) VALUES (?, ?)';
try {
await pool.execute(query, [userId, tourId]);
const [result] = await pool.execute(query, [userId, tourId]);
return result; // คืนค่าผลลัพธ์จากการจอง
} catch (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 = ?';
// ลบการจอง
static async deleteBooking(bookingId) {
const query = 'DELETE FROM bookings WHERE id = ?';
try {
const [result] = await pool.execute(query, [bookingId, userId]);
return result.affectedRows;
const [result] = await pool.execute(query, [bookingId]);
return result; // คืนค่าผลลัพธ์จากการลบการจอง
} catch (error) {
throw new Error('เกิดข้อผิดพลาดในการยกเลิกการจอง');
console.error('Error deleting booking:', error);
throw new Error('Error deleting booking');
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment