Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
projectmelon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160383
projectmelon
Commits
b291ec67
Commit
b291ec67
authored
3 months ago
by
65160383
Browse files
Options
Downloads
Patches
Plain Diff
werehouse_db
parent
e06804cc
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
controllers/productController.js
+39
-21
39 additions, 21 deletions
controllers/productController.js
routes/productRoutes.js
+2
-3
2 additions, 3 deletions
routes/productRoutes.js
with
41 additions
and
24 deletions
controllers/productController.js
+
39
−
21
View file @
b291ec67
...
...
@@ -86,23 +86,14 @@ exports.updateProduct = (req, res) => {
exports
.
deleteProduct
=
(
req
,
res
)
=>
{
const
{
id
}
=
req
.
params
;
// ลบข้อมูลในตาราง orders ก่อน
const
deleteOrdersQuery
=
'
DELETE FROM orders WHERE product_id = ?
'
;
db
.
query
(
deleteOrdersQuery
,
[
id
],
(
err
,
orderResult
)
=>
{
if
(
err
)
{
console
.
error
(
'
Error deleting orders:
'
,
err
);
return
res
.
redirect
(
'
/products?error=เกิดข้อผิดพลาดในการลบประวัติการเบิก
'
);
}
// หลังจากลบข้อมูลใน orders แล้ว จึงลบสินค้า
const
deleteProductQuery
=
'
DELETE FROM products WHERE id = ?
'
;
db
.
query
(
deleteProductQuery
,
[
id
],
(
err
,
productResult
)
=>
{
// ลบสินค้าโดยตรง ไม่ต้องตรวจสอบ orders
const
deleteQuery
=
'
DELETE FROM products WHERE id = ?
'
;
db
.
query
(
deleteQuery
,
[
id
],
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
error
(
'
Error deleting product:
'
,
err
);
return
res
.
redirect
(
'
/products?error=เกิดข้อผิดพลาดในการลบสินค้า
'
);
}
res
.
redirect
(
'
/products?success=ลบสินค้าและประวัติการเบิกสำเร็จ
'
);
});
res
.
redirect
(
'
/products?success=ลบสินค้าสำเร็จ
'
);
});
};
...
...
@@ -176,34 +167,61 @@ exports.showWithdrawConfirmation = (req, res) => {
};
// ยืนยันการเบิกสินค้า
exports
.
confirmWithdraw
=
(
req
,
res
)
=>
{
exports
.
withdrawProduct
=
(
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
.
redirect
(
'
/products?error=เกิดข้อผิดพลาดในการเบิกสินค้า
'
);
return
res
.
send
(
`
<script>
alert('เกิดข้อผิดพลาดในการเบิกสินค้า');
window.location.href = '/products';
</script>
`
);
}
if
(
result
.
length
>
0
)
{
const
product
=
result
[
0
];
if
(
product
.
quantity
<
quantity
)
{
return
res
.
redirect
(
'
/products?error=สินค้าในคลังไม่เพียงพอ
'
);
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
.
redirect
(
'
/products?error=เกิดข้อผิดพลาดในการอัพเดทจำนวนสินค้า
'
);
}
res
.
redirect
(
'
/products?success=เบิกสินค้าสำเร็จ
'
);
return
res
.
send
(
`
<script>
alert('เกิดข้อผิดพลาดในการอัพเดทจำนวนสินค้า');
window.location.href = '/products';
</script>
`
);
}
res
.
send
(
`
<script>
alert('เบิกสินค้าสำเร็จ');
window.location.href = '/products';
</script>
`
);
});
}
else
{
res
.
redirect
(
'
/products?error=ไม่พบสินค้า
'
);
return
res
.
send
(
`
<script>
alert('ไม่พบสินค้า');
window.location.href = '/products';
</script>
`
);
}
});
};
This diff is collapsed.
Click to expand it.
routes/productRoutes.js
+
2
−
3
View file @
b291ec67
...
...
@@ -15,7 +15,7 @@ router.post('/create', productController.createProduct);
router
.
get
(
'
/edit/:id
'
,
productController
.
editProductPage
);
// อัพเดทสินค้า
router
.
p
u
t
(
'
/update/:id
'
,
productController
.
updateProduct
);
router
.
p
os
t
(
'
/update/:id
'
,
productController
.
updateProduct
);
// ลบสินค้า
router
.
post
(
'
/delete/:id
'
,
productController
.
deleteProduct
);
...
...
@@ -24,7 +24,6 @@ router.post('/delete/:id', productController.deleteProduct);
router
.
get
(
'
/search
'
,
productController
.
searchProducts
);
// เบิกสินค้า
router
.
post
(
'
/withdraw
'
,
productController
.
showWithdrawConfirmation
);
router
.
post
(
'
/confirm-withdraw
'
,
productController
.
confirmWithdraw
);
router
.
post
(
'
/withdraw
'
,
productController
.
withdrawProduct
);
module
.
exports
=
router
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment