Gitlab@Informatics

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

Project Round 7

parent 1422bbe0
Branches
No related tags found
No related merge requests found
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
......@@ -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 };
......@@ -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;
......@@ -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, () => {
......
......@@ -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> <!-- ปุ่มสำหรับลบทัวร์ -->
<% } %>
......
<!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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment