diff --git a/index.js b/index.js index 2e47f40db6d7acf72d260622193acf4cdfd4b970..cc61c83d491306bdc4698ff7184ef73f5d24ee52 100644 --- a/index.js +++ b/index.js @@ -157,47 +157,35 @@ app.get("/", isAuthenticated, (req, res) => { }); -// ยืมอุปกรณ์ // ยืมอุปกรณ์ app.post("/borrow", isAuthenticated, (req, res) => { const { equipment_id, quantity } = req.body; // ตรวจสอบจำนวนที่ต้องการยืม - db.query( - "SELECT quantity FROM equipment WHERE id = ?", - [equipment_id], - (err, results) => { - if (err) throw err; + db.query("SELECT quantity FROM equipment WHERE id = ?", [equipment_id], (err, results) => { + if (err) throw err; - const availableQuantity = results[0].quantity; - if (availableQuantity >= quantity) { - // ลดจำนวนอุปกรณ์ที่มีในระบบ - db.query( - "UPDATE equipment SET quantity = quantity - ? WHERE id = ?", - [quantity, equipment_id], - (err) => { - if (err) throw err; - - // บันทึกข้อมูลการยืม (โดยใช้สถานะ 'pending' รอการยืนยัน) - db.query( - "INSERT INTO loans (equipment_id, user_id, quantity, status) VALUES (?, ?, ?, 'pending')", - [equipment_id, req.user.id, quantity], - (err) => { - if (err) throw err; - res.redirect("/loans"); // เปลี่ยนเส้นทางไปยังหน้า 'รวมการยืม' - } - ); - } - ); - } else { - res.send("จำนวนอุปกรณ์ไม่เพียงพอ"); - } + const availableQuantity = results[0].quantity; + if (availableQuantity >= quantity) { + // ลดจำนวนอุปกรณ์ที่มีในระบบ + db.query("UPDATE equipment SET quantity = quantity - ? WHERE id = ?", [quantity, equipment_id], (err) => { + if (err) throw err; + + // บันทึกข้อมูลการยืม (โดยใช้สถานะ 'pending' รอการยืนยัน) + db.query("INSERT INTO loans (equipment_id, user_id, quantity, status) VALUES (?, ?, ?, 'pending')", [equipment_id, req.user.id, quantity], (err) => { + if (err) throw err; + res.redirect("/loans"); // เปลี่ยนเส้นทางไปยังหน้า 'รวมการยืม' + }); + }); + } else { + res.send("จำนวนอุปกรณ์ไม่เพียงพอ"); } - ); + }); }); + // คืนอุปกรณ์ app.post("/return", isAuthenticated, (req, res) => { const { equipment_id } = req.body; @@ -294,7 +282,7 @@ app.post("/delete-equipment", isAuthenticated, (req, res) => { app.get("/loans", (req, res) => { if (req.user && req.user.role === "user") { db.query( - `SELECT loans.id, loans.quantity, loans.status, equipment.name AS equipment_name, users.name AS borrower_name + `SELECT loans.id, loans.quantity, loans.status, equipment.name AS equipment_name, users.username AS borrower_name FROM loans JOIN equipment ON loans.equipment_id = equipment.id JOIN users ON loans.user_id = users.id @@ -313,15 +301,16 @@ app.get("/loans", (req, res) => { } }); - - - // หน้าแสดงรายการการยืมสำหรับ Admin app.get("/admin-loans", (req, res) => { if (req.isAuthenticated() && req.user.role === "admin") { // Query ดึงข้อมูลการยืม db.query( - 'SELECT loans.id, users.username, equipment.name AS equipment_name, loans.quantity, loans.status FROM loans JOIN users ON loans.user_id = users.id JOIN equipment ON loans.equipment_id = equipment.id WHERE loans.status = "pending"', + `SELECT loans.id, users.username, equipment.name AS equipment_name, loans.quantity, loans.status + FROM loans + JOIN users ON loans.user_id = users.id + JOIN equipment ON loans.equipment_id = equipment.id + WHERE loans.status = "pending"`, (err, loans) => { if (err) throw err; res.render("admin-loans", { user: req.user, loans: loans }); // ส่งข้อมูล user และ loans @@ -332,28 +321,21 @@ app.get("/admin-loans", (req, res) => { } }); - - - // ยืนยันการยืม (สำหรับ Admin) -// อนุมัติหรือปฏิเสธคำขอยืม (สำหรับ Admin) app.post("/approve-loan", isAuthenticated, isAdmin, (req, res) => { const { loan_id, action } = req.body; // กำหนดสถานะใหม่ - let newStatus = action === 'approve' ? 'approved' : 'rejected'; + let newStatus = action === "approve" ? "approved" : "rejected"; // อัปเดตสถานะในฐานข้อมูล - db.query( - "UPDATE loans SET status = ? WHERE id = ?", - [newStatus, loan_id], - (err) => { - if (err) throw err; - res.redirect("/admin-loans"); // หลังจากอนุมัติหรือปฏิเสธเสร็จ, กลับไปที่หน้าการจัดการการยืม - } - ); + db.query("UPDATE loans SET status = ? WHERE id = ?", [newStatus, loan_id], (err) => { + if (err) throw err; + res.redirect("/admin-loans"); // หลังจากอนุมัติหรือปฏิเสธเสร็จ, กลับไปที่หน้าการจัดการการยืม + }); }); + // Route แสดงหน้าการยืนยันการยืม app.post("/confirm-loan", (req, res) => { if (!req.user) {