From a71df4e70d36bcad1e502d1a9f8574d1cffbdc2e Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 24 Feb 2025 12:41:50 -0300 Subject: [PATCH] Controlador y ruta de operadores para API --- app/resources/routes/api/proyectos.php | 7 ++ .../routes/api/proyectos/brokers.php | 12 ++ app/src/Controller/API/Proyectos/Brokers.php | 105 ++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 app/resources/routes/api/proyectos/brokers.php create mode 100644 app/src/Controller/API/Proyectos/Brokers.php diff --git a/app/resources/routes/api/proyectos.php b/app/resources/routes/api/proyectos.php index 0dd2495..896206d 100644 --- a/app/resources/routes/api/proyectos.php +++ b/app/resources/routes/api/proyectos.php @@ -3,6 +3,13 @@ use Incoviba\Controller\API\Proyectos; $app->group('/proyectos', function($app) { $app->get('/escriturando[/]', [Proyectos::class, 'escriturando']); + $files = new FilesystemIterator(implode(DIRECTORY_SEPARATOR, [__DIR__, 'proyectos'])); + foreach ($files as $file) { + if ($file->isDir()) { + continue; + } + include_once $file->getRealPath(); + } $app->get('[/]', [Proyectos::class, 'list']); }); $app->group('/proyecto/{proyecto_id}', function($app) { diff --git a/app/resources/routes/api/proyectos/brokers.php b/app/resources/routes/api/proyectos/brokers.php new file mode 100644 index 0000000..48b5da2 --- /dev/null +++ b/app/resources/routes/api/proyectos/brokers.php @@ -0,0 +1,12 @@ +group('/brokers', function($app) { + $app->post('/add[/]', Brokers::class . ':add'); + $app->post('/edit[/]', Brokers::class . ':edit'); + $app->get('[/]', Brokers::class); +}); +$app->group('/broker/{broker_rut}', function($app) { + $app->delete('[/]', Brokers::class . ':delete'); + $app->get('[/]', Brokers::class . ':show'); +}); diff --git a/app/src/Controller/API/Proyectos/Brokers.php b/app/src/Controller/API/Proyectos/Brokers.php new file mode 100644 index 0000000..f9633ca --- /dev/null +++ b/app/src/Controller/API/Proyectos/Brokers.php @@ -0,0 +1,105 @@ +getAll(); + + return $this->withJson($response, compact('brokers')); + } + public function show(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface + { + try { + $broker = $brokerService->get($broker_rut); + return $this->withJson($response, compact('broker')); + } catch (ServiceAction\Read $exception) { + return $this->withError($response, $exception); + } + + } + public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'input' => $body, + 'brokers' => [], + 'success' => false, + 'partial' => false, + 'errors' => [] + ]; + foreach ($body['brokers'] as $jsonData) { + try { + $data = json_decode($jsonData, true); + if (is_array($jsonData)) { + $data = $jsonData; + } + $output['brokers'] []= [ + 'broker' => $brokerService->add($data), + 'success' => true + ]; + $output['partial'] = true; + } catch (ServiceAction\Create $exception) { + $output['errors'] []= $this->parseError($exception); + } + } + if (count($output['brokers']) == count($body['brokers'])) { + $output['success'] = true; + } + + return $this->withJson($response, $output); + } + public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'input' => $body, + 'brokers' => [], + 'success' => false, + 'partial' => false, + 'errors' => [] + ]; + foreach ($body['brokers'] as $data) { + try { + $output['brokers'] []= [ + 'rut' => $data['rut'], + 'broker' => $brokerService->edit(json_decode($data, true)), + 'success' => true + ]; + $output['partial'] = true; + } catch (ServiceAction\Update $exception) { + $output['errors'] []= $this->parseError($exception); + } + } + if (count($output['brokers']) == count($body['brokers'])) { + $output['success'] = true; + } + + return $this->withJson($response, $output); + } + public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface + { + $output = [ + 'broker_rut' => $broker_rut, + 'broker' => null, + 'success' => false + ]; + try { + $output['broker'] = $brokerService->delete($broker_rut); + $output['success'] = true; + } catch (ServiceAction\Delete $exception) { + return $this->withError($response, $exception); + } + + return $this->withJson($response, $output); + } +}