Redis
This commit is contained in:
@ -2,19 +2,21 @@
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Controller\withRedis;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Ventas
|
||||
{
|
||||
use withJson, withRedis;
|
||||
|
||||
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta $service): ResponseInterface
|
||||
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Repository\Proyecto $proyectoRepository, Repository\Venta $ventaRepository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
@ -25,26 +27,50 @@ class Ventas
|
||||
],
|
||||
'total' => 0
|
||||
];
|
||||
$proyectosKey = "proyectos";
|
||||
try {
|
||||
$ventas = $service->fetchActivaByProyecto($proyecto_id);
|
||||
$output['ventas'] = array_map(function(Model\Venta $venta) {return $venta->id;}, $ventas);
|
||||
$output['proyecto']['descripcion'] = $ventas[0]->proyecto()->descripcion;
|
||||
$output['total'] = count($ventas);
|
||||
} catch (EmptyResult) {}
|
||||
$proyectos = $this->fetchRedis($redisService, $proyectosKey);
|
||||
} catch (EmptyRedis) {
|
||||
$proyectos = $proyectoRepository->fetchAllActive();
|
||||
$this->saveRedis($redisService, $proyectosKey, $proyectos);
|
||||
}
|
||||
$proyecto = array_values(array_filter($proyectos, function($proyecto) use ($proyecto_id) {return $proyecto->id === $proyecto_id;}))[0];
|
||||
$output['proyecto']['descripcion'] = $proyecto->descripcion;
|
||||
|
||||
$redisKey = "ventas:proyecto:{$proyecto_id}";
|
||||
try {
|
||||
$output['ventas'] = $this->fetchRedis($redisService, $redisKey);
|
||||
$output['total'] = count($output['ventas']);
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$ventas = $ventaRepository->fetchActivaByProyecto($proyecto_id);
|
||||
$output['ventas'] = array_map(function(Model\Venta $venta) {return $venta->id;}, $ventas);
|
||||
$output['total'] = count($ventas);
|
||||
$this->saveRedis($redisService, $redisKey, $output['ventas']);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $service, int $venta_id): ResponseInterface
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Service\Venta $service, int $venta_id): ResponseInterface
|
||||
{
|
||||
$redisKey = "venta:{$venta_id}";
|
||||
try {
|
||||
$venta = $service->getById($venta_id);
|
||||
$venta = $this->fetchRedis($redisService, $redisKey);
|
||||
$output = compact('venta');
|
||||
} catch (EmptyResult $exception) {
|
||||
$output = [
|
||||
'error' => [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => str_replace([PHP_EOL, "\r"], [' ', ''], $exception->getMessage())
|
||||
]
|
||||
];
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$venta = $service->getById($venta_id);
|
||||
$output = compact('venta');
|
||||
$this->saveRedis($redisService, $redisKey, $venta);
|
||||
} catch (EmptyResult $exception) {
|
||||
$output = [
|
||||
'error' => [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => str_replace([PHP_EOL, "\r"], [' ', ''], $exception->getMessage())
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
@ -54,7 +80,7 @@ class Ventas
|
||||
$json = json_decode($body->getContents());
|
||||
$proyecto_id = $json->proyecto_id;
|
||||
$today = new DateTimeImmutable();
|
||||
$redisKey = "promesas_por_firmar-proyecto-{$proyecto_id}-{$today->format('Y-m-d')}";
|
||||
$redisKey = "promesas:por_firmar:proyecto:{$proyecto_id}:{$today->format('Y-m-d')}";
|
||||
|
||||
$output = [
|
||||
'proyecto_id' => $proyecto_id,
|
||||
@ -80,7 +106,7 @@ class Ventas
|
||||
$json = json_decode($body->getContents());
|
||||
$proyecto_id = $json->proyecto_id;
|
||||
$today = new DateTimeImmutable();
|
||||
$redisKey = "escrituras-proyecto-{$proyecto_id}-{$today->format('Y-m-d')}";
|
||||
$redisKey = "escrituras:proyecto:{$proyecto_id}:{$today->format('Y-m-d')}";
|
||||
|
||||
$output = [
|
||||
'proyecto_id' => $proyecto_id,
|
||||
@ -127,18 +153,27 @@ class Ventas
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function comentarios(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $service, Repository\Venta\Comentario $comentarioRepository, int $venta_id): ResponseInterface
|
||||
public function comentarios(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Service\Venta $service, Repository\Venta\Comentario $comentarioRepository, int $venta_id): ResponseInterface
|
||||
{
|
||||
$venta = $service->getById($venta_id);
|
||||
$output = ['total' => 0];
|
||||
$output = ['total' => 0, 'comentarios' => []];
|
||||
$redisKey = "comentarios:venta:{$venta_id}";
|
||||
try {
|
||||
$comentarios = $comentarioRepository->fetchByVenta($venta->id);
|
||||
$output['total'] = count($comentarios);
|
||||
$output['comentarios'] = $comentarios;
|
||||
} catch (EmptyResult) {}
|
||||
$output['comentarios'] = $this->fetchRedis($redisService, $redisKey);
|
||||
$output['total'] = count($output['comentarios']);
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$comentarios = $comentarioRepository->fetchByVenta($venta->id);
|
||||
$output['total'] = count($comentarios);
|
||||
$output['comentarios'] = $comentarios;
|
||||
$this->saveRedis($redisService, $redisKey, $output['comentarios']);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService): ResponseInterface
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Service\Venta $ventaService): ResponseInterface
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
$output = [
|
||||
@ -146,28 +181,36 @@ class Ventas
|
||||
'errors' => []
|
||||
];
|
||||
try {
|
||||
$ventaService->add($data);
|
||||
$venta = $ventaService->add($data);
|
||||
$this->saveRedis($redisService, "venta:{$venta->id}", $venta);
|
||||
$output['status'] = true;
|
||||
} catch (\Exception $exception) {
|
||||
$output['errors'] = $exception;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface
|
||||
public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'venta_id' => $venta_id,
|
||||
'unidades' => []
|
||||
];
|
||||
$redisKey = "unidades:venta:{$venta_id}";
|
||||
try {
|
||||
$unidades = $unidadService->getByVenta($venta_id);
|
||||
$output['unidades'] = json_decode(json_encode($unidades));
|
||||
array_walk($output['unidades'], function($unidad, $index, $unidades) {
|
||||
$unidad->prorrateo = $unidades[$index]->prorrateo;
|
||||
$unidad->precios = $unidades[$index]->precios;
|
||||
$unidad->current_precio = $unidades[$index]->currentPrecio;
|
||||
}, $unidades);
|
||||
} catch (EmptyResult) {}
|
||||
$output['unidades'] = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$unidades = $unidadService->getByVenta($venta_id);
|
||||
$output['unidades'] = json_decode(json_encode($unidades));
|
||||
array_walk($output['unidades'], function($unidad, $index, $unidades) {
|
||||
$unidad->prorrateo = $unidades[$index]->prorrateo;
|
||||
$unidad->precios = $unidades[$index]->precios;
|
||||
$unidad->current_precio = $unidades[$index]->currentPrecio;
|
||||
}, $unidades);
|
||||
$this->saveRedis($redisService, $redisKey, $output['unidades']);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user