Initial commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\MoneyPlanner\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
|
||||
use OCA\MoneyPlanner\Db\Account;
|
||||
use OCA\MoneyPlanner\Db\AccountMapper;
|
||||
|
||||
|
||||
class AccountService {
|
||||
|
||||
private $mapper;
|
||||
|
||||
public function __construct(AccountMapper $mapper){
|
||||
$this->mapper = $mapper;
|
||||
}
|
||||
|
||||
public function findAll(string $userId) {
|
||||
return $this->mapper->findAll($userId);
|
||||
}
|
||||
|
||||
private function handleException ($e) {
|
||||
if ($e instanceof DoesNotExistException ||
|
||||
$e instanceof MultipleObjectsReturnedException) {
|
||||
throw new NotFoundException($e->getMessage());
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function find(int $id, string $userId) {
|
||||
try {
|
||||
return $this->mapper->find($id, $userId);
|
||||
|
||||
// in order to be able to plug in different storage backends like files
|
||||
// for instance it is a good idea to turn storage related exceptions
|
||||
// into service related exceptions so controllers and service users
|
||||
// have to deal with only one type of exception
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function create(string $name, string $userId) {
|
||||
$account = new Account();
|
||||
$account->setName($name);
|
||||
$account->setUserId($userId);
|
||||
return $this->mapper->insert($account);
|
||||
}
|
||||
|
||||
public function update(int $id, string $name, string $userId) {
|
||||
try {
|
||||
$account = $this->mapper->find($id, $userId);
|
||||
$account->setName($name);
|
||||
return $this->mapper->update($account);
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(int $id, string $userId) {
|
||||
try {
|
||||
$account = $this->mapper->find($id, $userId);
|
||||
$this->mapper->delete($account);
|
||||
return $account;
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\MoneyPlanner\Service;
|
||||
|
||||
class NotFoundException extends ServiceException {}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\MoneyPlanner\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ServiceException extends Exception {}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
namespace OCA\MoneyPlanner\Service;
|
||||
|
||||
use Exception;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
use OCA\MoneyPlanner\Db\Transaction;
|
||||
use OCA\MoneyPlanner\Db\TransactionMapper;
|
||||
|
||||
class TransactionService {
|
||||
private $mapper;
|
||||
|
||||
public function __construct(TransactionMapper $mapper){
|
||||
$this->mapper = $mapper;
|
||||
}
|
||||
|
||||
public function findAll(string $userId) {
|
||||
return $this->mapper->findAll($userId);
|
||||
}
|
||||
|
||||
private function handleException($e) {
|
||||
if ($e instanceof DoesNotExistException ||
|
||||
$e instanceof MultipleObjectsReturnedException) {
|
||||
throw new NotFoundException($e->getMessage());
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function find(int $id, string $userId) {
|
||||
try {
|
||||
return $this->mapper->find($id, $userId);
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function create(
|
||||
$accountFrom,
|
||||
$accountTo,
|
||||
int $group,
|
||||
int $category,
|
||||
string $balance,
|
||||
string $date,
|
||||
$freqNum,
|
||||
string $freqType,
|
||||
string $userId
|
||||
) {
|
||||
$transaction = new Transaction();
|
||||
$transaction->setAccountFrom($accountFrom);
|
||||
$transaction->setAccountTo($accountTo);
|
||||
$transaction->setGroup($group);
|
||||
$transaction->setCategory($category);
|
||||
$transaction->setBalance($balance);
|
||||
$transaction->setDate($date);
|
||||
$transaction->setFreqNum($freqNum);
|
||||
$transaction->setFreqType($freqType);
|
||||
$transaction->setUserId($userId);
|
||||
return $this->mapper->insert($transaction);
|
||||
}
|
||||
|
||||
public function update(
|
||||
int $id,
|
||||
$accountFrom,
|
||||
$accountTo,
|
||||
int $group,
|
||||
int $category,
|
||||
string $balance,
|
||||
string $date,
|
||||
$freqNum,
|
||||
string $freqType,
|
||||
string $userId
|
||||
) {
|
||||
try {
|
||||
$transaction = $this->mapper->find($id, $userId);
|
||||
$transaction->setAccountFrom($accountFrom);
|
||||
$transaction->setAccountTo($accountTo);
|
||||
$transaction->setGroup($group);
|
||||
$transaction->setCategory($category);
|
||||
$transaction->setBalance($balance);
|
||||
$transaction->setDate($date);
|
||||
$transaction->setFreqNum($freqNum);
|
||||
$transaction->setFreqType($freqType);
|
||||
return $this->mapper->update($transaction);
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(int $id, string $userId) {
|
||||
try {
|
||||
$transaction = $this->mapper->find($id, $userId);
|
||||
$this->mapper->delete($transaction);
|
||||
return $transaction;
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user