From cb650d2fdacae91da68252c912c64f339d0f63eb Mon Sep 17 00:00:00 2001
From: Atiwit Pattanapukdee <65160394@go.buu.ac.th>
Date: Tue, 18 Mar 2025 22:10:57 +0700
Subject: [PATCH] Project Round 4

---
 controllers/tourController.js | 12 +++++++++++-
 models/tourModel.js           | 14 ++++++++++++--
 routes/tourRoutes.js          |  3 +++
 views/index.ejs               |  9 +++++++--
 views/search.ejs              | 15 +++++++++++++++
 5 files changed, 48 insertions(+), 5 deletions(-)
 create mode 100644 views/search.ejs

diff --git a/controllers/tourController.js b/controllers/tourController.js
index 9149bd5..027d0c6 100644
--- a/controllers/tourController.js
+++ b/controllers/tourController.js
@@ -154,4 +154,14 @@ exports.postCreateTour = async (req, res) => {
       res.status(500).send('เกิดข้อผิดพลาดในการลบทัวร์');
     }
 };
-    
+
+//ค้นหาทัวร์
+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
diff --git a/models/tourModel.js b/models/tourModel.js
index df8b710..562888b 100644
--- a/models/tourModel.js
+++ b/models/tourModel.js
@@ -13,8 +13,18 @@ class Tour {
         throw new Error('เกิดข้อผิดพลาดในการดึงข้อมูลทัวร์: ' + error.message);
       }
     }
-  
-  
+    
+    // ค้นหาทัวร์โดยใช้ชื่อ
+    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) {
       // ตรวจสอบข้อมูล
diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js
index cf0569f..35f4425 100644
--- a/routes/tourRoutes.js
+++ b/routes/tourRoutes.js
@@ -24,4 +24,7 @@ router.post('/edit/:id', tourController.postEditTour); // บันทึกท
 //ลบทัวร์
 router.get('/delete/:id', tourController.deleteTour); //ลบทัวร์
 
+//ค้นหา
+router.get('/search', tourController.searchTours); //หน้าค้นหาทัวร์
+
 module.exports = router;
diff --git a/views/index.ejs b/views/index.ejs
index 283cee4..38cdbc5 100644
--- a/views/index.ejs
+++ b/views/index.ejs
@@ -24,12 +24,17 @@
   </header>
   
   <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" สำหรับผู้ใช้ที่เข้าสู่ระบบแล้ว -->
     <% if (session.userId) { %>
       <a href="/create" class="btn-create">Create Tour</a>
     <% } %>
-
+    <br>
     <ul>
       <% tours.forEach(tour => { %>
         <li>
diff --git a/views/search.ejs b/views/search.ejs
new file mode 100644
index 0000000..31ebd89
--- /dev/null
+++ b/views/search.ejs
@@ -0,0 +1,15 @@
+<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>
-- 
GitLab