Initial commit

This commit is contained in:
2026-07-05 21:52:31 +02:00
commit 93259f5b6d
35 changed files with 2389 additions and 0 deletions
+77
View File
@@ -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);
}
}
}
?>