Is now capable of updating transactions over the API.

This commit is contained in:
James Cole
2019-04-06 08:10:50 +02:00
parent b692cccdfb
commit c519b4d0df
36 changed files with 1840 additions and 709 deletions

View File

@@ -220,25 +220,16 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* @param Budget|null $budget
* @param int|null $budgetId
* @param string|null $budgetName
*
* @return Budget|null
*/
public function findBudget(?Budget $budget, ?int $budgetId, ?string $budgetName): ?Budget
public function findBudget(?int $budgetId, ?string $budgetName): ?Budget
{
Log::debug('Now in findBudget()');
$result = null;
if (null !== $budget) {
Log::debug(sprintf('Parameters contain budget #%d, will return this.', $budget->id));
$result = $budget;
}
if (null === $result) {
Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId));
$result = $this->findNull((int)$budgetId);
}
Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId));
$result = $this->findNull((int)$budgetId);
if (null === $result) {
Log::debug(sprintf('Searching for budget with name %s...', $budgetName));
$result = $this->findByName((string)$budgetName);

View File

@@ -82,13 +82,12 @@ interface BudgetRepositoryInterface
public function destroyBudgetLimit(BudgetLimit $budgetLimit): void;
/**
* @param Budget|null $budget
* @param int|null $budgetId
* @param string|null $budgetName
*
* @return Budget|null
*/
public function findBudget(?Budget $budget, ?int $budgetId, ?string $budgetName): ?Budget;
public function findBudget( ?int $budgetId, ?string $budgetName): ?Budget;
/**
* Find budget by name.

View File

@@ -253,25 +253,16 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category|null $category
* @param int|null $categoryId
* @param string|null $categoryName
* @param int|null $categoryId
* @param string|null $categoryName
*
* @return Category|null
*/
public function findCategory(?Category $category, ?int $categoryId, ?string $categoryName): ?Category
public function findCategory(?int $categoryId, ?string $categoryName): ?Category
{
Log::debug('Now in findCategory()');
$result = null;
if (null !== $category) {
Log::debug(sprintf('Parameters contain category #%d, will return this.', $category->id));
$result = $category;
}
if (null === $result) {
Log::debug(sprintf('Searching for category with ID #%d...', $categoryId));
$result = $this->findNull((int)$categoryId);
}
Log::debug(sprintf('Searching for category with ID #%d...', $categoryId));
$result = $this->findNull((int)$categoryId);
if (null === $result) {
Log::debug(sprintf('Searching for category with name %s...', $categoryName));
$result = $this->findByName((string)$categoryName);

View File

@@ -34,13 +34,12 @@ interface CategoryRepositoryInterface
{
/**
* @param Category|null $category
* @param int|null $categoryId
* @param string|null $categoryName
*
* @return Category|null
*/
public function findCategory(?Category $category, ?int $categoryId, ?string $categoryName): ?Category;
public function findCategory( ?int $categoryId, ?string $categoryName): ?Category;
/**
* @param Category $category

View File

@@ -265,16 +265,13 @@ class CurrencyRepository implements CurrencyRepositoryInterface
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency
{
Log::debug('Now in findCurrency()');
$result = $this->find((int)$currencyId);
if (null === $result) {
Log::debug(sprintf('Searching for currency with code %s...', $currencyCode));
$result = $this->findByCode((string)$currencyCode);
}
$result = $this->findCurrencyNull($currencyId, $currencyCode);
if (null === $result) {
Log::debug('Grabbing default currency for this user...');
$result = app('amount')->getDefaultCurrencyByUser($this->user);
}
if (null === $result) {
Log::debug('Grabbing EUR as fallback.');
$result = $this->findByCode('EUR');
@@ -288,6 +285,30 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $result;
}
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
Log::debug('Now in findCurrencyNull()');
$result = $this->find((int)$currencyId);
if (null === $result) {
Log::debug(sprintf('Searching for currency with code %s...', $currencyCode));
$result = $this->findByCode((string)$currencyCode);
}
if (null !== $result && false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code));
$this->enable($result);
}
return $result;
}
/**
* Find by ID, return NULL if not found.
* Used in Import Currency!

View File

@@ -135,13 +135,23 @@ interface CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency;
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency;
/**
* Find by ID, return NULL if not found.
*

View File

@@ -774,40 +774,6 @@ class JournalRepository implements JournalRepositoryInterface
$this->user = $user;
}
/**
* @param array $data
*
* @return TransactionGroup
*
* @throws FireflyException
*/
public function store(array $data): TransactionGroup
{
/** @var TransactionGroupFactory $factory */
$factory = app(TransactionGroupFactory::class);
$factory->setUser($this->user);
return $factory->create($data);
}
/**
* @param TransactionGroup $journal
* @param array $data
*
* @return TransactionGroup
*
* @throws FireflyException
* @throws FireflyException
*/
public function update(TransactionGroup $journal, array $data): TransactionGroup
{
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
$journal = $service->update($journal, $data);
return $journal;
}
/**
* Update budget for a journal.
*

View File

@@ -319,21 +319,7 @@ interface JournalRepositoryInterface
*/
public function setUser(User $user);
/**
* @param array $data
*
* @throws FireflyException
* @return TransactionJournal
*/
public function store(array $data): TransactionGroup;
/**
* @param TransactionGroup $transactionGroup
* @param array $data
*
* @return TransactionGroup
*/
public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup;
/**
* Update budget for a journal.

View File

@@ -27,8 +27,12 @@ namespace FireflyIII\Repositories\TransactionGroup;
use Carbon\Carbon;
use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\TransactionGroupFactory;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Update\GroupUpdateService;
use FireflyIII\Support\NullArrayObject;
/**
@@ -36,6 +40,8 @@ use FireflyIII\Support\NullArrayObject;
*/
class TransactionGroupRepository implements TransactionGroupRepositoryInterface
{
private $user;
/**
* Constructor.
*/
@@ -61,6 +67,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
::table('journal_meta')
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->whereNull('deleted_at')
->get(['name', 'data']);
$return = [];
@@ -85,6 +92,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
::table('journal_meta')
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->whereNull('deleted_at')
->get(['name', 'data']);
$return = [];
@@ -133,4 +141,45 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
return $result->pluck('tag')->toArray();
}
/**
* @param mixed $user
*/
public function setUser($user): void
{
$this->user = $user;
}
/**
* @param array $data
*
* @return TransactionGroup
*
* @throws FireflyException
*/
public function store(array $data): TransactionGroup
{
/** @var TransactionGroupFactory $factory */
$factory = app(TransactionGroupFactory::class);
$factory->setUser($this->user);
return $factory->create($data);
}
/**
* @param TransactionGroup $transactionGroup
* @param array $data
*
* @return TransactionGroup
*
* @throws FireflyException
*/
public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup
{
/** @var GroupUpdateService $service */
$service = app(GroupUpdateService::class);
$updatedGroup = $service->update($transactionGroup, $data);
return $updatedGroup;
}
}

View File

@@ -23,7 +23,10 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\TransactionGroup;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Support\NullArrayObject;
use FireflyIII\User;
/**
* Interface TransactionGroupRepositoryInterface
@@ -67,4 +70,32 @@ interface TransactionGroupRepositoryInterface
* @return array
*/
public function getTags(int $journalId): array;
/**
* Set the user.
*
* @param User $user
*/
public function setUser(User $user): void;
/**
* Create a new transaction group.
*
* @param array $data
*
* @return TransactionGroup
* @throws FireflyException
*/
public function store(array $data): TransactionGroup;
/**
* Update an existing transaction group.
*
* @param TransactionGroup $transactionGroup
* @param array $data
*
* @return TransactionGroup
*/
public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup;
}

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\TransactionType;
use FireflyIII\Models\TransactionType;
use Log;
/**
* Class TransactionTypeRepository
*/
@@ -57,7 +58,8 @@ class TransactionTypeRepository implements TransactionTypeRepositoryInterface
return $type;
}
$search = $this->findByType($typeString);
$typeString = $typeString ?? TransactionType::WITHDRAWAL;
$search = $this->findByType($typeString);
if (null === $search) {
$search = $this->findByType(TransactionType::WITHDRAWAL);
}