diff --git a/app/src/Service/Proyecto/Broker/Contract.php b/app/src/Service/Proyecto/Broker/Contract.php new file mode 100644 index 0000000..3d34307 --- /dev/null +++ b/app/src/Service/Proyecto/Broker/Contract.php @@ -0,0 +1,85 @@ +contractRepository->fetchAll(); + } catch (Implement\Exception\EmptyResult) { + return []; + } + } + + /** + * @throws ServiceAction\Read + */ + public function getById(int $id): Model\Proyecto\Broker\Contract + { + try { + return $this->contractRepository->fetchById($id); + } catch (Implement\Exception\EmptyResult $exception) { + throw new ServiceAction\Read(__CLASS__, $exception); + } + } + + /** + * @throws ServiceAction\Create + */ + public function add(array $data): Model\Proyecto\Broker\Contract + { + try { + return $this->contractRepository->fetchActiveByProjectAndBroker($data['proyecto_id'], $data['broker_rut']); + } catch (Implement\Exception\EmptyResult) {} + + try { + $filteredData = $this->contractRepository->filterData($data); + $contract = $this->contractRepository->create($filteredData); + return $this->contractRepository->save($contract); + } catch (Implement\Exception\EmptyResult $exception) { + throw new ServiceAction\Create(__CLASS__, $exception); + } + } + + /** + * @throws Update + */ + public function edit(Model\Proyecto\Broker\Contract $contract, array $data): Model\Proyecto\Broker\Contract + { + try { + $filteredData = $this->contractRepository->filterData($data); + return $this->contractRepository->edit($contract, $filteredData); + } catch (PDOException | Implement\Exception\EmptyResult) { + throw new ServiceAction\Update(__CLASS__); + } + } + + /** + * @throws ServiceAction\Delete + */ + public function delete(int $id): Model\Proyecto\Broker\Contract + { + try { + $contract = $this->contractRepository->fetchById($id); + $this->contractRepository->remove($contract->id); + return $contract; + } catch (PDOException | Implement\Exception\EmptyResult $exception) { + throw new ServiceAction\Delete(__CLASS__, $exception); + } + } +}