Cleanup logs, fixed add Venta, fixed search

This commit is contained in:
2024-01-19 23:10:20 -03:00
parent f55e4dbd5f
commit fa11f5b240
15 changed files with 238 additions and 91 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace Incoviba\Controller\API;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Service;
class Search
{
use withJson;
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);
}
}

View File

@ -1,17 +1,19 @@
<?php
namespace Incoviba\Controller\API;
use PDOException;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal\Controller;
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
class Ventas extends Controller
{
use withJson, withRedis;
@ -55,6 +57,9 @@ class Ventas
Service\Venta $service, int $venta_id): ResponseInterface
{
$redisKey = "venta:{$venta_id}";
$output = [
'venta' => null
];
try {
$venta = $this->fetchRedis($redisService, $redisKey);
$output = compact('venta');
@ -64,12 +69,7 @@ class Ventas
$output = compact('venta');
$this->saveRedis($redisService, $redisKey, $venta);
} catch (EmptyResult $exception) {
$output = [
'error' => [
'code' => $exception->getCode(),
'message' => str_replace([PHP_EOL, "\r"], [' ', ''], $exception->getMessage())
]
];
$this->logger->notice($exception);
}
}
return $this->withJson($response, $output);
@ -178,14 +178,20 @@ class Ventas
$data = $request->getParsedBody();
$output = [
'status' => false,
'venta_id' => null,
'errors' => []
];
try {
$venta = $ventaService->add($data);
$this->saveRedis($redisService, "venta:{$venta->id}", $venta);
$output['venta_id'] = $venta->id;
$output['status'] = true;
} catch (\Exception $exception) {
$output['errors'] = $exception;
} catch (EmptyResult | PDOException $exception) {
$output['errors'] = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'stack' => $exception->getTraceAsString()
];
}
return $this->withJson($response, $output);
}

View File

@ -13,11 +13,4 @@ class Search
$post = $request->getParsedBody() ?? '';
return $view->render($response, 'search', compact('post', 'query', 'tipo'));
}
public function query(ServerRequestInterface $request, ResponseInterface $response, Service\Search $service): ResponseInterface
{
$data = $request->getParsedBody();
$results = $service->query($data['query'], $data['tipo']);
$response->getBody()->write(json_encode(compact('results')));
return $response->withHeader('Content-Type', 'application/json');
}
}