Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 2e6a56d1 authored by 65160024's avatar 65160024
Browse files

update index.js

parent e7677368
No related branches found
No related tags found
No related merge requests found
......@@ -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) => {
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) => {
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) => {
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,27 +321,20 @@ 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) => {
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) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment