| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Models\ProductModel;
- use Psr\Http\Message\ServerRequestInterface;
- class ProductUpdateController
- {
- private ProductModel $model;
- public function __construct()
- {
- $this->model = new ProductModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true) ?? [];
- $companyId = $body['company_id'] ?? null;
- $productId = $body['update_product_id'] ?? null;
- $productName = $body['product_name'] ?? null;
- $productPrice = $body['product_price'] ?? null;
- if (!$companyId || !$productId) {
- return ResponseLib::sendFail("Missing company_id or update_product_id", [], "E_VALIDATE")->withStatus(400);
- }
- if ($productName === null && $productPrice === null) {
- return ResponseLib::sendFail("Missing product_name or product_price for update", [], "E_VALIDATE")->withStatus(400);
- }
- $updated = $this->model->updateProduct(
- (int)$productId,
- (int)$companyId,
- $productName,
- $productPrice !== null ? (float)$productPrice : null
- );
- return $updated ? ResponseLib::sendOk(['updated' => true]) : ResponseLib::sendFail("Failed to Update Product or Product Not Found", [], "E_DATABASE")->withStatus(404);
- }
- }
|