Gitlab@Informatics

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

update submit loan

parent c486147d
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -43,13 +43,15 @@
<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>
......@@ -70,7 +72,7 @@
fetch('/submit-loan', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json', // ตั้งค่า header ให้เป็น application/json
},
body: JSON.stringify({
equipment_id: equipment_id,
......@@ -88,6 +90,7 @@
.catch(error => {
alert('เกิดข้อผิดพลาด: ' + error);
});
});
</script>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment