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) return Promise.all(promises)
} },
} }
}, },
draw() { draw() {

View File

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

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Incoviba\Repository\Venta; namespace Incoviba\Repository\Venta;
use PDO;
use PDOException;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Define; use Incoviba\Common\Define;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
@ -17,14 +19,7 @@ class Propiedad extends Ideal\Repository
public function create(?array $data = null): Model\Venta\Propiedad public function create(?array $data = null): Model\Venta\Propiedad
{ {
$map = (new Implement\Repository\MapperParser()) $map = (new Implement\Repository\MapperParser(['unidad_principal', 'estacionamientos', 'bodegas', 'estado']))
->register('unidad_principal', (new Implement\Repository\Mapper())
->setProperty('unidades')
->setDefault([])
->setFunction(function($data) {
return [$this->unidadService->getById($data['unidad_principal'])];
})
)
->register('estado', new Implement\Repository\Mapper\Boolean('estado')); ->register('estado', new Implement\Repository\Mapper\Boolean('estado'));
return $this->parseData(new Model\Venta\Propiedad(), $data, $map); 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 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]); return $this->fetchOne($query, [$unidad_id]);
} }
} }

View File

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

View File

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