Controlador y ruta de operadores para API

This commit is contained in:
Juan Pablo Vial
2025-02-24 12:41:50 -03:00
parent f17b7a758a
commit a71df4e70d
3 changed files with 124 additions and 0 deletions

View File

@ -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) {

View File

@ -0,0 +1,12 @@
<?php
use Incoviba\Controller\API\Proyectos\Brokers;
$app->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');
});

View File

@ -0,0 +1,105 @@
<?php
namespace Incoviba\Controller\API\Proyectos;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Brokers
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
{
$brokers = $brokerService->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);
}
}