Agregar, editar y borrar comentarios
This commit is contained in:
@ -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