From c486147dfd575d959bd438758312476e839dcb7d Mon Sep 17 00:00:00 2001
From: 65160024 <65160024@go.buu.ac.th>
Date: Tue, 25 Mar 2025 04:05:15 +0000
Subject: [PATCH] update submit loan

---
 index.js | 59 ++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 21 deletions(-)

diff --git a/index.js b/index.js
index 59f44a6..bc59bc9 100644
--- a/index.js
+++ b/index.js
@@ -378,31 +378,48 @@ app.post("/confirm-loan", (req, res) => {
 
 // Route สำหรับยืนยันการยืม
 app.post('/submit-loan', (req, res) => {
-    const user_id = req.user.id; // สมมติว่าผู้ใช้ล็อกอินแล้ว
-    const equipment_id = req.body.equipment_id;
-    const quantity = req.body.quantity;
+  if (!req.user || !req.user.id) {
+      return res.status(401).send('กรุณาล็อกอินก่อนการยืมอุปกรณ์');
+  }
 
-    // ตรวจสอบว่าอุปกรณ์มีจำนวนเพียงพอหรือไม่
-    db.query('SELECT * FROM equipment WHERE id = ?', [equipment_id], (err, result) => {
-        if (err) throw err;
-        const equipment = result[0];
+  const user_id = req.user.id; // สมมติว่าผู้ใช้ล็อกอินแล้ว
+  const equipment_id = req.body.equipment_id;
+  const quantity = req.body.quantity;
 
-        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) throw err;
-                // ปรับจำนวนอุปกรณ์ในตาราง equipment
-                db.query('UPDATE equipment SET quantity = quantity - ? WHERE id = ?', [quantity, equipment_id], (err) => {
-                    if (err) throw err;
-                    res.redirect('/loans'); // เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์"
-                });
-            });
-        } else {
-            res.send('จำนวนอุปกรณ์ไม่เพียงพอ');
-        }
-    });
+  // ตรวจสอบว่าอุปกรณ์มีจำนวนเพียงพอหรือไม่
+  db.query('SELECT * FROM equipment WHERE id = ?', [equipment_id], (err, result) => {
+      if (err) {
+          return res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูล');
+      }
+
+      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('เกิดข้อผิดพลาดในการบันทึกการยืม');
+              }
+
+              // ปรับจำนวนอุปกรณ์ในตาราง equipment
+              db.query('UPDATE equipment SET quantity = quantity - ? WHERE id = ?', [quantity, equipment_id], (err) => {
+                  if (err) {
+                      return res.status(500).send('เกิดข้อผิดพลาดในการอัปเดตข้อมูลอุปกรณ์');
+                  }
+
+                  res.redirect('/loans'); // เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์"
+              });
+          });
+      } else {
+          res.status(400).send('จำนวนอุปกรณ์ไม่เพียงพอ');
+      }
+  });
 });
 
+
 // การยกเลิกการยืม (Backend)
 app.post("/cancel-loan", (req, res) => {
   const loanId = req.body.loan_id;
-- 
GitLab