Poder editar la direccion de un proyecto
This commit is contained in:
@ -14,6 +14,21 @@ class Regiones
|
||||
{
|
||||
use withRedis, withJson;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Region $regionRepository): ResponseInterface
|
||||
{
|
||||
$regiones = [];
|
||||
try {
|
||||
$regiones = $this->fetchRedis($redisService, 'regiones');
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$regiones = $regionRepository->fetchAll();
|
||||
$this->saveRedis($redisService, 'regiones', $regiones, 60 * 60 * 24 * 30);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, compact('regiones'));
|
||||
}
|
||||
|
||||
public function provincias(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Provincia $provinciaRepository, int $region_id): ResponseInterface
|
||||
{
|
||||
@ -36,4 +51,54 @@ class Regiones
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function comunas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Provincia $provinciaRepository,
|
||||
Repository\Comuna $comunaRepository, int $region_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'region_id' => $region_id,
|
||||
'comunas' => []
|
||||
];
|
||||
$comunas = [];
|
||||
try {
|
||||
$redisKey = "provincias:region:{$region_id}";
|
||||
$provincias = $this->fetchRedis($redisService, $redisKey);
|
||||
foreach ($provincias as $provincia) {
|
||||
$comunas = array_merge($comunas, $this->fetchComunasByProvincia($redisService, $comunaRepository, $provincia->id));
|
||||
}
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$provincias = $provinciaRepository->fetchByRegion($region_id);
|
||||
usort($provincias, function (Model\Provincia $a, Model\Provincia $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$this->saveRedis($redisService, $redisKey, $provincias, 60 * 60 * 24 * 30);
|
||||
foreach ($provincias as $provincia) {
|
||||
$comunas = array_merge($comunas, $this->fetchComunasByProvincia($redisService, $comunaRepository, $provincia->id));
|
||||
}
|
||||
} catch (EmptyResult) {}
|
||||
} finally {
|
||||
usort($comunas, function ($a, $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$output['comunas'] = $comunas;
|
||||
}
|
||||
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
||||
protected function fetchComunasByProvincia(Service\Redis $redisService, Repository\Comuna $comunaRepository, int $provincia_id): array
|
||||
{
|
||||
$redisKey = "comunas:provincia:{$provincia_id}";
|
||||
try {
|
||||
$comunas = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
$comunas = $comunaRepository->fetchByProvincia($provincia_id);
|
||||
usort($comunas, function (Model\Comuna $a, Model\Comuna $b) {
|
||||
return strcmp($a->descripcion, $b->descripcion);
|
||||
});
|
||||
$this->saveRedis($redisService, $redisKey, $comunas, 60 * 60 * 24 * 30);
|
||||
}
|
||||
return $comunas;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user