Gitlab@Informatics

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

update submit loan

parent fdb39e3d
No related branches found
No related tags found
No related merge requests found
......@@ -377,31 +377,32 @@ app.post("/confirm-loan", (req, res) => {
// Route สำหรับยืนยันการยืม
app.post('/submit-loan', (req, res) => {
app.post('/submit-loan', async (req, res) => {
try {
const user_id = req.user.id; // สมมติว่าผู้ใช้ล็อกอินแล้ว
const equipment_id = req.body.equipment_id;
const quantity = req.body.quantity;
const { equipment_id, quantity } = req.body;
// ตรวจสอบว่าอุปกรณ์มีจำนวนเพียงพอหรือไม่
db.query('SELECT * FROM equipment WHERE id = ?', [equipment_id], (err, result) => {
if (err) throw err;
const equipment = result[0];
const [equipment] = await db.promise().query('SELECT * FROM equipment WHERE id = ?', [equipment_id]);
if (equipment.quantity >= quantity) {
if (equipment && 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;
await db.promise().query('INSERT INTO loans (user_id, equipment_id, quantity, status) VALUES (?, ?, ?, "pending")', [user_id, equipment_id, quantity]);
// ปรับจำนวนอุปกรณ์ในตาราง equipment
db.query('UPDATE equipment SET quantity = quantity - ? WHERE id = ?', [quantity, equipment_id], (err) => {
if (err) throw err;
res.redirect('/loans'); // เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์"
});
});
await db.promise().query('UPDATE equipment SET quantity = quantity - ? WHERE id = ?', [quantity, equipment_id]);
// เปลี่ยนเส้นทางไปยังหน้า "รวมการยืมอุปกรณ์"
res.redirect('/loans');
} else {
res.send('จำนวนอุปกรณ์ไม่เพียงพอ');
}
} catch (err) {
console.error('Error occurred:', err);
res.status(500).send('เกิดข้อผิดพลาดในขณะดำเนินการ');
}
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment