Gitlab@Informatics

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

Project Round 4

parent ce8c5d19
No related branches found
No related tags found
No related merge requests found
...@@ -155,3 +155,13 @@ exports.postCreateTour = async (req, res) => { ...@@ -155,3 +155,13 @@ exports.postCreateTour = async (req, res) => {
} }
}; };
//ค้นหาทัวร์
exports.searchTours = async (req, res) => {
try {
const searchQuery = req.query.query;
const tours = await Tour.searchTours(searchQuery);
res.render('search', { tours, session: req.session });
} catch (error) {
res.status(500).send("Error searching tours");
}
};
\ No newline at end of file
...@@ -14,7 +14,17 @@ class Tour { ...@@ -14,7 +14,17 @@ class Tour {
} }
} }
// ค้นหาทัวร์โดยใช้ชื่อ
static async searchTours(query) {
try {
console.log('Searching for tours with query:', query);
const [rows] = await pool.query('SELECT * FROM tours WHERE name LIKE ?', [`%${query}%`]);
return rows;
} catch (error) {
console.error('Error searching tours:', error.message);
throw new Error('เกิดข้อผิดพลาดในการค้นหาทัวร์');
}
}
// สร้างทัวร์ // สร้างทัวร์
static async createTour(name, description, price, duration) { static async createTour(name, description, price, duration) {
// ตรวจสอบข้อมูล // ตรวจสอบข้อมูล
......
...@@ -24,4 +24,7 @@ router.post('/edit/:id', tourController.postEditTour); // บันทึกท ...@@ -24,4 +24,7 @@ router.post('/edit/:id', tourController.postEditTour); // บันทึกท
//ลบทัวร์ //ลบทัวร์
router.get('/delete/:id', tourController.deleteTour); //ลบทัวร์ router.get('/delete/:id', tourController.deleteTour); //ลบทัวร์
//ค้นหา
router.get('/search', tourController.searchTours); //หน้าค้นหาทัวร์
module.exports = router; module.exports = router;
...@@ -24,12 +24,17 @@ ...@@ -24,12 +24,17 @@
</header> </header>
<section> <section>
<form action="/search" method="GET">
<input type="text" name="query" placeholder="Search for tours..." required>
<button type="submit">Search</button>
</form>
<br>
<!-- แสดงปุ่ม "Create Tour" สำหรับผู้ใช้ที่เข้าสู่ระบบแล้ว --> <!-- แสดงปุ่ม "Create Tour" สำหรับผู้ใช้ที่เข้าสู่ระบบแล้ว -->
<% if (session.userId) { %> <% if (session.userId) { %>
<a href="/create" class="btn-create">Create Tour</a> <a href="/create" class="btn-create">Create Tour</a>
<% } %> <% } %>
<br>
<ul> <ul>
<% tours.forEach(tour => { %> <% tours.forEach(tour => { %>
<li> <li>
......
<h2>Search Results</h2>
<ul>
<% if (tours.length > 0) { %>
<% tours.forEach(tour => { %>
<li>
<a href="/tour/<%= tour.id %>"><%= tour.name %></a> - <%= tour.price %> THB
</li>
<% }) %>
<% } else { %>
<p>No tours found</p>
<% } %>
</ul>
<a href="/">Back to Home</a>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment