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);
|
||||
}
|
||||
}
|
||||
|
68
app/src/Controller/API/Ventas/Comentarios.php
Normal file
68
app/src/Controller/API/Ventas/Comentarios.php
Normal 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);
|
||||
}
|
||||
}
|
@ -3,9 +3,11 @@ namespace Incoviba\Model\Venta;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model\Venta;
|
||||
|
||||
class Comentario extends Ideal\Model
|
||||
{
|
||||
public Venta $venta;
|
||||
public DateTimeInterface $fecha;
|
||||
public string $texto;
|
||||
public bool $activo;
|
||||
@ -13,6 +15,7 @@ class Comentario extends Ideal\Model
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'venta_id' => $this->venta->id,
|
||||
'fecha' => $this->fecha->format('Y-m-d'),
|
||||
'texto' => $this->texto,
|
||||
'activo' => $this->activo
|
||||
|
@ -6,10 +6,11 @@ use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Comentario extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection)
|
||||
public function __construct(Define\Connection $connection, protected Repository\Venta $ventaRepsitory)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('comentario');
|
||||
@ -18,6 +19,10 @@ class Comentario extends Ideal\Repository
|
||||
public function create(?array $data = null): Define\Model
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['texto']))
|
||||
->register('venta', (new Implement\Repository\Mapper())
|
||||
->setFunction(function($data) {
|
||||
return $this->ventaRepsitory->fetchById($data['venta']);
|
||||
}))
|
||||
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'))
|
||||
->register('estado', new Implement\Repository\Mapper\Boolean('estado', 'activo'));
|
||||
return $this->parseData(new Model\Venta\Comentario(), $data, $map);
|
||||
@ -25,19 +30,40 @@ class Comentario extends Ideal\Repository
|
||||
public function save(Define\Model $model): Define\Model
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['fecha', 'texto', 'estado'],
|
||||
[$model->fecha->format('Y-m-d'), $model->texto, $model->activo ? 1 : 0]
|
||||
['venta', 'fecha', 'texto', 'estado'],
|
||||
[$model->venta->id, $model->fecha->format('Y-m-d'), $model->texto, $model->activo ? 1 : 0]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
{
|
||||
return $this->update($model, ['fecha', 'texto', 'estado'], $new_data);
|
||||
return $this->update($model, ['venta', 'fecha', 'texto', 'estado'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByVenta(int $venta_id): array
|
||||
public function fetchActiveByVenta(int $venta_id): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `venta` = ? AND `estado` = 1 ORDER BY `fecha` DESC";
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('venta = ? AND estado = 1')
|
||||
->order('fecha DESC');
|
||||
return $this->fetchMany($query, [$venta_id]);
|
||||
}
|
||||
public function fetchByVenta(int $venta_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('venta = ?')
|
||||
->order('fecha DESC');
|
||||
return $this->fetchMany($query, [$venta_id]);
|
||||
}
|
||||
public function fetchByVentaAndFechaAndTexto(int $venta_id, DateTimeImmutable $fecha, string $texto): Model\Venta\Comentario
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('venta = ? AND fecha = ? AND texto = ?');
|
||||
return $this->fetchOne($query, [$venta_id, $fecha->format('Y-m-d'), $texto]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user