100 lines
2.5 KiB
PHP
100 lines
2.5 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|