KitchenGetController.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Models\KitchenModel;
  6. use Respect\Validation\Validator as v;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class KitchenGetController
  9. {
  10. private KitchenModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new KitchenModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string) $request->getBody(), true) ?? [];
  18. try {
  19. v::key('company_id', v::intType()->positive())->assert($body);
  20. } catch (ValidationException $e) {
  21. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
  22. }
  23. $companyId = (int) $body['company_id'];
  24. $orders = $this->model->getKitchenOrders($companyId);
  25. if (!$orders) {
  26. return ResponseLib::sendFail("No kitchen orders found", [], "E_NO_DATA")->withStatus(204);
  27. }
  28. return ResponseLib::sendOk($orders);
  29. }
  30. }