Manejo de excepciones

This commit is contained in:
Juan Pablo Vial
2025-04-22 13:07:31 -04:00
parent ed96f25475
commit 5147450ed6
5 changed files with 105 additions and 26 deletions

View File

@ -268,7 +268,7 @@
}))
})
return Promise.all(promises)
}
},
}
},
draw() {

View File

@ -4,9 +4,11 @@ namespace Incoviba\Controller\API\Ventas;
use PDOException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Controller\API\{withJson, emptyBody};
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis};
use Incoviba\Exception\ServiceAction\{Read, Create, Update};
use Incoviba\Repository;
use Incoviba\Service;
@ -14,7 +16,9 @@ class PropiedadesUnidades
{
use emptyBody, withJson, withRedis;
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
public function add(ServerRequestInterface $request, ResponseInterface $response,
LoggerInterface $logger,
Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
@ -26,8 +30,8 @@ class PropiedadesUnidades
$pu = $propiedadUnidadService->add($body);
$output['propiedad_unidad'] = $pu;
$output['added'] = true;
} catch (EmptyResult $exception) {
error_log($exception);
} catch (Read | Create $exception) {
$logger->notice($exception);
}
return $this->withJson($response, $output);
}
@ -44,7 +48,7 @@ class PropiedadesUnidades
$pu = $propiedadUnidadService->getById($pu_id);
$propiedadUnidadService->edit($pu, (array) $body);
$output['edited'] = true;
} catch (PDOException | EmptyResult) {}
} catch (PDOException | Read | EmptyResult | Update) {}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
@ -58,7 +62,7 @@ class PropiedadesUnidades
$pu = $propiedadUnidadRepository->fetchById($pu_id);
$propiedadUnidadRepository->remove($pu);
$output['removed'] = true;
} catch (EmptyResult) {}
} catch (PDOException | EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -1,6 +1,8 @@
<?php
namespace Incoviba\Repository\Venta;
use PDO;
use PDOException;
use Incoviba\Common\Ideal;
use Incoviba\Common\Define;
use Incoviba\Common\Implement;
@ -17,14 +19,7 @@ class Propiedad extends Ideal\Repository
public function create(?array $data = null): Model\Venta\Propiedad
{
$map = (new Implement\Repository\MapperParser())
->register('unidad_principal', (new Implement\Repository\Mapper())
->setProperty('unidades')
->setDefault([])
->setFunction(function($data) {
return [$this->unidadService->getById($data['unidad_principal'])];
})
)
$map = (new Implement\Repository\MapperParser(['unidad_principal', 'estacionamientos', 'bodegas', 'estado']))
->register('estado', new Implement\Repository\Mapper\Boolean('estado'));
return $this->parseData(new Model\Venta\Propiedad(), $data, $map);
}
@ -67,7 +62,10 @@ class Propiedad extends Ideal\Repository
*/
public function fetchVigenteByUnidad(int $unidad_id): Model\Venta\Propiedad
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `unidad_principal` = ?";
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('`unidad_principal` = ? AND `estado` = 1');
return $this->fetchOne($query, [$unidad_id]);
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Repository\Venta;
use PDOException;
use Incoviba\Common\Ideal;
use Incoviba\Common\Define;
use Incoviba\Common\Implement;
@ -57,7 +58,12 @@ class PropiedadUnidad extends Ideal\Repository
return $this->update($model, ['propiedad', 'unidad', 'valor'], $new_data);
}
public function fetchById(int $id): Define\Model
/**
* @param int $id
* @return Model\Venta\PropiedadUnidad
* @throws Implement\Exception\EmptyResult
*/
public function fetchById(int $id): Model\Venta\PropiedadUnidad
{
$query = $this->connection->getQueryBuilder()
->select()
@ -66,16 +72,27 @@ class PropiedadUnidad extends Ideal\Repository
return $this->fetchOne($query, [$id]);
}
/**
* @param int $venta_id
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchByVenta(int $venta_id): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN unidad ON a.unidad = unidad.id
JOIN venta ON venta.propiedad = a.propiedad')
->joined('INNER JOIN unidad ON a.unidad = unidad.id')
->joined('INNER JOIN venta ON venta.propiedad = a.propiedad')
->where('venta.id = ?');
return $this->fetchMany($query, [$venta_id]);
}
/**
* @param int $propiedad_id
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchByPropiedad(int $propiedad_id): array
{
$query = $this->connection->getQueryBuilder()
@ -86,6 +103,12 @@ class PropiedadUnidad extends Ideal\Repository
->group('`unidad`.`id`');
return $this->fetchMany($query, [$propiedad_id]);
}
/**
* @param Define\Model $model
* @return void
* @throws PDOException
*/
public function remove(Define\Model $model): void
{
$query = $this->connection->getQueryBuilder()
@ -94,7 +117,15 @@ class PropiedadUnidad extends Ideal\Repository
$this->connection->execute($query, [$model->pu_id]);
}
protected function update(Define\Model $model, array $columns, array $data): Define\Model
/**
* @param Define\Model $model
* @param array $columns
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws Implement\Exception\EmptyResult
* @throws PDOException
*/
protected function update(Define\Model $model, array $columns, array $data): Model\Venta\PropiedadUnidad
{
$changes = [];
$values = [];

View File

@ -1,7 +1,9 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Exception\ServiceAction\Read;
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
use Incoviba\Repository;
use Incoviba\Model;
@ -11,21 +13,49 @@ class PropiedadUnidad
protected Repository\Venta\Unidad $unidadRepository,
protected Precio $precioService) {}
/**
* @param int $unidad_id
* @return Model\Venta\PropiedadUnidad
* @throws Read
*/
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
{
try {
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
public function getByVenta(int $venta_id): array
{
try {
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
} catch (EmptyResult) {
return [];
}
}
public function getByPropiedad(int $propiedad_id): array
{
try {
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
} catch (EmptyResult) {
return [];
}
}
/**
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws Create
* @throws Read
*/
public function add(array $data): Model\Venta\PropiedadUnidad
{
try {
$unidad = $this->unidadRepository->fetchById($data['unidad']);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
$temp = json_decode(json_encode($unidad), JSON_OBJECT_AS_ARRAY);
$columnMap = [
'proyecto_tipo_unidad' => 'pt'
@ -38,11 +68,27 @@ class PropiedadUnidad
$temp['propiedad'] = $data['propiedad'];
$temp['valor'] = $data['valor'];
$pu = $this->propiedadUnidadRepository->create($temp);
try {
return $this->process($this->propiedadUnidadRepository->save($pu));
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
/**
* @param Model\Venta\PropiedadUnidad $propiedadUnidad
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws EmptyResult
* @throws Update
*/
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
{
try {
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));
} catch (PDOException $exception) {
throw new Update(__CLASS__, $exception);
}
}
protected function process(Model\Venta\PropiedadUnidad $unidad): Model\Venta\PropiedadUnidad