HarvestListController.php 845 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\HarvestModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class HarvestListController
  7. {
  8. private HarvestModel $harvestModel;
  9. public function __construct()
  10. {
  11. $this->harvestModel = new HarvestModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. try {
  16. $harvests = $this->harvestModel->getAll();
  17. } catch (\Throwable $e) {
  18. return ResponseLib::sendFail('Falha ao listar safra: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  19. }
  20. if (!$harvests) {
  21. return ResponseLib::sendFail('Nenhuma safra encontrada', [], 'E_NOT_FOUND')->withStatus(204);
  22. }
  23. return ResponseLib::sendOk($harvests, 'S_HARVEST_LIST');
  24. }
  25. }