77 lines
1.9 KiB
PHP
77 lines
1.9 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
?>
|