Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit d1ee9826 authored by 65160394's avatar 65160394
Browse files

Project Round 8

parent 8d9a7449
No related branches found
No related tags found
No related merge requests found
......@@ -148,6 +148,12 @@ exports.createTour = async (req, res) => {
if (!name || !description || !price || !duration) {
return res.status(400).send('กรุณากรอกข้อมูลให้ครบทุกช่อง');
}
// แปลง userId ให้เป็นตัวเลข
const userId = parseInt(req.session.userId, 10);
if (isNaN(userId)) {
return res.status(400).send('User ID is not valid');
}
try {
// ส่ง userId ที่เก็บไว้ใน session ไปพร้อมกับข้อมูลทัวร์
......
......@@ -40,10 +40,16 @@ class Tour {
throw new Error('ข้อมูลไม่ครบถ้วน กรุณากรอกข้อมูลให้ครบถ้วน');
}
// ตรวจสอบว่า userId เป็นตัวเลขหรือไม่
const validUserId = parseInt(userId, 10);
if (isNaN(validUserId)) {
throw new Error('User ID ไม่ถูกต้อง');
}
const query = 'INSERT INTO tours (name, description, price, duration, userId) VALUES (?, ?, ?, ?, ?)';
try {
// บันทึกทัวร์พร้อมกับ userId ที่สร้างทัวร์
await pool.execute(query, [name, description, price, duration, userId]);
await pool.execute(query, [name, description, price, duration, validUserId]);
} catch (error) {
console.error('Error creating tour:', error);
throw new Error('เกิดข้อผิดพลาดในการสร้างทัวร์');
......@@ -51,7 +57,6 @@ class Tour {
}
// อัปเดตทัวร์
static async updateTour(id, tourData) {
const { name, description, price, duration } = tourData;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment