| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Models\ProductModel;
- use Psr\Http\Message\ServerRequestInterface;
- use Respect\Validation\Validator as v;
- use Respect\Validation\Exceptions\ValidationException;
- class ProductGetController
- {
- private ProductModel $model;
- public function __construct()
- {
- $this->model = new ProductModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true) ?? [];
- try {
- v::key('company_id', v::intType()->positive())
- ->key('show_description', v::optional(v::boolType()), false)
- ->assert($body);
- } catch (ValidationException $e) {
- return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
- }
- $companyId = (int) $body['company_id'];
- $showDescription = $body['show_description'] ?? false;
- $products = $this->model->getProducts($companyId, $showDescription);
- if ($products) {
- return ResponseLib::sendOk($products);
- }
- return ResponseLib::sendFail("Failed to Retrieve Products", [], "E_DATABASE")->withStatus(500);
- }
- }
|