diff --git a/controllers/tourController.js b/controllers/tourController.js index e96ca983b6c4a692b4e8ab16021bf9c03339065f..43cc262bc2c12821b24f33d89839fb6971633acc 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -1,68 +1,27 @@ const { User, Tour } = require('../models/tourModel'); -// ดึงข้อมูลทัวร์ทั้งหมด exports.getTours = async (req, res) => { try { - const tours = await Tour.getAll(); - res.render('index', { - tours: tours, - userId: req.session.userId, - userName: req.session.userName - }); - } catch (err) { - res.status(500).json({ error: err.message }); + const tours = await Tour.getAllTours(); + res.render('index', { + tours: tours, + session: req.session // ส่ง session ไปยัง ejs + }); + } catch (error) { + res.status(500).send('เกิดข้อผิดพลาด'); } }; -exports.getAllTours = async (req, res) => { - try { - const results = await Tour.getAll(); - res.json(results); - } catch (err) { - res.status(500).json({ error: err.message }); - } -}; - -exports.getTourById = async (req, res) => { - try { - const result = await Tour.getById(req.params.id); - if (!result) return res.status(404).json({ message: "Tour not found" }); - res.json(result); - } catch (err) { - res.status(500).json({ error: err.message }); - } -}; - -exports.createTour = async (req, res) => { - try { - const insertId = await Tour.create(req.body); - res.json({ message: 'Tour added', id: insertId }); - } catch (err) { - res.status(500).json({ error: err.message }); - } -}; - -exports.updateTour = async (req, res) => { - try { - const success = await Tour.update(req.params.id, req.body); - if (!success) return res.status(404).json({ message: "Tour not found" }); - res.json({ message: 'Tour updated' }); - } catch (err) { - res.status(500).json({ error: err.message }); - } -}; - -exports.deleteTour = async (req, res) => { - try { - const success = await Tour.delete(req.params.id); - if (!success) return res.status(404).json({ message: "Tour not found" }); - res.json({ message: 'Tour deleted' }); - } catch (err) { - res.status(500).json({ error: err.message }); - } +exports.getTourDetails = async (req, res) => { + try { + const tour = await Tour.getTourById(req.params.id); + if (!tour) return res.status(404).send('ไม่พบข้อมูล'); + res.render('tour-details', { tour }); + } catch (error) { + res.status(500).send('เกิดข้อผิดพลาด'); + } }; - //User// exports.getLogin = (req, res) => { res.render('login', { message: null }); @@ -125,3 +84,49 @@ exports.Logout = (req, res) => { }); }; +//CRUD +exports.getTour = (req, res) => { + res.render('createtour', { message: null }); + }; +// ฟังก์ชันสำหรับการสร้างทัวร์ +exports.postCreateTour = async (req, res) => { + const { name, description, price, duration } = req.body; + const tourData = { name, description, price, duration }; + + try { + await Tour.createTour(tourData); + res.redirect('/'); // หลังจากสร้างทัวร์เสร็จ ให้กลับไปหน้า home + } catch (error) { + res.status(500).send('เกิดข้อผิดพลาดในการสร้างทัวร์'); + } + }; + exports.getEditTour = async (req, res) => { + try { + const tour = await Tour.getTourById(req.params.id); + if (!tour) return res.status(404).send('ไม่พบข้อมูลทัวร์'); + res.render('edit-tour', { tour }); + } catch (error) { + res.status(500).send('เกิดข้อผิดพลาด'); + } + }; + + exports.postEditTour = async (req, res) => { + const { name, description, price, duration } = req.body; + const tourData = { name, description, price, duration }; + + try { + await Tour.updateTour(req.params.id, tourData); // คำสั่งในการอัปเดต + res.redirect('/tour/' + req.params.id); // ไปที่หน้ารายละเอียดทัวร์ + } catch (error) { + res.status(500).send('เกิดข้อผิดพลาดในการแก้ไขทัวร์'); + } + }; + exports.deleteTour = async (req, res) => { + try { + await Tour.deleteTour(req.params.id); // คำสั่งในการลบทัวร์ + res.redirect('/'); // ไปที่หน้า home หลังจากลบทัวร์ + } catch (error) { + res.status(500).send('เกิดข้อผิดพลาดในการลบทัวร์'); + } + }; + diff --git a/models/tourModel.js b/models/tourModel.js index 67e370e74527c9e6bf38fa4ce4aaac85a98e26f8..ccae065cf3599a2d47323bc0b712306114fdde53 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -1,60 +1,39 @@ const pool = require('../config/database'); const bcrypt = require('bcryptjs'); class Tour { - static async getAll() { - try { - const [results] = await pool.query('SELECT * FROM tours'); - return results; - } catch (err) { - throw err; // Throw error to be caught by the caller - } + // ดึงทัวร์ทั้งหมด + static async getAllTours() { + const [rows] = await pool.query('SELECT * FROM tours'); + return rows; } - - static async getById(id) { - try { - const [result] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); - return result; - } catch (err) { - throw err; - } + + // ดึงทัวร์ตาม ID + static async getTourById(id) { + const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]); + return rows[0]; } - - static async create(data) { - try { - const [result] = await pool.query('INSERT INTO tours SET ?', data); - return result.insertId; // Return the ID of the newly inserted tour - } catch (err) { - throw err; - } - } - - static async update(id, data) { - try { - const [result] = await pool.query('UPDATE tours SET ? WHERE id = ?', [data, id]); - return result.affectedRows > 0; // Return true if update was successful - } catch (err) { - throw err; - } + + // สร้างทัวร์ + static async createTour(tourData) { + const { name, description, price, duration } = tourData; + const query = 'INSERT INTO tours (name, description, price, duration) VALUES (?, ?, ?, ?)'; + await pool.execute(query, [name, description, price, duration]); } - - static async delete(id) { - try { - const [result] = await pool.query('DELETE FROM tours WHERE id = ?', [id]); - return result.affectedRows > 0; // Return true if delete was successful - } catch (err) { - throw err; - } + + // อัปเดตทัวร์ + static async updateTour(id, tourData) { + const { name, description, price, duration } = tourData; + const query = 'UPDATE tours SET name = ?, description = ?, price = ?, duration = ? WHERE id = ?'; + await pool.execute(query, [name, description, price, duration, id]); } - - static async search(query) { - try { - const [results] = await pool.query('SELECT * FROM tours WHERE name LIKE ?', [`%${query}%`]); - return results; - } catch (err) { - throw err; - } + + // ลบทัวร์ + static async deleteTour(id) { + const query = 'DELETE FROM tours WHERE id = ?'; + await pool.execute(query, [id]); } -} + } + class User { static async findOne(email) { diff --git a/routes/tourRoutes.js b/routes/tourRoutes.js index 9606b8608592582711c7e2c745b36a53e897db1d..59a0c8ba99b0f698b4c1453e5d04c323832331d5 100644 --- a/routes/tourRoutes.js +++ b/routes/tourRoutes.js @@ -2,38 +2,22 @@ const express = require('express'); const router = express.Router(); const tourController = require('../controllers/tourController'); -// หน้าแสดงข้อมูล -router.get('/', tourController.getTours); -router.get('/tour/:id', tourController.getTourDetails); - -//login logout -router.get('/login', tourController.getLogin); -router.get('/logout', tourController.Logout); -router.get('/register', tourController.getRegister); -router.post('/login', tourController.postLogin); -router.post('/register', tourController.postRegister); - - -// ✅ ดึงข้อมูลทัวร์ทั้งหมด -router.get('/tours', tourController.getAllTours); - -// ✅ ดึงข้อมูลทัวร์ตาม ID -router.get('/tours/:id', tourController.getTourById); - -// ✅ ค้นหาทัวร์ (เพิ่มใหม่) -router.get('/tours/search', tourController.searchTours); - -// ✅ สร้างทัวร์ -router.post('/tours', tourController.createTour); - -// ✅ แก้ไขทัวร์ -router.put('/tours/:id', tourController.updateTour); - -// ✅ ลบทัวร์ -router.delete('/tours/:id', tourController.deleteTour); - -module.exports = router; - - +// ทัวร์ +router.get('/', tourController.getTours); // หน้าหลักที่แสดงทัวร์ทั้งหมด +router.get('/tour/:id', tourController.getTourDetails); // หน้ารายละเอียดของทัวร์ + +// User +router.get('/login', tourController.getLogin); // หน้าเข้าสู่ระบบ +router.get('/register', tourController.getRegister); // หน้าลงทะเบียน +router.post('/register', tourController.postRegister); // ฟอร์มลงทะเบียน +router.post('/login', tourController.postLogin); // ฟอร์มเข้าสู่ระบบ +router.get('/logout', tourController.Logout); // ออกจากระบบ + +// สร้างทัวร์ +router.get('/tour/create', tourController.getTour); // หน้าสร้างทัวร์ +// เพิ่ม route สำหรับการสร้างทัวร์โดยการ POST +router.post('/tour/create', async (req, res) => { + // logic สำหรับสร้างทัวร์ใหม่ +}); module.exports = router; diff --git a/server.js b/server.js index 067bb970ac8a0cf69499712b6d21091c086a8975..2ea0bea11c94b7e8c2ddd7747ed9608717abccb8 100644 --- a/server.js +++ b/server.js @@ -14,8 +14,6 @@ app.use(session({ saveUninitialized: true })); - - app.set('view engine', 'ejs'); app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: false })); diff --git a/views/createtour.ejs b/views/createtour.ejs new file mode 100644 index 0000000000000000000000000000000000000000..95ebcfed3fc98b468f1cef2ed1321c80b6f2fc98 --- /dev/null +++ b/views/createtour.ejs @@ -0,0 +1,40 @@ +<!DOCTYPE html> +<html lang="th"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Create Tour</title> + <link rel="stylesheet" href="/styles.css"> +</head> +<body> + <header> + <h1>Create New Tour</h1> + </header> + + <main> + <form action="/tour/create" method="POST"> + <div> + <label for="tourName">Tour Name:</label> + <input type="text" id="tourName" name="tourName" required> + </div> + <div> + <label for="tourDescription">Description:</label> + <textarea id="tourDescription" name="tourDescription" required></textarea> + </div> + <div> + <label for="tourPrice">Price:</label> + <input type="number" id="tourPrice" name="tourPrice" required> + </div> + <div> + <label for="tourDate">Date:</label> + <input type="date" id="tourDate" name="tourDate" required> + </div> + <button type="submit">Create Tour</button> + </form> + </main> + + <footer> + <p>© 2025 Your Tour Website</p> + </footer> +</body> +</html> diff --git a/views/edit-tour.ejs b/views/edit-tour.ejs new file mode 100644 index 0000000000000000000000000000000000000000..951807d448c95bf0bc43fc9f0c184eaf2f9d2e46 --- /dev/null +++ b/views/edit-tour.ejs @@ -0,0 +1,30 @@ +<!DOCTYPE html> +<html lang="th"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Edit Tour</title> +</head> +<body> + <h1>Edit Tour</h1> + <form action="/tour/edit/<%= tour.id %>" method="POST"> + <div> + <label for="tourName">Tour Name:</label> + <input type="text" id="tourName" name="name" value="<%= tour.name %>" required> + </div> + <div> + <label for="tourDescription">Description:</label> + <textarea id="tourDescription" name="description" required><%= tour.description %></textarea> + </div> + <div> + <label for="tourPrice">Price:</label> + <input type="number" id="tourPrice" name="price" value="<%= tour.price %>" required> + </div> + <div> + <label for="tourDuration">Duration (in days):</label> + <input type="number" id="tourDuration" name="duration" value="<%= tour.duration %>" required> + </div> + <button type="submit">Update Tour</button> + </form> +</body> +</html> diff --git a/views/index.ejs b/views/index.ejs index 204a65706d97f889caaa95d6e0ac0db9c0e4cd5d..2962118c65f4010fee28707eaaca04775a310c83 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -5,61 +5,37 @@ <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tour Website</title> <link rel="stylesheet" href="/css/styles.css"> - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"> </head> <body> - <header class="bg-primary text-white p-3"> - <div class="container d-flex justify-content-between align-items-center"> - <h1>Welcome to Our Tour Website</h1> - <nav> - <a href="/" class="btn btn-light">Home</a> - <a href="/about" class="btn btn-light">About</a> - <a href="/contact" class="btn btn-light">Contact</a> - - <% if (userId) { %> - <a href="/profile" class="btn btn-warning"><%= userName %></a> - <a href="/logout" class="btn btn-danger">Logout</a> - <% } else { %> - <a href="/login" class="btn btn-success">Login</a> - <% } %> - </nav> - </div> + <header> + <h1>Welcome to Our Tour Website</h1> + <nav> + <a href="/">Home</a> + <a href="/about">About</a> + <a href="/contact">Contact</a> + + <% if (session.userId) { %> <!-- ตรวจสอบว่า userId อยู่ใน session หรือไม่ --> + <a href="/profile"><%= session.userName %></a> <!-- แสดงชื่อผู้ใช้ --> + <a href="/logout">Logout</a> + <% } else { %> + <a href="/login">Login</a> <!-- ถ้ายังไม่ได้ login --> + <% } %> + </nav> </header> - <section class="container mt-4"> - <h2 class="text-center">Available Tours</h2> - - <% if (tours.length === 0) { %> - <p class="text-center text-danger">No tours available at the moment.</p> - <% } else { %> - <div class="row"> - <% tours.forEach(tour => { %> - <div class="col-md-4 mb-4"> - <div class="card"> - <% if (tour.image) { %> - <img src="<%= tour.image %>" class="card-img-top" alt="<%= tour.name %>"> - <% } else { %> - <img src="/images/default-tour.jpg" class="card-img-top" alt="Tour Image"> - <% } %> - <div class="card-body"> - <h5 class="card-title"><%= tour.name %></h5> - <p class="card-text">Price: <strong><%= tour.price %> THB</strong></p> - <a href="/tour/<%= tour.id %>" class="btn btn-primary">View Details</a> - <% if (userId) { %> - <a href="/book/<%= tour.id %>" class="btn btn-success">Book Now</a> - <% } %> - </div> - </div> - </div> - <% }) %> - </div> - <% } %> + <section> + <h2>Available Tours</h2> + <ul> + <% tours.forEach(tour => { %> + <li> + <a href="/tour/<%= tour.id %>"><%= tour.name %></a> - <%= tour.price %> THB + </li> + <% }) %> + </ul> </section> - <footer class="bg-dark text-white text-center p-3 mt-4"> + <footer> <p>© 2025 Tour Website</p> </footer> - - <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>