Editar propiedad en venta

This commit is contained in:
2023-12-01 15:00:25 -03:00
parent af9c6c51d4
commit 57579a52f1
6 changed files with 127 additions and 8 deletions

View File

@ -7,12 +7,30 @@ use Psr\Http\Message\ResponseInterface;
use Incoviba\Controller\API\{withJson, emptyBody};
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis};
use Incoviba\Repository;
use Incoviba\Service;
class PropiedadesUnidades
{
use emptyBody, withJson, withRedis;
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'propiedad_unidad' => null,
'added' => false
];
try {
$pu = $propiedadUnidadService->add($body);
$output['propiedad_unidad'] = $pu;
$output['added'] = true;
} catch (EmptyResult $exception) {
error_log($exception);
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\PropiedadUnidad $propiedadUnidadService, int $pu_id): ResponseInterface
{
@ -29,4 +47,18 @@ class PropiedadesUnidades
} catch (PDOException | EmptyResult) {}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, int $pu_id): ResponseInterface
{
$output = [
'propiedad_unidad_id' => $pu_id,
'removed' => false
];
try {
$pu = $propiedadUnidadRepository->fetchById($pu_id);
$propiedadUnidadRepository->remove($pu);
$output['removed'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -10,6 +10,8 @@ class PropiedadUnidad extends Unidad
public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(), [
'pu_id' => $this->pu_id,
'propiedad_id' => $this->propiedad_id,
'valor' => $this->valor
]);
}

View File

@ -86,6 +86,13 @@ class PropiedadUnidad extends Ideal\Repository
->group('`unidad`.`id`');
return $this->fetchMany($query, [$propiedad_id]);
}
public function remove(Define\Model $model): void
{
$query = $this->connection->getQueryBuilder()
->delete()->from($this->getTable())
->where("id = ?");
$this->connection->execute($query, [$model->pu_id]);
}
protected function update(Define\Model $model, array $columns, array $data): Define\Model
{

View File

@ -8,7 +8,9 @@ use Incoviba\Model;
class PropiedadUnidad
{
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, protected Precio $precioService) {}
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository,
protected Repository\Venta\Unidad $unidadRepository,
protected Precio $precioService) {}
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
{
@ -22,6 +24,23 @@ class PropiedadUnidad
{
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
}
public function add(array $data): Model\Venta\PropiedadUnidad
{
$unidad = $this->unidadRepository->fetchById($data['unidad']);
$temp = json_decode(json_encode($unidad), JSON_OBJECT_AS_ARRAY);
$columnMap = [
'proyecto_tipo_unidad' => 'pt'
];
foreach ($temp as $key => $value) {
if (isset($columnMap[$key])) {
$temp[$columnMap[$key]] = $value['id'];
}
}
$temp['propiedad'] = $data['propiedad'];
$temp['valor'] = $data['valor'];
$pu = $this->propiedadUnidadRepository->create($temp);
return $this->process($this->propiedadUnidadRepository->save($pu));
}
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
{
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));