Agregar, editar y borrar comentarios
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user