CORS
This commit is contained in:
@ -10,11 +10,6 @@
|
||||
<div class="column">
|
||||
Inmobiliarias
|
||||
</div>
|
||||
{{--<div class="right aligned column">
|
||||
<button class="ui icon button" type="button">
|
||||
<i class="plus icon"></i>
|
||||
</button>
|
||||
</div>--}}
|
||||
</h2>
|
||||
<div class="ui divider"></div>
|
||||
<div class="ui cards">
|
||||
@ -22,9 +17,7 @@
|
||||
<div class="ui card">
|
||||
<div class="content">
|
||||
<div class="header">
|
||||
{{$inmobiliaria->abreviacion}}
|
||||
{{--<a href="{{$urls->base}}/inmobiliaria/{{$inmobiliaria->rut}}">
|
||||
</a>--}}
|
||||
{{$inmobiliaria->abreviacion}}
|
||||
</div>
|
||||
<div class="description">{{$inmobiliaria->razon}} {{$inmobiliaria->tipoSociedad->descripcion}}</div>
|
||||
<div class="meta">{{$inmobiliaria->rut()}}</div>
|
||||
|
2
app/setup/middlewares/96_cors.php
Normal file
2
app/setup/middlewares/96_cors.php
Normal file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
$app->add($app->getContainer()->get(Incoviba\Middleware\CORS::class));
|
@ -12,6 +12,16 @@ class Inmobiliarias
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Repository\Inmobiliaria $inmobiliariaRepository): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliarias' => []
|
||||
];
|
||||
try {
|
||||
$output['inmobiliarias'] = $inmobiliariaRepository->fetchAll('razon');
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function cuentas(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Inmobiliaria\Cuenta $cuentaRepository, int $inmobiliaria_rut): ResponseInterface
|
||||
|
@ -20,8 +20,8 @@ class Inmobiliarias
|
||||
$inmobiliarias = [];
|
||||
try {
|
||||
$inmobiliarias = array_map(function($row) use ($inmobiliariaRepository) {
|
||||
return $inmobiliariaRepository->load((array) $row);
|
||||
}, $this->fetchRedis($redisService, $redisKey));
|
||||
return $inmobiliariaRepository->load($row);
|
||||
}, $this->fetchRedis($redisService, $redisKey, true));
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$inmobiliarias = $inmobiliariaRepository->fetchAll();
|
||||
|
@ -6,13 +6,13 @@ use Incoviba\Service;
|
||||
|
||||
trait withRedis
|
||||
{
|
||||
public function fetchRedis(Service\Redis $redisService, string $redisKey): mixed
|
||||
public function fetchRedis(Service\Redis $redisService, string $redisKey, ?bool $asArray = null): mixed
|
||||
{
|
||||
$jsonString = $redisService->get($redisKey);
|
||||
if ($jsonString === null) {
|
||||
throw new EmptyRedis($redisKey);
|
||||
}
|
||||
return json_decode($jsonString);
|
||||
return json_decode($jsonString, $asArray);
|
||||
}
|
||||
public function saveRedis(Service\Redis $redisService, string $redisKey, mixed $value, ?int $expiration = null): void
|
||||
{
|
||||
|
21
app/src/Middleware/CORS.php
Normal file
21
app/src/Middleware/CORS.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Incoviba\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseFactoryInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class CORS
|
||||
{
|
||||
public function __construct(protected ResponseFactoryInterface $responseFactory) {}
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if ($request->getMethod() === 'OPTIONS') {
|
||||
return $this->responseFactory->createResponse()
|
||||
->withHeader('Access-Control-Allow-Origin', '*')
|
||||
->withHeader('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
@ -10,8 +10,6 @@ class Inmobiliaria extends Model
|
||||
public ?string $dv;
|
||||
public ?string $razon;
|
||||
public ?string $abreviacion;
|
||||
public ?string $cuenta;
|
||||
public ?Banco $banco;
|
||||
public ?TipoSociedad $tipoSociedad;
|
||||
|
||||
public function rut(): string
|
||||
@ -37,8 +35,6 @@ class Inmobiliaria extends Model
|
||||
'rut_formateado' => $this->rut(),
|
||||
'razon' => $this->razon ?? '',
|
||||
'abreviacion' => $this->abreviacion ?? '',
|
||||
'cuenta' => $this->cuenta ?? '',
|
||||
'banco' => $this->banco ?? '',
|
||||
'tipo_sociedad' => $this->tipoSociedad ?? ''
|
||||
];
|
||||
}
|
||||
|
@ -20,13 +20,9 @@ class Inmobiliaria extends Ideal\Repository
|
||||
return 'rut';
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Define\Model
|
||||
public function create(?array $data = null): Model\Inmobiliaria
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['dv', 'razon', 'abreviacion', 'cuenta']))
|
||||
->register('banco', (new Implement\Repository\Mapper())
|
||||
->setFunction(function($data) {
|
||||
return $this->bancoRepository->fetchById($data['banco']);
|
||||
}))
|
||||
$map = (new Implement\Repository\MapperParser(['dv', 'razon', 'abreviacion']))
|
||||
->register('sociedad', (new Implement\Repository\Mapper())
|
||||
->setProperty('tipoSociedad')
|
||||
->setFunction(function($data) {
|
||||
@ -34,7 +30,7 @@ class Inmobiliaria extends Ideal\Repository
|
||||
}));
|
||||
return $this->parseData(new Model\Inmobiliaria(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Define\Model
|
||||
public function save(Define\Model $model): Model\Inmobiliaria
|
||||
{
|
||||
$model->rut = $this->saveNew(
|
||||
['dv', 'razon', 'abreviacion', 'cuenta', 'banco', 'sociedad'],
|
||||
@ -42,7 +38,7 @@ class Inmobiliaria extends Ideal\Repository
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria
|
||||
{
|
||||
return $this->update($model, ['dv', 'razon', 'abreviacion', 'cuenta', 'banco', 'sociedad'], $new_data);
|
||||
}
|
||||
|
Reference in New Issue
Block a user