This commit is contained in:
2023-11-23 00:53:49 -03:00
parent 9ab0515954
commit bf03e85975
32 changed files with 599 additions and 314 deletions

View File

@ -1,36 +1,53 @@
<?php
namespace Incoviba\Controller\API\Ventas;
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\API;
use Incoviba\Service;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class Precios
{
use API\withJson, API\emptyBody;
use API\withJson, API\emptyBody, \Incoviba\Controller\withRedis;
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Precio $precioService): ResponseInterface
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Precio $precioService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$proyecto_id = $json->proyecto_id;
$output = ['total' => 0];
$output = ['total' => 0, 'precios' => []];
$redisKey = "precios:proyecto:{$proyecto_id}";
try {
$precios = $precioService->getByProyecto($proyecto_id);
$output['precios'] = $precios;
$output['total'] = count($precios);
} catch (EmptyResult) {}
$output['precios'] = $this->fetchRedis($redisService, $redisKey);
$output['total'] = count($output['precios']);
} catch (EmptyRedis) {
try {
$precios = $precioService->getByProyecto($proyecto_id);
$output['precios'] = $precios;
$output['total'] = count($precios);
$this->saveRedis($redisService, $redisKey, $output['precios']);
} catch (EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Precio $precioService, int $unidad_id): ResponseInterface
public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Precio $precioService, int $unidad_id): ResponseInterface
{
$redisKey = "precio:unidad:{$unidad_id}";
try {
$precio = $precioService->getVigenteByUnidad($unidad_id);
$precio = $this->fetchRedis($redisService, $redisKey);
return $this->withJson($response, compact('precio'));
} catch (EmptyResult) {
return $this->emptyBody($response);
} catch (EmptyRedis) {
try {
$precio = $precioService->getVigenteByUnidad($unidad_id);
$this->saveRedis($redisService, $redisKey, $precio);
return $this->withJson($response, compact('precio'));
} catch (EmptyResult) {
return $this->emptyBody($response);
}
}
}
}