diff --git a/index.js b/index.js index bc59bc90e7ae27476b594537676a64a728834a9a..3537a08d1370f3e1f735dc8a656b2d6d0d1613fc 100644 --- a/index.js +++ b/index.js @@ -378,48 +378,33 @@ app.post("/confirm-loan", (req, res) => { // Route สำหรับยืนยันการยืม app.post('/submit-loan', (req, res) => { - if (!req.user || !req.user.id) { - return res.status(401).send('กรุณาล็อกอินก่อนการยืมอุปกรณ์'); - } - const user_id = req.user.id; // สมมติว่าผู้ใช้ล็อกอินแล้ว const equipment_id = req.body.equipment_id; const quantity = req.body.quantity; // ตรวจสอบว่าอุปกรณ์มีจำนวนเพียงพอหรือไม่ db.query('SELECT * FROM equipment WHERE id = ?', [equipment_id], (err, result) => { - if (err) { - return res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูล'); - } - + if (err) throw err; const equipment = result[0]; - if (!equipment) { - return res.status(404).send('ไม่พบอุปกรณ์นี้ในระบบ'); - } if (equipment.quantity >= quantity) { // บันทึกข้อมูลการยืมลงในตาราง loans db.query('INSERT INTO loans (user_id, equipment_id, quantity, status) VALUES (?, ?, ?, "pending")', [user_id, equipment_id, quantity], (err) => { - if (err) { - return res.status(500).send('เกิดข้อผิดพลาดในการบันทึกการยืม'); - } - + if (err) throw err; // ปรับจำนวนอุปกรณ์ในตาราง equipment db.query('UPDATE equipment SET quantity = quantity - ? WHERE id = ?', [quantity, equipment_id], (err) => { - if (err) { - return res.status(500).send('เกิดข้อผิดพลาดในการอัปเดตข้อมูลอุปกรณ์'); - } - + if (err) throw err; res.redirect('/loans'); // เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์" }); }); } else { - res.status(400).send('จำนวนอุปกรณ์ไม่เพียงพอ'); + res.send('จำนวนอุปกรณ์ไม่เพียงพอ'); } }); }); + // การยกเลิกการยืม (Backend) app.post("/cancel-loan", (req, res) => { const loanId = req.body.loan_id; diff --git a/views/confirm-loan.ejs b/views/confirm-loan.ejs index 25fcb9c3788879e9c9dfd033cef1ccf6e43f0065..29357c51ab30f73ca17215409bf843fe7511a1ff 100644 --- a/views/confirm-loan.ejs +++ b/views/confirm-loan.ejs @@ -19,10 +19,10 @@ <a href="/" class="text-white hover:text-gray-200">หน้าหลัก</a> <a href="/loans" class="text-white hover:text-gray-200">รวมการยืมอุปกรณ์</a> <a href="/logout" class="text-white hover:text-gray-200">ออกจากระบบ</a> - <% } else { %> - <a href="/login" class="text-white hover:text-gray-200">เข้าสู่ระบบ</a> - <a href="/register" class="text-white hover:text-gray-200">สมัครสมาชิก</a> - <% } %> + <% } else { %> + <a href="/login" class="text-white hover:text-gray-200">เข้าสู่ระบบ</a> + <a href="/register" class="text-white hover:text-gray-200">สมัครสมาชิก</a> + <% } %> </div> </div> </nav> @@ -43,54 +43,57 @@ <div class="flex justify-center space-x-4"> <!-- ฟอร์มยืนยันการยืม --> - <button id="confirmButton" class="bg-blue-600 text-white px-6 py-3 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400"> + <button id="confirmButton" + class="bg-blue-600 text-white px-6 py-3 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400"> ยืนยันการยืม </button> <!-- ฟอร์มยกเลิกการยืม --> <form action="/cancel-loan" method="GET" class="flex space-x-2"> - <button type="submit" class="bg-gray-300 text-gray-800 px-6 py-3 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-400">ยกเลิก</button> + <button type="submit" + class="bg-gray-300 text-gray-800 px-6 py-3 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-400">ยกเลิก</button> </form> </div> </div> - <% } else { %> - <!-- ถ้าไม่ใช่ผู้ใช้ที่ล็อกอิน --> - <div class="container mx-auto p-6"> - <p class="text-xl text-center text-gray-500">คุณต้องเข้าสู่ระบบก่อนทำการยืมอุปกรณ์</p> - </div> - <% } %> + <% } else { %> + <!-- ถ้าไม่ใช่ผู้ใช้ที่ล็อกอิน --> + <div class="container mx-auto p-6"> + <p class="text-xl text-center text-gray-500">คุณต้องเข้าสู่ระบบก่อนทำการยืมอุปกรณ์</p> + </div> + <% } %> + + <script> + // เมื่อกดปุ่ม "ยืนยันการยืม" ให้เปลี่ยนเส้นทางไปยังหน้า /loans + document.getElementById('confirmButton').addEventListener('click', function () { + // ส่งข้อมูลยืมไปยังเซิร์ฟเวอร์ (ในกรณีที่ต้องการให้มีการบันทึกข้อมูลก่อน) + const equipment_id = "<%= equipment.id %>"; + const quantity = "<%= quantity %>"; - <script> - // เมื่อกดปุ่ม "ยืนยันการยืม" ให้เปลี่ยนเส้นทางไปยังหน้า /loans - document.getElementById('confirmButton').addEventListener('click', function() { - // ส่งข้อมูลยืมไปยังเซิร์ฟเวอร์ (ในกรณีที่ต้องการให้มีการบันทึกข้อมูลก่อน) - const equipment_id = "<%= equipment.id %>"; - const quantity = "<%= quantity %>"; + fetch('/submit-loan', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', // ตั้งค่า header ให้เป็น application/json + }, + body: JSON.stringify({ + equipment_id: equipment_id, + quantity: quantity + }) + }) + .then(response => { + if (response.ok) { + // หลังจากยืนยันเสร็จแล้วเปลี่ยนเส้นทางไปที่ /loans + window.location.href = '/loans'; + } else { + alert('การยืมอุปกรณ์ล้มเหลว'); + } + }) + .catch(error => { + alert('เกิดข้อผิดพลาด: ' + error); + }); - fetch('/submit-loan', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - equipment_id: equipment_id, - quantity: quantity - }) - }) - .then(response => { - if (response.ok) { - // หลังจากยืนยันเสร็จแล้วเปลี่ยนเส้นทางไปที่ /loans - window.location.href = '/loans'; - } else { - alert('การยืมอุปกรณ์ล้มเหลว'); - } - }) - .catch(error => { - alert('เกิดข้อผิดพลาด: ' + error); - }); - }); - </script> + }); + </script> </body> -</html> +</html> \ No newline at end of file