Agregar, editar y borrar comentarios

This commit is contained in:
Juan Pablo Vial
2024-07-04 18:09:25 -04:00
parent fc543729d2
commit a428aeebe1
7 changed files with 358 additions and 24 deletions

View File

@ -12,6 +12,7 @@ use Incoviba\Controller\withRedis;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Log\LoggerInterface;
class Ventas extends Controller
{
@ -190,7 +191,7 @@ class Ventas extends Controller
$output['total'] = count($output['comentarios']);
} catch (EmptyRedis) {
try {
$comentarios = $comentarioRepository->fetchByVenta($venta->id);
$comentarios = $comentarioRepository->fetchActiveByVenta($venta->id);
$output['total'] = count($comentarios);
$output['comentarios'] = $comentarios;
$this->saveRedis($redisService, $redisKey, $output['comentarios']);
@ -337,4 +338,36 @@ class Ventas extends Controller
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function addComentario(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta $ventaRepository, Repository\Venta\Comentario $comentarioRepository,
Service\Redis $redisService,
int $venta_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $body,
'comentario' => null,
'added' => false
];
try {
$venta = $ventaRepository->fetchById($venta_id);
$body['venta'] = $venta->id;
$body['estado'] = true;
$filteredData = $comentarioRepository->filterData($body);
try {
$comentarioRepository->fetchByVentaAndFechaAndTexto($venta_id, new DateTimeImmutable($filteredData['fecha']), $filteredData['texto']);
} catch (EmptyResult) {
$redisKey = "comentarios:venta:{$venta_id}";
$comentarios = $this->fetchRedis($redisService, $redisKey);
$comentario = $comentarioRepository->create($filteredData);
$output['comentario'] = $comentarioRepository->save($comentario);
$comentarios []= $comentario;
$this->saveRedis($redisService, $redisKey, $comentarios);
}
$output['added'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Controller\withRedis;
use Incoviba\Repository;
use Incoviba\Service;
class Comentarios
{
use withJson, withRedis;
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta\Comentario $comentarioRepository,
Service\Redis $redisService, int $comentario_id): ResponseInterface
{
$output = [
'comentario_id' => $comentario_id,
'comentario' => null,
'edited' => false
];
$data = $request->getParsedBody();
try {
$comentario = $comentarioRepository->fetchById($comentario_id);
$filteredData = $comentarioRepository->filterData($data);
$comentario = $comentarioRepository->edit($comentario, $filteredData);
$redisKey = "comentarios:venta:{$comentario->venta->id}";
$comentarios = $this->fetchRedis($redisService, $redisKey);
$index = array_column(json_decode(json_encode($comentarios), true), 'id');
$idx = array_search($comentario->id, $index);
$comentarios[$idx] = $comentario;
$this->saveRedis($redisService, $redisKey, $comentarios);
$output['comentario'] = $comentario;
$output['edited'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta\Comentario $comentarioRepository, Service\Redis $redisService,
int $comentario_id): ResponseInterface
{
$output = [
'comentario_id' => $comentario_id,
'comentario' => null,
'removed' => false
];
try {
$comentario = $comentarioRepository->fetchById($comentario_id);
$output['comentario'] = $comentario;
$comentarioRepository->remove($comentario);
$redisKey = "comentarios:venta:{$comentario->venta->id}";
$comentarios = $this->fetchRedis($redisService, $redisKey);
$index = array_column(json_decode(json_encode($comentarios), true), 'id');
$idx = array_search($comentario->id, $index);
unset($comentarios[$idx]);
$this->saveRedis($redisService, $redisKey, $comentarios);
$output['removed'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}