diff --git a/app/resources/routes/api/proyectos.php b/app/resources/routes/api/proyectos.php index 896206d..0d00dc5 100644 --- a/app/resources/routes/api/proyectos.php +++ b/app/resources/routes/api/proyectos.php @@ -27,4 +27,5 @@ $app->group('/proyecto/{proyecto_id}', function($app) { $app->group('/terreno', function($app) { $app->post('/edit[/]', [Proyectos::class, 'terreno']); }); + $app->get('/brokers', [Proyectos::class, 'brokers']); }); diff --git a/app/src/Controller/API/Proyectos.php b/app/src/Controller/API/Proyectos.php index dc5e46f..b7216cf 100644 --- a/app/src/Controller/API/Proyectos.php +++ b/app/src/Controller/API/Proyectos.php @@ -4,6 +4,7 @@ namespace Incoviba\Controller\API; use Incoviba\Common\Implement\Exception\EmptyRedis; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Controller\withRedis; +use Incoviba\Exception\ServiceAction\Read; use Incoviba\Model; use Incoviba\Repository; use Incoviba\Service; @@ -170,4 +171,20 @@ class Proyectos } catch (EmptyResult) {} return $this->withJson($response, $output); } + public function brokers(ServerRequestInterface $request, ResponseInterface $response, + Repository\Proyecto $proyectoRepository, Service\Proyecto\Broker\Contract $contractService, + int $proyecto_id): ResponseInterface + { + $output = [ + 'proyecto_id' => $proyecto_id, + 'contracts' => [] + ]; + try { + $proyecto = $proyectoRepository->fetchById($proyecto_id); + $output['contracts'] = $contractService->getByProject($proyecto->id); + } catch (EmptyResult | Read $exception) { + return $this->withError($response, $exception); + } + return $this->withJson($response, $output); + } } diff --git a/app/src/Service/Proyecto/Broker/Contract.php b/app/src/Service/Proyecto/Broker/Contract.php index c23e3b8..5695a43 100644 --- a/app/src/Service/Proyecto/Broker/Contract.php +++ b/app/src/Service/Proyecto/Broker/Contract.php @@ -29,6 +29,26 @@ class Contract extends Ideal\Service return []; } } + + /** + * @param int $project_id + * @return array + * @throws ServiceAction\Read + */ + public function getByProject(int $project_id): array + { + try { + return array_map([$this, 'process'], $this->contractRepository->fetchByProject($project_id)); + } catch (Implement\Exception\EmptyResult $exception) { + throw new ServiceAction\Read(__CLASS__, $exception); + } + } + + /** + * @param int $broker_rut + * @return array + * @throws ServiceAction\Read + */ public function getByBroker(int $broker_rut): array { try {