From 30d653faee84c5466c0a24f313644068335c16b4 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 5 Mar 2021 07:03:28 +0100 Subject: [PATCH] Updated API code. --- .../Autocomplete/AccountController.php | 2 +- .../Autocomplete/BillController.php | 2 +- .../Autocomplete/BudgetController.php | 2 +- .../Autocomplete/CategoryController.php | 2 +- .../Autocomplete/CurrencyController.php | 4 +- .../Autocomplete/ObjectGroupController.php | 2 +- .../Autocomplete/PiggyBankController.php | 4 +- .../Autocomplete/RecurrenceController.php | 45 +++++++ .../Autocomplete/RuleController.php | 45 +++++++ .../Autocomplete/RuleGroupController.php | 44 +++++++ .../Autocomplete/TagController.php | 2 +- .../Autocomplete/TransactionController.php | 8 +- .../TransactionTypeController.php | 2 +- .../Controllers/Chart/AccountController.php | 2 +- .../Insight/Expense/AccountController.php | 59 +++++++++ .../Insight/Expense/BudgetController.php | 95 +++++++++++++++ .../Insight/Expense/PeriodController.php | 85 +++++++++++++ .../Recurring/RecurringRepository.php | 25 +++- .../RecurringRepositoryInterface.php | 8 ++ app/Repositories/Rule/RuleRepository.php | 15 +++ .../Rule/RuleRepositoryInterface.php | 8 ++ .../RuleGroup/RuleGroupRepository.php | 15 +++ .../RuleGroupRepositoryInterface.php | 10 +- routes/api.php | 113 +++++++++--------- 24 files changed, 521 insertions(+), 78 deletions(-) create mode 100644 app/Api/V1/Controllers/Insight/Expense/BudgetController.php create mode 100644 app/Api/V1/Controllers/Insight/Expense/PeriodController.php diff --git a/app/Api/V1/Controllers/Autocomplete/AccountController.php b/app/Api/V1/Controllers/Autocomplete/AccountController.php index 9dfec18862..1121928794 100644 --- a/app/Api/V1/Controllers/Autocomplete/AccountController.php +++ b/app/Api/V1/Controllers/Autocomplete/AccountController.php @@ -90,7 +90,7 @@ class AccountController extends Controller } $return[] = [ - 'id' => $account->id, + 'id' => (string)$account->id, 'name' => $account->name, 'name_with_balance' => $nameWithBalance, 'type' => $account->accountType->type, diff --git a/app/Api/V1/Controllers/Autocomplete/BillController.php b/app/Api/V1/Controllers/Autocomplete/BillController.php index 890996dd2b..2038883f2e 100644 --- a/app/Api/V1/Controllers/Autocomplete/BillController.php +++ b/app/Api/V1/Controllers/Autocomplete/BillController.php @@ -69,7 +69,7 @@ class BillController extends Controller $filtered = $result->map( static function (Bill $item) { return [ - 'id' => $item->id, + 'id' => (string)$item->id, 'name' => $item->name, ]; } diff --git a/app/Api/V1/Controllers/Autocomplete/BudgetController.php b/app/Api/V1/Controllers/Autocomplete/BudgetController.php index 066a1b2c15..616989d2f2 100644 --- a/app/Api/V1/Controllers/Autocomplete/BudgetController.php +++ b/app/Api/V1/Controllers/Autocomplete/BudgetController.php @@ -69,7 +69,7 @@ class BudgetController extends Controller $filtered = $result->map( static function (Budget $item) { return [ - 'id' => $item->id, + 'id' => (string)$item->id, 'name' => $item->name, ]; } diff --git a/app/Api/V1/Controllers/Autocomplete/CategoryController.php b/app/Api/V1/Controllers/Autocomplete/CategoryController.php index 878defc648..ab05ebf504 100644 --- a/app/Api/V1/Controllers/Autocomplete/CategoryController.php +++ b/app/Api/V1/Controllers/Autocomplete/CategoryController.php @@ -68,7 +68,7 @@ class CategoryController extends Controller $filtered = $result->map( static function (Category $item) { return [ - 'id' => $item->id, + 'id' => (string)$item->id, 'name' => $item->name, ]; } diff --git a/app/Api/V1/Controllers/Autocomplete/CurrencyController.php b/app/Api/V1/Controllers/Autocomplete/CurrencyController.php index 5f17ca3bb2..fe6cc5648f 100644 --- a/app/Api/V1/Controllers/Autocomplete/CurrencyController.php +++ b/app/Api/V1/Controllers/Autocomplete/CurrencyController.php @@ -71,7 +71,7 @@ class CurrencyController extends Controller /** @var TransactionCurrency $currency */ foreach ($collection as $currency) { $result[] = [ - 'id' => $currency->id, + 'id' => (string) $currency->id, 'name' => sprintf('%s (%s)', $currency->name, $currency->code), 'code' => $currency->code, 'symbol' => $currency->symbol, @@ -96,7 +96,7 @@ class CurrencyController extends Controller /** @var TransactionCurrency $currency */ foreach ($collection as $currency) { $result[] = [ - 'id' => $currency->id, + 'id' => (string) $currency->id, 'name' => $currency->name, 'code' => $currency->code, 'symbol' => $currency->symbol, diff --git a/app/Api/V1/Controllers/Autocomplete/ObjectGroupController.php b/app/Api/V1/Controllers/Autocomplete/ObjectGroupController.php index 4ab1885c4f..bfa257dca4 100644 --- a/app/Api/V1/Controllers/Autocomplete/ObjectGroupController.php +++ b/app/Api/V1/Controllers/Autocomplete/ObjectGroupController.php @@ -71,7 +71,7 @@ class ObjectGroupController extends Controller /** @var ObjectGroup $account */ foreach ($result as $objectGroup) { $return[] = [ - 'id' => $objectGroup->id, + 'id' => (string)$objectGroup->id, 'name' => $objectGroup->title, 'title' => $objectGroup->title, ]; diff --git a/app/Api/V1/Controllers/Autocomplete/PiggyBankController.php b/app/Api/V1/Controllers/Autocomplete/PiggyBankController.php index fc2c682a91..c1499c9f25 100644 --- a/app/Api/V1/Controllers/Autocomplete/PiggyBankController.php +++ b/app/Api/V1/Controllers/Autocomplete/PiggyBankController.php @@ -78,7 +78,7 @@ class PiggyBankController extends Controller foreach ($piggies as $piggy) { $currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency; $response[] = [ - 'id' => $piggy->id, + 'id' => (string)$piggy->id, 'name' => $piggy->name, 'currency_id' => $currency->id, 'currency_name' => $currency->name, @@ -106,7 +106,7 @@ class PiggyBankController extends Controller $currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency; $currentAmount = $this->piggyRepository->getRepetition($piggy)->currentamount ?? '0'; $response[] = [ - 'id' => $piggy->id, + 'id' => (string)$piggy->id, 'name' => $piggy->name, 'name_with_balance' => sprintf( '%s (%s / %s)', $piggy->name, app('amount')->formatAnything($currency, $currentAmount, false), diff --git a/app/Api/V1/Controllers/Autocomplete/RecurrenceController.php b/app/Api/V1/Controllers/Autocomplete/RecurrenceController.php index 75785eec08..01f2584a29 100644 --- a/app/Api/V1/Controllers/Autocomplete/RecurrenceController.php +++ b/app/Api/V1/Controllers/Autocomplete/RecurrenceController.php @@ -25,11 +25,56 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete; use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest; +use FireflyIII\Models\Rule; +use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; +use Illuminate\Http\JsonResponse; /** * Class RecurrenceController */ class RecurrenceController extends Controller { + private RecurringRepositoryInterface $repository; + + + /** + * RecurrenceController constructor. + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + $this->repository = app(RecurringRepositoryInterface::class); + $this->repository->setUser(auth()->user()); + + return $next($request); + } + ); + } + + /** + * @param AutocompleteRequest $request + * + * @return JsonResponse + */ + public function recurring(AutocompleteRequest $request): JsonResponse + { + $data = $request->getData(); + $rules = $this->repository->searchRecurrence($data['query'], $data['limit']); + $response = []; + + /** @var Rule $rule */ + foreach ($rules as $rule) { + $response[] = [ + 'id' => (string)$rule->id, + 'name' => $rule->title, + 'description' => $rule->description, + ]; + } + + return response()->json($response); + } } diff --git a/app/Api/V1/Controllers/Autocomplete/RuleController.php b/app/Api/V1/Controllers/Autocomplete/RuleController.php index fd7453d05a..cbd9022ed2 100644 --- a/app/Api/V1/Controllers/Autocomplete/RuleController.php +++ b/app/Api/V1/Controllers/Autocomplete/RuleController.php @@ -25,11 +25,56 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete; use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest; +use FireflyIII\Models\Rule; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use Illuminate\Http\JsonResponse; /** * Class RuleController */ class RuleController extends Controller { + private RuleRepositoryInterface $repository; + + + /** + * RuleController constructor. + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + $this->repository = app(RuleRepositoryInterface::class); + $this->repository->setUser(auth()->user()); + + return $next($request); + } + ); + } + + /** + * @param AutocompleteRequest $request + * + * @return JsonResponse + */ + public function rules(AutocompleteRequest $request): JsonResponse + { + $data = $request->getData(); + $rules = $this->repository->searchRule($data['query'], $data['limit']); + $response = []; + + /** @var Rule $rule */ + foreach ($rules as $rule) { + $response[] = [ + 'id' => (string)$rule->id, + 'name' => $rule->title, + 'description' => $rule->description, + ]; + } + + return response()->json($response); + } } diff --git a/app/Api/V1/Controllers/Autocomplete/RuleGroupController.php b/app/Api/V1/Controllers/Autocomplete/RuleGroupController.php index bbc598e40b..d67ae1c01c 100644 --- a/app/Api/V1/Controllers/Autocomplete/RuleGroupController.php +++ b/app/Api/V1/Controllers/Autocomplete/RuleGroupController.php @@ -25,11 +25,55 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete; use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest; +use FireflyIII\Models\RuleGroup; +use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; +use Illuminate\Http\JsonResponse; /** * Class RuleGroupController */ class RuleGroupController extends Controller { + private RuleGroupRepositoryInterface $repository; + + /** + * RuleGroupController constructor. + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + $this->repository = app(RuleGroupRepositoryInterface::class); + $this->repository->setUser(auth()->user()); + + return $next($request); + } + ); + } + + /** + * @param AutocompleteRequest $request + * + * @return JsonResponse + */ + public function ruleGroups(AutocompleteRequest $request): JsonResponse + { + $data = $request->getData(); + $groups = $this->repository->searchRuleGroup($data['query'], $data['limit']); + $response = []; + + /** @var RuleGroup $group */ + foreach ($groups as $group) { + $response[] = [ + 'id' => (string)$group->id, + 'name' => $group->title, + 'description' => $group->description, + ]; + } + + return response()->json($response); + } } diff --git a/app/Api/V1/Controllers/Autocomplete/TagController.php b/app/Api/V1/Controllers/Autocomplete/TagController.php index 5b2aeabc67..ef26cd12fe 100644 --- a/app/Api/V1/Controllers/Autocomplete/TagController.php +++ b/app/Api/V1/Controllers/Autocomplete/TagController.php @@ -71,7 +71,7 @@ class TagController extends Controller /** @var Tag $tag */ foreach ($result as $tag) { $array[] = [ - 'id' => $tag->id, + 'id' => (string)$tag->id, 'name' => $tag->tag, 'tag' => $tag->tag, ]; diff --git a/app/Api/V1/Controllers/Autocomplete/TransactionController.php b/app/Api/V1/Controllers/Autocomplete/TransactionController.php index 7a0a9b480b..680e72bba4 100644 --- a/app/Api/V1/Controllers/Autocomplete/TransactionController.php +++ b/app/Api/V1/Controllers/Autocomplete/TransactionController.php @@ -81,8 +81,8 @@ class TransactionController extends Controller /** @var TransactionJournal $journal */ foreach ($filtered as $journal) { $array[] = [ - 'id' => $journal->id, - 'transaction_group_id' => $journal->transaction_group_id, + 'id' => (string)$journal->id, + 'transaction_group_id' => (string)$journal->transaction_group_id, 'name' => $journal->description, 'description' => $journal->description, ]; @@ -120,8 +120,8 @@ class TransactionController extends Controller /** @var TransactionJournal $journal */ foreach ($result as $journal) { $array[] = [ - 'id' => $journal->id, - 'transaction_group_id' => $journal->transaction_group_id, + 'id' => (string)$journal->id, + 'transaction_group_id' => (string)$journal->transaction_group_id, 'name' => sprintf('#%d: %s', $journal->transaction_group_id, $journal->description), 'description' => sprintf('#%d: %s', $journal->transaction_group_id, $journal->description), ]; diff --git a/app/Api/V1/Controllers/Autocomplete/TransactionTypeController.php b/app/Api/V1/Controllers/Autocomplete/TransactionTypeController.php index 5dcee6c308..38faca2036 100644 --- a/app/Api/V1/Controllers/Autocomplete/TransactionTypeController.php +++ b/app/Api/V1/Controllers/Autocomplete/TransactionTypeController.php @@ -69,7 +69,7 @@ class TransactionTypeController extends Controller foreach ($types as $type) { // different key for consistency. $array[] = [ - 'id' => $type->id, + 'id' =>(string) $type->id, 'name' => $type->type, 'type' => $type->type, ]; diff --git a/app/Api/V1/Controllers/Chart/AccountController.php b/app/Api/V1/Controllers/Chart/AccountController.php index 283c7d3785..7547cbb248 100644 --- a/app/Api/V1/Controllers/Chart/AccountController.php +++ b/app/Api/V1/Controllers/Chart/AccountController.php @@ -193,7 +193,7 @@ class AccountController extends Controller } $currentSet = [ 'label' => $account->name, - 'currency_id' => $currency->id, + 'currency_id' => (string) $currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, diff --git a/app/Api/V1/Controllers/Insight/Expense/AccountController.php b/app/Api/V1/Controllers/Insight/Expense/AccountController.php index 16c937591f..214ea2cb26 100644 --- a/app/Api/V1/Controllers/Insight/Expense/AccountController.php +++ b/app/Api/V1/Controllers/Insight/Expense/AccountController.php @@ -143,6 +143,65 @@ class AccountController extends Controller } + // sort temp array by amount. + $amounts = array_column($tempData, 'difference_float'); + array_multisort($amounts, SORT_ASC, $tempData); + + return response()->json($tempData); + } + + /** + * @param DateRequest $request + * + * @return JsonResponse + */ + public function asset(DateRequest $request): JsonResponse + { + $dates = $request->getAll(); + /** @var Carbon $start */ + $start = $dates['start']; + /** @var Carbon $end */ + $end = $dates['end']; + + $start->subDay(); + + // prep some vars: + $currencies = []; + $tempData = []; + + // grab all accounts and names + $accounts = $this->repository->getAccountsByType([AccountType::ASSET]); + $accountNames = $this->extractNames($accounts); + $startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start); + $endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end); + + // loop the end balances. This is an array for each account ($expenses) + foreach ($endBalances as $accountId => $expenses) { + $accountId = (int)$accountId; + // loop each expense entry (each entry can be a different currency). + foreach ($expenses as $currencyId => $endAmount) { + $currencyId = (int)$currencyId; + + // see if there is an accompanying start amount. + // grab the difference and find the currency. + $startAmount = $startBalances[$accountId][$currencyId] ?? '0'; + $diff = bcsub($endAmount, $startAmount); + $currencies[$currencyId] = $currencies[$currencyId] ?? $this->currencyRepository->findNull($currencyId); + if (0 !== bccomp($diff, '0')) { + // store the values in a temporary array. + $tempData[] = [ + 'id' => $accountId, + 'name' => $accountNames[$accountId], + 'difference' => bcmul($diff, '-1'), + 'difference_float' => ((float)$diff) * -1, + 'currency_id' => (string) $currencyId, + 'currency_code' => $currencies[$currencyId]->code, + ]; + } + } + } + + // sort temp array by amount. $amounts = array_column($tempData, 'difference_float'); array_multisort($amounts, SORT_ASC, $tempData); diff --git a/app/Api/V1/Controllers/Insight/Expense/BudgetController.php b/app/Api/V1/Controllers/Insight/Expense/BudgetController.php new file mode 100644 index 0000000000..34a48645cb --- /dev/null +++ b/app/Api/V1/Controllers/Insight/Expense/BudgetController.php @@ -0,0 +1,95 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Insight\Expense; + + +use Carbon\Carbon; +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\DateRequest; +use FireflyIII\Models\Budget; +use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; +use Illuminate\Http\JsonResponse; +use Illuminate\Support\Collection; + +/** + * Class BudgetController + */ +class BudgetController extends Controller +{ + private OperationsRepositoryInterface $opsRepository; + private BudgetRepositoryInterface $repository; + + /** + * AccountController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + $this->opsRepository = app(OperationsRepositoryInterface::class); + $this->repository = app(BudgetRepositoryInterface::class); + $this->opsRepository->setUser(auth()->user()); + $this->repository->setUser(auth()->user()); + + return $next($request); + } + ); + } + + /** + * @param DateRequest $request + * + * @return JsonResponse + */ + public function budget(DateRequest $request): JsonResponse + { + $dates = $request->getAll(); + /** @var Carbon $start */ + $start = $dates['start']; + /** @var Carbon $end */ + $end = $dates['end']; + $result = []; + $budgets = $this->repository->getActiveBudgets(); + /** @var Budget $budget */ + foreach ($budgets as $budget) { + $expenses = $this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]), null); + /** @var array $expense */ + foreach ($expenses as $expense) { + $result[] = [ + 'id' => (string)$budget->id, + 'name' => $budget->name, + 'difference' => $expense['sum'], + 'difference_float' => (float)$expense['sum'], + 'currency_id' => (string)$expense['currency_id'], + 'currency_code' => $expense['currency_code'], + ]; + } + } + + return response()->json($result); + } + +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/Insight/Expense/PeriodController.php b/app/Api/V1/Controllers/Insight/Expense/PeriodController.php new file mode 100644 index 0000000000..30e2291266 --- /dev/null +++ b/app/Api/V1/Controllers/Insight/Expense/PeriodController.php @@ -0,0 +1,85 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Insight\Expense; + + +use Carbon\Carbon; +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\DateRequest; +use FireflyIII\Helpers\Collector\GroupCollectorInterface; +use FireflyIII\Models\TransactionType; +use Illuminate\Http\JsonResponse; + +/** + * Class PeriodController + */ +class PeriodController extends Controller +{ + + /** + * @param DateRequest $request + * + * @return JsonResponse + */ + public function total(DateRequest $request): JsonResponse + { + $dates = $request->getAll(); + /** @var Carbon $start */ + $start = $dates['start']; + /** @var Carbon $end */ + $end = $dates['end']; + $response = []; + + // collect all expenses in this period (regardless of type) + $collector = app(GroupCollectorInterface::class); + $collector->setTypes([TransactionType::WITHDRAWAL])->setRange($start, $end); + $genericSet = $collector->getExtractedJournals(); + foreach ($genericSet as $journal) { + $currencyId = (int)$journal['currency_id']; + $foreignCurrencyId = (int)$journal['foreign_currency_id']; + + if (0 !== $currencyId) { + $response[$currencyId] = $response[$currencyId] ?? [ + 'difference' => '0', + 'difference_float' => 0, + 'currency_id' => (string)$currencyId, + 'currency_code' => $journal['currency_code'], + ]; + $response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']); + $response[$currencyId]['difference_float'] = (float)$response[$currencyId]['difference']; + } + if (0 !== $foreignCurrencyId) { + $response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [ + 'difference' => '0', + 'difference_float' => 0, + 'currency_id' => (string)$foreignCurrencyId, + 'currency_code' => $journal['foreign_currency_code'], + ]; + $response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']); + $response[$foreignCurrencyId]['difference_float'] = (float)$response[$foreignCurrencyId]['difference']; + } + } + + return response()->json(array_values($response)); + } + +} \ No newline at end of file diff --git a/app/Repositories/Recurring/RecurringRepository.php b/app/Repositories/Recurring/RecurringRepository.php index 97671ed50e..4c7eed9c21 100644 --- a/app/Repositories/Recurring/RecurringRepository.php +++ b/app/Repositories/Recurring/RecurringRepository.php @@ -53,6 +53,7 @@ use Log; class RecurringRepository implements RecurringRepositoryInterface { use CalculateRangeOccurrences, CalculateXOccurrences, CalculateXOccurrencesSince, FiltersWeekends; + private User $user; @@ -522,6 +523,7 @@ class RecurringRepository implements RecurringRepositoryInterface // filter out everything if "repeat_until" is set. $repeatUntil = $repetition->recurrence->repeat_until; + return $this->filterMaxDate($repeatUntil, $occurrences); } @@ -560,16 +562,16 @@ class RecurringRepository implements RecurringRepositoryInterface public function totalTransactions(Recurrence $recurrence, RecurrenceRepetition $repetition): int { // if repeat = null just return 0. - if (null === $recurrence->repeat_until && 0 === (int) $recurrence->repetitions) { + if (null === $recurrence->repeat_until && 0 === (int)$recurrence->repetitions) { return 0; } // expect X transactions then stop. Return that number - if (null === $recurrence->repeat_until && 0 !== (int) $recurrence->repetitions) { - return (int) $recurrence->repetitions; + if (null === $recurrence->repeat_until && 0 !== (int)$recurrence->repetitions) { + return (int)$recurrence->repetitions; } // need to calculate, this depends on the repetition: - if (null !== $recurrence->repeat_until && 0 === (int) $recurrence->repetitions) { + if (null !== $recurrence->repeat_until && 0 === (int)$recurrence->repetitions) { $occurrences = $this->getOccurrencesInRange($repetition, $recurrence->first_date ?? today(), $recurrence->repeat_until); return count($occurrences); @@ -577,4 +579,19 @@ class RecurringRepository implements RecurringRepositoryInterface return 0; } + + /** + * @inheritDoc + */ + public function searchRecurrence(string $query, int $limit): Collection + { + $search = $this->user->recurrences(); + if ('' !== $query) { + $search->where('recurrences.title', 'LIKE', sprintf('%%%s%%', $query)); + } + $search + ->orderBy('recurrences.title', 'ASC'); + + return $search->take($limit)->get(['id', 'title', 'description']); + } } diff --git a/app/Repositories/Recurring/RecurringRepositoryInterface.php b/app/Repositories/Recurring/RecurringRepositoryInterface.php index ddca5c2b21..71996f203d 100644 --- a/app/Repositories/Recurring/RecurringRepositoryInterface.php +++ b/app/Repositories/Recurring/RecurringRepositoryInterface.php @@ -44,6 +44,14 @@ interface RecurringRepositoryInterface */ public function destroyAll(): void; + /** + * @param string $query + * @param int $limit + * + * @return Collection + */ + public function searchRecurrence(string $query, int $limit): Collection; + /** * Calculate how many transactions are to be expected from this recurrence. * diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index ac49dd5c41..dfd44c40c7 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -565,4 +565,19 @@ class RuleRepository implements RuleRepositoryInterface return implode(' ', $params); } + + /** + * @inheritDoc + */ + public function searchRule(string $query, int $limit): Collection + { + $search = $this->user->rules(); + if ('' !== $query) { + $search->where('rules.title', 'LIKE', sprintf('%%%s%%', $query)); + } + $search->orderBy('rules.order', 'ASC') + ->orderBy('rules.title', 'ASC'); + + return $search->take($limit)->get(['id','title','description']); + } } diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.php index d49199c4fc..2b59bfc297 100644 --- a/app/Repositories/Rule/RuleRepositoryInterface.php +++ b/app/Repositories/Rule/RuleRepositoryInterface.php @@ -39,6 +39,14 @@ interface RuleRepositoryInterface */ public function count(): int; + /** + * @param string $query + * @param int $limit + * + * @return Collection + */ + public function searchRule(string $query, int $limit): Collection; + /** * Return search query for rule. * diff --git a/app/Repositories/RuleGroup/RuleGroupRepository.php b/app/Repositories/RuleGroup/RuleGroupRepository.php index 421e01a531..b67da9bc7a 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepository.php +++ b/app/Repositories/RuleGroup/RuleGroupRepository.php @@ -388,4 +388,19 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface $group->delete(); } } + + /** + * @inheritDoc + */ + public function searchRuleGroup(string $query, int $limit): Collection + { + $search = $this->user->ruleGroups(); + if ('' !== $query) { + $search->where('rule_groups.title', 'LIKE', sprintf('%%%s%%', $query)); + } + $search->orderBy('rule_groups.order', 'ASC') + ->orderBy('rule_groups.title', 'ASC'); + + return $search->take($limit)->get(['id','title','description']); + } } diff --git a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php index 216fefdf57..8845b69e6e 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php +++ b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php @@ -36,6 +36,14 @@ interface RuleGroupRepositoryInterface */ public function destroyAll(): void; + /** + * @param string $query + * @param int $limit + * + * @return Collection + */ + public function searchRuleGroup(string $query, int $limit): Collection; + /** * @return int */ @@ -155,7 +163,7 @@ interface RuleGroupRepositoryInterface /** * @param RuleGroup $ruleGroup - * @param array $data + * @param array $data * * @return RuleGroup */ diff --git a/routes/api.php b/routes/api.php index 89b3bbd782..820281bf15 100644 --- a/routes/api.php +++ b/routes/api.php @@ -42,10 +42,9 @@ Route::group( Route::get('object-groups', ['uses' => 'ObjectGroupController@objectGroups', 'as' => 'object-groups']); Route::get('piggy-banks', ['uses' => 'PiggyBankController@piggyBanks', 'as' => 'piggy-banks']); Route::get('piggy-banks-with-balance', ['uses' => 'PiggyBankController@piggyBanksWithBalance', 'as' => 'piggy-banks-with-balance']); - // TODO create me: + Route::get('recurring', ['uses' => 'RecurrenceController@recurring', 'as' => 'recurring']); Route::get('rules', ['uses' => 'RuleController@rules', 'as' => 'rules']); - // TODO create me: - Route::get('rule-groups', ['uses' => 'RuleGroupController@rules', 'as' => 'rule-groups']); + Route::get('rule-groups', ['uses' => 'RuleGroupController@ruleGroups', 'as' => 'rule-groups']); Route::get('tags', ['uses' => 'TagController@tags', 'as' => 'tags']); Route::get('transactions', ['uses' => 'TransactionController@transactions', 'as' => 'transactions']); Route::get('transactions-with-id', ['uses' => 'TransactionController@transactionsWithID', 'as' => 'transactions-with-id']); @@ -85,6 +84,60 @@ Route::group( } ); +/** + * INSIGHTS ROUTES + */ + +// Insight in expenses: +Route::group( + ['namespace' => 'FireflyIII\Api\V1\Controllers\Insight\Expense', 'prefix' => 'insight/expense', + 'as' => 'api.v1.insight.expense.',], + static function () { + // Insight in expenses. + Route::get('expense', ['uses' => 'AccountController@expense', 'as' => 'expense']); + Route::get('asset', ['uses' => 'AccountController@asset', 'as' => 'asset']); + Route::get('total', ['uses' => 'PeriodController@total', 'as' => 'total']); + Route::get('budget', ['uses' => 'BudgetController@budget', 'as' => 'budget']); + + // TODO Budget/no budget and budget limit + Route::get('no-budget', ['uses' => 'BudgetController@noBudget', 'as' => 'no-budget']); + + // TODO category and no category + Route::get('category', ['uses' => 'CategoryController@category', 'as' => 'category']); + Route::get('no-category', ['uses' => 'CategoryController@noCategory', 'as' => 'no-category']); + + // TODO bill and no bill + Route::get('bill', ['uses' => 'BillController@bill', 'as' => 'bill']); + Route::get('no-bill', ['uses' => 'BillController@noBill', 'as' => 'no-bill']); + } +); + +// Insight in expenses: +Route::group( + ['namespace' => 'FireflyIII\Api\V1\Controllers\Insight\Income', 'prefix' => 'insight/income', + 'as' => 'api.v1.insight.income.',], + static function () { + // Insight in income + + // TODO grouped by expense account or asset account: + Route::get('revenue', ['uses' => 'AccountController@revenue', 'as' => 'expense']); + Route::get('asset', ['uses' => 'AccountController@asset', 'as' => 'asset']); + + // TODO total: + Route::get('total', ['uses' => 'PeriodController@total', 'as' => 'total']); + + // TODO category and no category + Route::get('category', ['uses' => 'CategoryController@category', 'as' => 'category']); + Route::get('no-category', ['uses' => 'CategoryController@noCategory', 'as' => 'no-category']); + + } +); + + + + + + @@ -135,60 +188,6 @@ Route::group( * INSIGHT CONTROLLERS */ -// Insight in expenses: -Route::group( - ['namespace' => 'FireflyIII\Api\V1\Controllers\Insight\Expense', 'prefix' => 'insight/expense', - 'as' => 'api.v1.insight.expense.',], - static function () { - // Insight in expenses. - - // TODO grouped by expense account or asset account: - Route::get('expense', ['uses' => 'AccountController@expense', 'as' => 'expense']); - Route::get('asset', ['uses' => 'AccountController@asset', 'as' => 'asset']); - - // TODO total: - Route::get('total', ['uses' => 'PeriodController@total', 'as' => 'total']); - - // TODO Budget/no budget and budget limit - Route::get('budget', ['uses' => 'BudgetController@budget', 'as' => 'budget']); - Route::get('no-budget', ['uses' => 'BudgetController@noBudget', 'as' => 'no-budget']); - - // TODO category and no category - Route::get('category', ['uses' => 'CategoryController@category', 'as' => 'category']); - Route::get('no-category', ['uses' => 'CategoryController@noCategory', 'as' => 'no-category']); - - // TODO bill and no bill - Route::get('bill', ['uses' => 'BillController@bill', 'as' => 'bill']); - Route::get('no-bill', ['uses' => 'BillController@noBill', 'as' => 'no-bill']); - } -); - -// Insight in expenses: -Route::group( - ['namespace' => 'FireflyIII\Api\V1\Controllers\Insight\Income', 'prefix' => 'insight/income', - 'as' => 'api.v1.insight.income.',], - static function () { - // Insight in income - - // TODO grouped by expense account or asset account: - Route::get('revenue', ['uses' => 'AccountController@revenue', 'as' => 'expense']); - Route::get('asset', ['uses' => 'AccountController@asset', 'as' => 'asset']); - - // TODO total: - Route::get('total', ['uses' => 'PeriodController@total', 'as' => 'total']); - - // TODO category and no category - Route::get('category', ['uses' => 'CategoryController@category', 'as' => 'category']); - Route::get('no-category', ['uses' => 'CategoryController@noCategory', 'as' => 'no-category']); - - } -); - - - - - -