Files
oficial/app/src/Controller/API/Ventas/Precios.php

55 lines
2.2 KiB
PHP

<?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\{withJson,emptyBody};
use Incoviba\Service;
use Incoviba\Controller\withRedis;
class Precios
{
use withJson, emptyBody, withRedis;
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, 'precios' => []];
$redisKey = "precios:proyecto:{$proyecto_id}";
try {
$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\Redis $redisService,
Service\Venta\Precio $precioService, int $unidad_id): ResponseInterface
{
$redisKey = "precio:unidad:{$unidad_id}";
try {
$precio = $this->fetchRedis($redisService, $redisKey);
return $this->withJson($response, compact('precio'));
} 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);
}
}
}
}