ProductUpdateController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\ProductModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class ProductUpdateController
  7. {
  8. private ProductModel $model;
  9. public function __construct()
  10. {
  11. $this->model = new ProductModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $body = json_decode((string)$request->getBody(), true) ?? [];
  16. $companyId = $body['company_id'] ?? null;
  17. $productId = $body['update_product_id'] ?? null;
  18. $productName = $body['product_name'] ?? null;
  19. $productPrice = $body['product_price'] ?? null;
  20. if (!$companyId || !$productId) {
  21. return ResponseLib::sendFail("Missing company_id or update_product_id", [], "E_VALIDATE")->withStatus(400);
  22. }
  23. if ($productName === null && $productPrice === null) {
  24. return ResponseLib::sendFail("Missing product_name or product_price for update", [], "E_VALIDATE")->withStatus(400);
  25. }
  26. $updated = $this->model->updateProduct(
  27. (int)$productId,
  28. (int)$companyId,
  29. $productName,
  30. $productPrice !== null ? (float)$productPrice : null
  31. );
  32. return $updated ? ResponseLib::sendOk(['updated' => true]) : ResponseLib::sendFail("Failed to Update Product or Product Not Found", [], "E_DATABASE")->withStatus(404);
  33. }
  34. }