170 lines
7.1 KiB
PHP
170 lines
7.1 KiB
PHP
<?php
|
|
namespace Incoviba\Controller\API;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Incoviba\Controller\withRedis;
|
|
use Incoviba\Common\Implement\Exception;
|
|
use Incoviba\Service;
|
|
|
|
class Search
|
|
{
|
|
use withJson, withRedis;
|
|
|
|
public function query(ServerRequestInterface $request, ResponseInterface $response,
|
|
Service\Search $service): ResponseInterface
|
|
{
|
|
$data = $request->getParsedBody();
|
|
$results = $service->query($data['query'], $data['tipo']);
|
|
$output = compact('results');
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Service\Venta\Unidad $unidadService, int $unidad_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'unidad_id' => $unidad_id,
|
|
'unidad' => null
|
|
];
|
|
$redisKey = "search:unidad:{$unidad_id}";
|
|
try {
|
|
$output['unidad'] = $this->fetchRedis($redisService, $redisKey);
|
|
} catch (Exception\EmptyRedis) {
|
|
try {
|
|
$unidad = $unidadService->getByIdForSearch($unidad_id);
|
|
$output['unidad'] = [
|
|
'id' => $unidad['id'],
|
|
'descripcion' => $unidad['descripcion'],
|
|
'proyecto_tipo_unidad' => [
|
|
'proyecto' => [
|
|
'id' => $unidad['proyecto_id'],
|
|
'descripcion' => $unidad['proyecto_descripcion']
|
|
],
|
|
'tipo_unidad' => [
|
|
'descripcion' => $unidad['tipo_unidad_descripcion']
|
|
],
|
|
'superficie' => $unidad['superficie']
|
|
],
|
|
'current_precio' => [
|
|
'valor' => $unidad['precio']
|
|
]
|
|
];
|
|
$this->saveRedis($redisService, $redisKey, $output['unidad']);
|
|
} catch (Exception\EmptyResult) {}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Service\Venta\Unidad $unidadService): ResponseInterface
|
|
{
|
|
$body = $request->getParsedBody();
|
|
$output = [
|
|
'input' => $body,
|
|
'unidades' => []
|
|
];
|
|
$unidades = explode(',', $body['unidades']);
|
|
foreach ($unidades as $unidad_id) {
|
|
$redisKey = "search:unidad:{$unidad_id}";
|
|
try {
|
|
$output['unidades'] []= $this->fetchRedis($redisService, $redisKey);
|
|
} catch (Exception\EmptyRedis) {
|
|
try {
|
|
$unidad = $unidadService->getByIdForSearch($unidad_id);
|
|
$unidad = [
|
|
'id' => $unidad['id'],
|
|
'descripcion' => $unidad['descripcion'],
|
|
'proyecto_tipo_unidad' => [
|
|
'proyecto' => [
|
|
'id' => $unidad['proyecto_id'],
|
|
'descripcion' => $unidad['proyecto_descripcion']
|
|
],
|
|
'tipo_unidad' => [
|
|
'descripcion' => $unidad['tipo_unidad_descripcion']
|
|
],
|
|
'superficie' => $unidad['superficie']
|
|
],
|
|
'current_precio' => [
|
|
'valor' => $unidad['precio']
|
|
]
|
|
];
|
|
$output['unidades'] []= $unidad;
|
|
$this->saveRedis($redisService, $redisKey, $unidad);
|
|
} catch (Exception\EmptyResult) {}
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function venta(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Service\Venta $ventaService, int $venta_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'venta_id' => $venta_id,
|
|
'venta' => null
|
|
];
|
|
$redisKey = "search:venta:{$venta_id}";
|
|
try {
|
|
$output['venta'] = $this->fetchRedis($redisService, $redisKey);
|
|
} catch (Exception\EmptyRedis) {
|
|
try {
|
|
$venta = $ventaService->getById($venta_id);
|
|
$output['venta'] = $venta;
|
|
$this->saveRedis($redisService, $redisKey, $output['venta']);
|
|
} catch (Exception\EmptyResult) {}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Service\Venta $ventaService): ResponseInterface
|
|
{
|
|
$body = $request->getParsedBody();
|
|
$output = [
|
|
'input' => $body,
|
|
'ventas' => []
|
|
];
|
|
$ventas = explode(',', $body['ventas']);
|
|
foreach ($ventas as $venta_id) {
|
|
$redisKey = "search:venta:{$venta_id}";
|
|
try {
|
|
$output['ventas'] []= $this->fetchRedis($redisService, $redisKey);
|
|
} catch (Exception\EmptyRedis) {
|
|
try {
|
|
$venta = $ventaService->getByIdForSearch($venta_id);
|
|
$venta = [
|
|
'id' => $venta['id'],
|
|
'proyecto' => [
|
|
'id' => $venta['proyecto_id'],
|
|
'descripcion' => $venta['proyecto_descripcion']
|
|
],
|
|
'propietario' => [
|
|
'nombre_completo' => $venta['propietario']
|
|
],
|
|
'propiedad' => [
|
|
'unidades' => [
|
|
[
|
|
'descripcion' => $venta['unidad_descripcion'],
|
|
'proyecto_tipo_unidad' => [
|
|
'tipo_unidad' => [
|
|
'descripcion' => $venta['tipo_unidad_descripcion']
|
|
],
|
|
'superficie' => $venta['superficie']
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'fecha' => $venta['fecha'],
|
|
'current_estado' => [
|
|
'tipo_estado_venta' => [
|
|
'activa' => $venta['activa']
|
|
]
|
|
],
|
|
'valor' => $venta['valor']
|
|
];
|
|
$output['ventas'] []= $venta;
|
|
$this->saveRedis($redisService, $redisKey, json_encode($venta));
|
|
} catch (Exception\EmptyResult) {}
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|