From 9db1e9883a0b0539e1d54a51f1db151295a7a681 Mon Sep 17 00:00:00 2001 From: Atiwit Pattanapukdee <65160394@go.buu.ac.th> Date: Wed, 26 Mar 2025 17:13:49 +0700 Subject: [PATCH] Project Round 11 --- controllers/tourController.js | 20 ++++++++++++++++---- models/tourModel.js | 10 +++++++++- views/createtour.ejs | 10 +++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/controllers/tourController.js b/controllers/tourController.js index 55a53cd..460324d 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 3afa161..91d72f6 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 0c024d2..96a6b93 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> -- GitLab