Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit e06804cc authored by 65160383's avatar 65160383
Browse files

werehouse_db

parent 7314dfd9
No related branches found
No related tags found
No related merge requests found
......@@ -127,68 +127,6 @@ exports.searchProducts = (req, res) => {
});
};
// ฟังก์ชันสำหรับการสั่งสินค้า
exports.placeOrder = (req, res) => {
const { product_id, quantity } = req.body;
// ตรวจสอบสินค้าในคลัง
const query = 'SELECT * FROM products WHERE id = ?';
db.query(query, [product_id], (err, result) => {
if (err) {
console.error(err);
return res.send(`
<script>
alert('เกิดข้อผิดพลาดในการสั่งซื้อ');
window.location.href = '/products';
</script>
`);
}
if (result.length > 0) {
const product = result[0];
// ตรวจสอบว่าสินค้ามีจำนวนเพียงพอหรือไม่
if (product.quantity < quantity) {
return res.send(`
<script>
alert('สินค้าในคลังไม่เพียงพอ');
window.location.href = '/products';
</script>
`);
}
// อัพเดทจำนวนสินค้า
const updateQuery = 'UPDATE products SET quantity = quantity - ? WHERE id = ?';
db.query(updateQuery, [quantity, product_id], (err, updateResult) => {
if (err) {
console.error(err);
return res.send(`
<script>
alert('เกิดข้อผิดพลาดในการอัพเดทจำนวนสินค้า');
window.location.href = '/products';
</script>
`);
}
// แสดงข้อความสำเร็จและรีเฟรชหน้า
res.send(`
<script>
alert('สั่งซื้อสำเร็จ');
window.location.href = '/products';
</script>
`);
});
} else {
return res.send(`
<script>
alert('ไม่พบสินค้า');
window.location.href = '/products';
</script>
`);
}
});
};
// แสดงหน้าเพิ่มสินค้า
exports.addProductPage = (req, res) => {
const query = 'SELECT * FROM categories ORDER BY name';
......
......@@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment