diff --git a/controllers/tourController.js b/controllers/tourController.js index 9149bd582057624ec1d7987d998083878801bcd1..027d0c6a44dc867e7f9ebc913d7f82809ce7978c 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 df8b710a30dfd17f92e8b64965b73b0ed4304eea..562888be07f36b41b4fb188b4cbda43eb7c3718e 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 cf0569f22754c51f8f3935ffadc2cc816baf6f7f..35f4425b3872c9539259e58a9de1b68249351069 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 283cee439927702bcbaf40aa43935b899a6c7daf..38cdbc51626c2c9b3eb80207d3cfcca4d7c2eff9 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 0000000000000000000000000000000000000000..31ebd89e01263dd120ecd2bda04fd52143c50ef8 --- /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>