diff --git a/controllers/tourController.js b/controllers/tourController.js index 55a53cdd22781167d3d59faa33667c8051816913..460324d6379c0ca26ddbf0c047ed0e8c094fb4c5 100644 --- a/controllers/tourController.js +++ b/controllers/tourController.js @@ -1,4 +1,4 @@ -const { User, Tour } = require('../models/tourModel'); +const { User, Tour, company } = require('../models/tourModel'); const pool = require('../config/database'); exports.getTours = async (req, res) => { @@ -147,8 +147,18 @@ exports.updateProfile = async (req, res) => { //CRUD // แสดงฟอร์มสร้างทัวร์ -exports.getCreateTour = (req, res) => { - res.render('createtour'); +// ฟังก์ชันแสดงฟอร์มสร้างทัวร์ +exports.getCreateTour = async (req, res) => { + try { + // ดึงข้อมูลบริษัททั้งหมดจากฐานข้อมูล + const companies = await company.getAllCompanies(); + + // ส่งข้อมูลบริษัทไปยัง view 'createtour.ejs' + res.render('createtour', { companies }); + } catch (error) { + console.error('Error fetching companies:', error); + res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูลบริษัท'); + } }; // บันทึกทัวร์ใหม่ @@ -198,6 +208,8 @@ exports.postCreateTour = async (req, res) => { res.render('createtour', { message: 'เกิดข้อผิดพลาดในการสร้างทัวร์' }); } }; + +//edit exports.getEditTour = async (req, res) => { try { const tour = await Tour.getTourById(req.params.id); @@ -247,7 +259,7 @@ exports.postEditTour = async (req, res) => { } }; - +//delete exports.deleteTour = async (req, res) => { try { const tourId = req.params.id; diff --git a/models/tourModel.js b/models/tourModel.js index 3afa161f2c005e19039d743f3f342d552e1539a4..91d72f621aca7b3d72b2cbeeea05c957d1edf85d 100644 --- a/models/tourModel.js +++ b/models/tourModel.js @@ -137,4 +137,12 @@ class User { } } -module.exports = { User, Tour }; +class company { + static async getAllCompany() { + const query = 'SELECT * FROM company'; + const [rows] = await pool.execute(query); + return rows; + } +} + +module.exports = { User, Tour ,company}; diff --git a/views/createtour.ejs b/views/createtour.ejs index 0c024d2256fd5efa729fe7600fdb6b6a00dd71c0..96a6b93fc2384cfd52461bd20851a58012539bb5 100644 --- a/views/createtour.ejs +++ b/views/createtour.ejs @@ -30,12 +30,12 @@ <div> <label for="tourCompany">บริษัท:</label> <select id="tourCompany" name="company_id" required> - <option value="" re>เลือกบริษัท</option> - <option value="1">Amazing Travel</option> - <option value="2">Wonderful Tours</option> - <option value="3">Explore Asia</option> + <option value="">เลือกบริษัท</option> + <% companies.forEach(company => { %> + <option value="<%= company.id %>"><%= company.name %></option> + <% }) %> </select> - </div> + </div> <button type="submit">สร้างโปรแกรม Tour</button> </form> </main>