Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Refactor API code.
James Cole2f1475340548
diff --git a/app/Api/V1/Controllers/Models/Account/DestroyController.php b/app/Api/V1/Controllers/Models/Account/DestroyController.phpindex aa8815c204..308d8b9669 100644--- a/app/Api/V1/Controllers/Models/Account/DestroyController.php+++ b/app/Api/V1/Controllers/Models/Account/DestroyController.php@@ -26,8 +26,6 @@ namespace FireflyIII\Api\V1\Controllers\Models\Account;use FireflyIII\Api\V1\Controllers\Controller;use FireflyIII\Models\Account;use FireflyIII\Repositories\Account\AccountRepositoryInterface;-use FireflyIII\Support\Http\Api\AccountFilter;-use FireflyIII\Support\Http\Api\TransactionFilter;use Illuminate\Http\JsonResponse;/**@@ -35,13 +33,10 @@ use Illuminate\Http\JsonResponse;*/class DestroyController extends Controller{- use AccountFilter, TransactionFilter;-public const RESOURCE_KEY = 'accounts';private AccountRepositoryInterface $repository;-/*** AccountController constructor.*diff --git a/app/Api/V1/Controllers/Models/Account/ListController.php b/app/Api/V1/Controllers/Models/Account/ListController.phpindex 139b8b8996..43d6343713 100644--- a/app/Api/V1/Controllers/Models/Account/ListController.php+++ b/app/Api/V1/Controllers/Models/Account/ListController.php@@ -27,9 +27,7 @@ use FireflyIII\Api\V1\Controllers\Controller;use FireflyIII\Helpers\Collector\GroupCollectorInterface;use FireflyIII\Models\Account;use FireflyIII\Repositories\Account\AccountRepositoryInterface;-use FireflyIII\Support\Http\Api\AccountFilter;use FireflyIII\Support\Http\Api\TransactionFilter;-use FireflyIII\Transformers\AccountTransformer;use FireflyIII\Transformers\AttachmentTransformer;use FireflyIII\Transformers\PiggyBankTransformer;use FireflyIII\Transformers\TransactionGroupTransformer;@@ -40,14 +38,13 @@ use Illuminate\Pagination\LengthAwarePaginator;use Illuminate\Support\Collection;use League\Fractal\Pagination\IlluminatePaginatorAdapter;use League\Fractal\Resource\Collection as FractalCollection;-use League\Fractal\Resource\Item;/*** Class ListController*/class ListController extends Controller{- use AccountFilter, TransactionFilter;+ use TransactionFilter;public const RESOURCE_KEY = 'accounts';@@ -100,43 +97,6 @@ class ListController extends Controllerreturn response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);}- /**- * Display a listing of the resource.- *- * @param Request $request- *- * @codeCoverageIgnore- * @return JsonResponse- */- public function index(Request $request): JsonResponse- {- $manager = $this->getManager();- $type = $request->get('type') ?? 'all';- $this->parameters->set('type', $type);-- // types to get, page size:- $types = $this->mapAccountTypes($this->parameters->get('type'));- $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;-- // get list of accounts. Count it and split it.- $collection = $this->repository->getAccountsByType($types);- $count = $collection->count();- $accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);-- // make paginator:- $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));- $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams());-- /** @var AccountTransformer $transformer */- $transformer = app(AccountTransformer::class);- $transformer->setParameters($this->parameters);-- $resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY);- $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-/*** List all piggies.*@@ -173,24 +133,6 @@ class ListController extends Controller}- /**- * Show single instance.- *- * @param Account $account- *- * @return JsonResponse- */- public function show(Account $account): JsonResponse- {- $manager = $this->getManager();-- /** @var AccountTransformer $transformer */- $transformer = app(AccountTransformer::class);- $transformer->setParameters($this->parameters);- $resource = new Item($account, $transformer, self::RESOURCE_KEY);-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }/*** Show all transaction groups related to the account.diff --git a/app/Api/V1/Controllers/Models/Account/ShowController.php b/app/Api/V1/Controllers/Models/Account/ShowController.phpnew file mode 100644index 0000000000..aa31be9ee5--- /dev/null+++ b/app/Api/V1/Controllers/Models/Account/ShowController.php@@ -0,0 +1,122 @@+<?php+/*+ * ShowController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Account;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Models\Account;+use FireflyIII\Repositories\Account\AccountRepositoryInterface;+use FireflyIII\Support\Http\Api\AccountFilter;+use FireflyIII\Transformers\AccountTransformer;+use Illuminate\Http\JsonResponse;+use Illuminate\Http\Request;+use Illuminate\Pagination\LengthAwarePaginator;+use League\Fractal\Pagination\IlluminatePaginatorAdapter;+use League\Fractal\Resource\Collection as FractalCollection;+use League\Fractal\Resource\Item;++/**+ * Class ShowController+ */+class ShowController extends Controller+{+ use AccountFilter;++ public const RESOURCE_KEY = 'accounts';++ private AccountRepositoryInterface $repository;++ /**+ * AccountController constructor.+ *+ * @codeCoverageIgnore+ */+ public function __construct()+ {+ parent::__construct();+ $this->middleware(+ function ($request, $next) {+ $this->repository = app(AccountRepositoryInterface::class);+ $this->repository->setUser(auth()->user());++ return $next($request);+ }+ );+ }++ /**+ * Display a listing of the resource.+ *+ * @param Request $request+ *+ * @codeCoverageIgnore+ * @return JsonResponse+ */+ public function index(Request $request): JsonResponse+ {+ $manager = $this->getManager();+ $type = $request->get('type') ?? 'all';+ $this->parameters->set('type', $type);++ // types to get, page size:+ $types = $this->mapAccountTypes($this->parameters->get('type'));+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;++ // get list of accounts. Count it and split it.+ $collection = $this->repository->getAccountsByType($types);+ $count = $collection->count();+ $accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);++ // make paginator:+ $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));+ $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams());++ /** @var AccountTransformer $transformer */+ $transformer = app(AccountTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY);+ $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }++ /**+ * Show single instance.+ *+ * @param Account $account+ *+ * @return JsonResponse+ */+ public function show(Account $account): JsonResponse+ {+ $manager = $this->getManager();++ /** @var AccountTransformer $transformer */+ $transformer = app(AccountTransformer::class);+ $transformer->setParameters($this->parameters);+ $resource = new Item($account, $transformer, self::RESOURCE_KEY);++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }++}\ No newline at end of filediff --git a/app/Api/V1/Controllers/Models/Account/StoreController.php b/app/Api/V1/Controllers/Models/Account/StoreController.phpindex 5609fda218..481782338a 100644--- a/app/Api/V1/Controllers/Models/Account/StoreController.php+++ b/app/Api/V1/Controllers/Models/Account/StoreController.php@@ -26,8 +26,6 @@ namespace FireflyIII\Api\V1\Controllers\Models\Account;use FireflyIII\Api\V1\Controllers\Controller;use FireflyIII\Api\V1\Requests\AccountStoreRequest;use FireflyIII\Repositories\Account\AccountRepositoryInterface;-use FireflyIII\Support\Http\Api\AccountFilter;-use FireflyIII\Support\Http\Api\TransactionFilter;use FireflyIII\Transformers\AccountTransformer;use Illuminate\Http\JsonResponse;use League\Fractal\Resource\Item;@@ -37,8 +35,6 @@ use League\Fractal\Resource\Item;*/class StoreController extends Controller{- use AccountFilter, TransactionFilter;-public const RESOURCE_KEY = 'accounts';private AccountRepositoryInterface $repository;diff --git a/app/Api/V1/Controllers/Models/Account/UpdateController.php b/app/Api/V1/Controllers/Models/Account/UpdateController.phpindex f4a73a978e..14665a5843 100644--- a/app/Api/V1/Controllers/Models/Account/UpdateController.php+++ b/app/Api/V1/Controllers/Models/Account/UpdateController.php@@ -27,8 +27,6 @@ use FireflyIII\Api\V1\Controllers\Controller;use FireflyIII\Api\V1\Requests\AccountUpdateRequest;use FireflyIII\Models\Account;use FireflyIII\Repositories\Account\AccountRepositoryInterface;-use FireflyIII\Support\Http\Api\AccountFilter;-use FireflyIII\Support\Http\Api\TransactionFilter;use FireflyIII\Transformers\AccountTransformer;use Illuminate\Http\JsonResponse;use League\Fractal\Resource\Item;@@ -38,8 +36,6 @@ use League\Fractal\Resource\Item;*/class UpdateController extends Controller{- use AccountFilter, TransactionFilter;-public const RESOURCE_KEY = 'accounts';private AccountRepositoryInterface $repository;diff --git a/app/Api/V1/Controllers/Models/Budget/DestroyController.php b/app/Api/V1/Controllers/Models/Budget/DestroyController.phpindex e6b7f4293c..9afe473fd5 100644--- a/app/Api/V1/Controllers/Models/Budget/DestroyController.php+++ b/app/Api/V1/Controllers/Models/Budget/DestroyController.php@@ -51,6 +51,7 @@ class DestroyController extends Controller});}+/*** Remove the specified resource from storage.*diff --git a/app/Api/V1/Controllers/Models/Budget/ListController.php b/app/Api/V1/Controllers/Models/Budget/ListController.phpindex f8f1e740ad..065b7597f5 100644--- a/app/Api/V1/Controllers/Models/Budget/ListController.php+++ b/app/Api/V1/Controllers/Models/Budget/ListController.php@@ -30,7 +30,6 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;use FireflyIII\Support\Http\Api\TransactionFilter;use FireflyIII\Transformers\AttachmentTransformer;use FireflyIII\Transformers\BudgetLimitTransformer;-use FireflyIII\Transformers\BudgetTransformer;use FireflyIII\Transformers\TransactionGroupTransformer;use FireflyIII\User;use Illuminate\Http\JsonResponse;@@ -38,7 +37,6 @@ use Illuminate\Http\Request;use Illuminate\Pagination\LengthAwarePaginator;use League\Fractal\Pagination\IlluminatePaginatorAdapter;use League\Fractal\Resource\Collection as FractalCollection;-use League\Fractal\Resource\Item;/**** Class ListController@@ -46,8 +44,9 @@ use League\Fractal\Resource\Item;class ListController extends Controller{use TransactionFilter;+private BudgetLimitRepositoryInterface $blRepository;- private BudgetRepositoryInterface $repository;+ private BudgetRepositoryInterface $repository;/*** ListController constructor.@@ -69,36 +68,6 @@ class ListController extends Controller);}- /**- * Display a listing of the resource.- *- * @param Budget $budget- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function budgetLimits(Budget $budget): JsonResponse- {- $manager = $this->getManager();- $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;- $this->parameters->set('budget_id', $budget->id);- $collection = $this->blRepository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));- $count = $collection->count();- $budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);- $paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page'));- $paginator->setPath(route('api.v1.budgets.budget_limits', [$budget->id]) . $this->buildParams());-- /** @var BudgetLimitTransformer $transformer */- $transformer = app(BudgetLimitTransformer::class);- $transformer->setParameters($this->parameters);--- $resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits');- $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-/*** @param Budget $budget*@@ -108,7 +77,7 @@ class ListController extends Controllerpublic function attachments(Budget $budget): JsonResponse{$manager = $this->getManager();- $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;$collection = $this->repository->getAttachments($budget);$count = $collection->count();@@ -131,58 +100,33 @@ class ListController extends Controller/*** Display a listing of the resource.*- * @return JsonResponse- * @codeCoverageIgnore- */- public function index(): JsonResponse- {- $manager = $this->getManager();-- // types to get, page size:- $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;-- // get list of budgets. Count it and split it.- $collection = $this->repository->getBudgets();- $count = $collection->count();- $budgets = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);-- // make paginator:- $paginator = new LengthAwarePaginator($budgets, $count, $pageSize, $this->parameters->get('page'));- $paginator->setPath(route('api.v1.budgets.index') . $this->buildParams());-- /** @var BudgetTransformer $transformer */- $transformer = app(BudgetTransformer::class);- $transformer->setParameters($this->parameters);-- $resource = new FractalCollection($budgets, $transformer, 'budgets');- $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-- /**- * Show a budget.- ** @param Budget $budget** @return JsonResponse* @codeCoverageIgnore*/- public function show(Budget $budget): JsonResponse+ public function budgetLimits(Budget $budget): JsonResponse{- $manager = $this->getManager();+ $manager = $this->getManager();+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;+ $this->parameters->set('budget_id', $budget->id);+ $collection = $this->blRepository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));+ $count = $collection->count();+ $budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);+ $paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page'));+ $paginator->setPath(route('api.v1.budgets.budget_limits', [$budget->id]) . $this->buildParams());- /** @var BudgetTransformer $transformer */- $transformer = app(BudgetTransformer::class);+ /** @var BudgetLimitTransformer $transformer */+ $transformer = app(BudgetLimitTransformer::class);$transformer->setParameters($this->parameters);- $resource = new Item($budget, $transformer, 'budgets');++ $resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits');+ $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);}--/*** Show all transactions.*@@ -195,7 +139,7 @@ class ListController extends Controller*/public function transactions(Request $request, Budget $budget): JsonResponse{- $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;// user can overrule page size with limit parameter.$limit = $this->parameters->get('limit');diff --git a/app/Api/V1/Controllers/Models/Budget/ShowController.php b/app/Api/V1/Controllers/Models/Budget/ShowController.phpnew file mode 100644index 0000000000..424e7fee6e--- /dev/null+++ b/app/Api/V1/Controllers/Models/Budget/ShowController.php@@ -0,0 +1,117 @@+<?php+/*+ * ShowController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Budget;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Models\Budget;+use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;+use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;+use FireflyIII\Transformers\BudgetTransformer;+use Illuminate\Http\JsonResponse;+use Illuminate\Pagination\LengthAwarePaginator;+use League\Fractal\Pagination\IlluminatePaginatorAdapter;+use League\Fractal\Resource\Collection as FractalCollection;+use League\Fractal\Resource\Item;++/**+ * Class ShowController+ */+class ShowController extends Controller+{+ private BudgetLimitRepositoryInterface $blRepository;+ private BudgetRepositoryInterface $repository;++ /**+ * ListController constructor.+ *+ * @codeCoverageIgnore+ */+ public function __construct()+ {+ parent::__construct();+ $this->middleware(+ function ($request, $next) {+ $this->repository = app(BudgetRepositoryInterface::class);+ $this->blRepository = app(BudgetLimitRepositoryInterface::class);+ $this->repository->setUser(auth()->user());+ $this->blRepository->setUser(auth()->user());++ return $next($request);+ }+ );+ }++ /**+ * Display a listing of the resource.+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function index(): JsonResponse+ {+ $manager = $this->getManager();++ // types to get, page size:+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;++ // get list of budgets. Count it and split it.+ $collection = $this->repository->getBudgets();+ $count = $collection->count();+ $budgets = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);++ // make paginator:+ $paginator = new LengthAwarePaginator($budgets, $count, $pageSize, $this->parameters->get('page'));+ $paginator->setPath(route('api.v1.budgets.index') . $this->buildParams());++ /** @var BudgetTransformer $transformer */+ $transformer = app(BudgetTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new FractalCollection($budgets, $transformer, 'budgets');+ $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }++ /**+ * Show a budget.+ *+ * @param Budget $budget+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function show(Budget $budget): JsonResponse+ {+ $manager = $this->getManager();++ /** @var BudgetTransformer $transformer */+ $transformer = app(BudgetTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new Item($budget, $transformer, 'budgets');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }++}\ No newline at end of filediff --git a/app/Api/V1/Controllers/Models/Budget/UpdateController.php b/app/Api/V1/Controllers/Models/Budget/UpdateController.phpindex 7c5637d9c6..9aca581620 100644--- a/app/Api/V1/Controllers/Models/Budget/UpdateController.php+++ b/app/Api/V1/Controllers/Models/Budget/UpdateController.php@@ -54,6 +54,7 @@ class UpdateController extends Controller});}+/*** Update a budget.*diff --git a/app/Api/V1/Controllers/Models/Transaction/DestroyController.php b/app/Api/V1/Controllers/Models/Transaction/DestroyController.phpnew file mode 100644index 0000000000..f4eacf40f7--- /dev/null+++ b/app/Api/V1/Controllers/Models/Transaction/DestroyController.php@@ -0,0 +1,96 @@+<?php+/*+ * DestroyController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Transaction;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Events\DestroyedTransactionGroup;+use FireflyIII\Models\TransactionGroup;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;+use FireflyIII\Support\Http\Api\TransactionFilter;+use FireflyIII\User;+use Illuminate\Http\JsonResponse;++/**+ * Class DestroyController+ */+class DestroyController extends Controller+{+ private JournalRepositoryInterface $repository;+++ /**+ * TransactionController constructor.+ *+ * @codeCoverageIgnore+ */+ public function __construct()+ {+ parent::__construct();+ $this->middleware(+ function ($request, $next) {+ /** @var User $admin */+ $admin = auth()->user();++ $this->repository = app(JournalRepositoryInterface::class);+ $this->repository->setUser($admin);++ return $next($request);+ }+ );+ }+++ /**+ * Remove the specified resource from storage.+ *+ * @param TransactionGroup $transactionGroup+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function destroy(TransactionGroup $transactionGroup): JsonResponse+ {+ $this->repository->destroyGroup($transactionGroup);+ // trigger just after destruction+ event(new DestroyedTransactionGroup($transactionGroup));++ return response()->json([], 204);+ }+++ /**+ * Remove the specified resource from storage.+ *+ * @param TransactionJournal $transactionJournal+ *+ * @codeCoverageIgnore+ * @return JsonResponse+ */+ public function destroyJournal(TransactionJournal $transactionJournal): JsonResponse+ {+ $this->repository->destroyJournal($transactionJournal);++ return response()->json([], 204);+ }+}\ No newline at end of filediff --git a/app/Api/V1/Controllers/Models/Transaction/ListController.php b/app/Api/V1/Controllers/Models/Transaction/ListController.phpnew file mode 100644index 0000000000..733b99dc64--- /dev/null+++ b/app/Api/V1/Controllers/Models/Transaction/ListController.php@@ -0,0 +1,135 @@+<?php+/*+ * ListController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Transaction;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Models\TransactionGroup;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;+use FireflyIII\Support\Http\Api\TransactionFilter;+use FireflyIII\Transformers\AttachmentTransformer;+use FireflyIII\Transformers\PiggyBankEventTransformer;+use FireflyIII\Transformers\TransactionLinkTransformer;+use FireflyIII\User;+use Illuminate\Http\JsonResponse;+use Illuminate\Support\Collection;+use League\Fractal\Resource\Collection as FractalCollection;++/**+ * Class ListController+ */+class ListController extends Controller+{+ private JournalAPIRepositoryInterface $journalAPIRepository;++ /**+ * TransactionController constructor.+ *+ * @codeCoverageIgnore+ */+ public function __construct()+ {+ parent::__construct();+ $this->middleware(+ function ($request, $next) {+ /** @var User $admin */+ $admin = auth()->user();++ $this->journalAPIRepository = app(JournalAPIRepositoryInterface::class);+ $this->journalAPIRepository->setUser($admin);++ return $next($request);+ }+ );+ }+++ /**+ * @param TransactionGroup $transactionGroup+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function attachments(TransactionGroup $transactionGroup): JsonResponse+ {+ $manager = $this->getManager();+ $attachments = new Collection;+ foreach ($transactionGroup->transactionJournals as $transactionJournal) {+ $attachments = $this->journalAPIRepository->getAttachments($transactionJournal)->merge($attachments);+ }++ /** @var AttachmentTransformer $transformer */+ $transformer = app(AttachmentTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new FractalCollection($attachments, $transformer, 'attachments');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }+++ /**+ * @param TransactionGroup $transactionGroup+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function piggyBankEvents(TransactionGroup $transactionGroup): JsonResponse+ {+ $manager = $this->getManager();+ $events = new Collection;+ foreach ($transactionGroup->transactionJournals as $transactionJournal) {+ $events = $this->journalAPIRepository->getPiggyBankEvents($transactionJournal)->merge($events);+ }++ /** @var PiggyBankEventTransformer $transformer */+ $transformer = app(PiggyBankEventTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new FractalCollection($events, $transformer, 'piggy_bank_events');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }+++ /**+ * @param TransactionJournal $transactionJournal+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function transactionLinks(TransactionJournal $transactionJournal): JsonResponse+ {+ $manager = $this->getManager();+ $journalLinks = $this->journalAPIRepository->getJournalLinks($transactionJournal);++ /** @var TransactionLinkTransformer $transformer */+ $transformer = app(TransactionLinkTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new FractalCollection($journalLinks, $transformer, 'transaction_links');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }+}\ No newline at end of filediff --git a/app/Api/V1/Controllers/Models/Transaction/ShowController.php b/app/Api/V1/Controllers/Models/Transaction/ShowController.phpnew file mode 100644index 0000000000..895093dd93--- /dev/null+++ b/app/Api/V1/Controllers/Models/Transaction/ShowController.php@@ -0,0 +1,146 @@+<?php+/*+ * ShowController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Transaction;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Helpers\Collector\GroupCollectorInterface;+use FireflyIII\Models\TransactionGroup;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\Support\Http\Api\TransactionFilter;+use FireflyIII\Transformers\TransactionGroupTransformer;+use FireflyIII\User;+use Illuminate\Http\JsonResponse;+use Illuminate\Http\Request;+use League\Fractal\Pagination\IlluminatePaginatorAdapter;+use League\Fractal\Resource\Collection as FractalCollection;+use League\Fractal\Resource\Item;+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;++/**+ * Class ShowController+ */+class ShowController extends Controller+{+ use TransactionFilter;++ /**+ * Show all transactions.+ *+ * @param Request $request+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function index(Request $request): JsonResponse+ {+ $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;+ $type = $request->get('type') ?? 'default';+ $this->parameters->set('type', $type);++ $types = $this->mapTransactionTypes($this->parameters->get('type'));+ $manager = $this->getManager();+ /** @var User $admin */+ $admin = auth()->user();++ // use new group collector:+ /** @var GroupCollectorInterface $collector */+ $collector = app(GroupCollectorInterface::class);+ $collector+ ->setUser($admin)+ // all info needed for the API:+ ->withAPIInformation()+ // set page size:+ ->setLimit($pageSize)+ // set page to retrieve+ ->setPage($this->parameters->get('page'))+ // set types of transactions to return.+ ->setTypes($types);+++ if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {+ $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));+ }+ $paginator = $collector->getPaginatedGroups();+ $paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());+ $transactions = $paginator->getCollection();++ /** @var TransactionGroupTransformer $transformer */+ $transformer = app(TransactionGroupTransformer::class);+ $transformer->setParameters($this->parameters);++ $resource = new FractalCollection($transactions, $transformer, 'transactions');+ $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }+++ /**+ * Show a single transaction.+ *+ * @param TransactionGroup $transactionGroup+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function show(TransactionGroup $transactionGroup): JsonResponse+ {+ $manager = $this->getManager();+ /** @var User $admin */+ $admin = auth()->user();+ // use new group collector:+ /** @var GroupCollectorInterface $collector */+ $collector = app(GroupCollectorInterface::class);+ $collector+ ->setUser($admin)+ // filter on transaction group.+ ->setTransactionGroup($transactionGroup)+ // all info needed for the API:+ ->withAPIInformation();++ $selectedGroup = $collector->getGroups()->first();+ if (null === $selectedGroup) {+ throw new NotFoundHttpException();+ }+ /** @var TransactionGroupTransformer $transformer */+ $transformer = app(TransactionGroupTransformer::class);+ $transformer->setParameters($this->parameters);+ $resource = new Item($selectedGroup, $transformer, 'transactions');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }++ /**+ * Show a single transaction, by transaction journal.+ *+ * @param TransactionJournal $transactionJournal+ *+ * @return JsonResponse+ * @codeCoverageIgnore+ */+ public function showByJournal(TransactionJournal $transactionJournal): JsonResponse+ {+ return $this->show($transactionJournal->transactionGroup);+ }++}\ No newline at end of filediff --git a/app/Api/V1/Controllers/Models/Transaction/StoreController.php b/app/Api/V1/Controllers/Models/Transaction/StoreController.phpnew file mode 100644index 0000000000..087a190d9e--- /dev/null+++ b/app/Api/V1/Controllers/Models/Transaction/StoreController.php@@ -0,0 +1,131 @@+<?php+/*+ * StoreController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Transaction;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Api\V1\Requests\TransactionStoreRequest;+use FireflyIII\Events\StoredTransactionGroup;+use FireflyIII\Exceptions\DuplicateTransactionException;+use FireflyIII\Exceptions\FireflyException;+use FireflyIII\Helpers\Collector\GroupCollectorInterface;+use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;+use FireflyIII\Rules\IsDuplicateTransaction;+use FireflyIII\Support\Http\Api\TransactionFilter;+use FireflyIII\Transformers\TransactionGroupTransformer;+use FireflyIII\User;+use Illuminate\Http\JsonResponse;+use Illuminate\Validation\ValidationException;+use League\Fractal\Resource\Item;++/**+ * Class StoreController+ */+class StoreController extends Controller+{+ use TransactionFilter;++ private TransactionGroupRepositoryInterface $groupRepository;+++ /**+ * TransactionController constructor.+ *+ * @codeCoverageIgnore+ */+ public function __construct()+ {+ parent::__construct();+ $this->middleware(+ function ($request, $next) {+ /** @var User $admin */+ $admin = auth()->user();++ $this->groupRepository = app(TransactionGroupRepositoryInterface::class);+ $this->groupRepository->setUser($admin);++ return $next($request);+ }+ );+ }+++ /**+ * Store a new transaction.+ *+ * @param TransactionStoreRequest $request+ *+ * @return JsonResponse+ * @throws FireflyException|ValidationException+ */+ public function store(TransactionStoreRequest $request): JsonResponse+ {+ Log::debug('Now in API TransactionController::store()');+ $data = $request->getAll();+ $data['user'] = auth()->user()->id;++ Log::channel('audit')+ ->info('Store new transaction over API.', $data);++ try {+ $transactionGroup = $this->groupRepository->store($data);+ } catch (DuplicateTransactionException $e) {+ Log::warning('Caught a duplicate transaction. Return error message.');+ $validator = Validator::make(+ ['transactions' => [['description' => $e->getMessage()]]], ['transactions.0.description' => new IsDuplicateTransaction]+ );+ throw new ValidationException($validator);+ } catch (FireflyException $e) {+ Log::warning('Caught an exception. Return error message.');+ Log::error($e->getMessage());+ $message = sprintf('Internal exception: %s', $e->getMessage());+ $validator = Validator::make(['transactions' => [['description' => $message]]], ['transactions.0.description' => new IsDuplicateTransaction]);+ throw new ValidationException($validator);+ }+ app('preferences')->mark();+ event(new StoredTransactionGroup($transactionGroup, $data['apply_rules'] ?? true));++ $manager = $this->getManager();+ /** @var User $admin */+ $admin = auth()->user();+ // use new group collector:+ /** @var GroupCollectorInterface $collector */+ $collector = app(GroupCollectorInterface::class);+ $collector+ ->setUser($admin)+ // filter on transaction group.+ ->setTransactionGroup($transactionGroup)+ // all info needed for the API:+ ->withAPIInformation();++ $selectedGroup = $collector->getGroups()->first();+ if (null === $selectedGroup) {+ throw new FireflyException('Cannot find transaction. Possibly, a rule deleted this transaction after its creation.');+ }+ /** @var TransactionGroupTransformer $transformer */+ $transformer = app(TransactionGroupTransformer::class);+ $transformer->setParameters($this->parameters);+ $resource = new Item($selectedGroup, $transformer, 'transactions');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }+}\ No newline at end of filediff --git a/app/Api/V1/Controllers/Models/Transaction/UpdateController.php b/app/Api/V1/Controllers/Models/Transaction/UpdateController.phpnew file mode 100644index 0000000000..951cb13875--- /dev/null+++ b/app/Api/V1/Controllers/Models/Transaction/UpdateController.php@@ -0,0 +1,108 @@+<?php+/*+ * UpdateController.php+ * Copyright (c) 2021 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Api\V1\Controllers\Models\Transaction;+++use FireflyIII\Api\V1\Controllers\Controller;+use FireflyIII\Api\V1\Requests\TransactionUpdateRequest;+use FireflyIII\Events\UpdatedTransactionGroup;+use FireflyIII\Helpers\Collector\GroupCollectorInterface;+use FireflyIII\Models\TransactionGroup;+use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;+use FireflyIII\Transformers\TransactionGroupTransformer;+use FireflyIII\User;+use Illuminate\Http\JsonResponse;+use League\Fractal\Resource\Item;+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;++/**+ * Class UpdateController+ */+class UpdateController extends Controller+{+ private TransactionGroupRepositoryInterface $groupRepository;++ /**+ * TransactionController constructor.+ *+ * @codeCoverageIgnore+ */+ public function __construct()+ {+ parent::__construct();+ $this->middleware(+ function ($request, $next) {+ /** @var User $admin */+ $admin = auth()->user();++ $this->groupRepository = app(TransactionGroupRepositoryInterface::class);+ $this->groupRepository->setUser($admin);++ return $next($request);+ }+ );+ }+++ /**+ * Update a transaction.+ *+ * @param TransactionUpdateRequest $request+ * @param TransactionGroup $transactionGroup+ *+ * @return JsonResponse+ */+ public function update(TransactionUpdateRequest $request, TransactionGroup $transactionGroup): JsonResponse+ {+ Log::debug('Now in update routine.');+ $data = $request->getAll();+ $transactionGroup = $this->groupRepository->update($transactionGroup, $data);+ $manager = $this->getManager();++ app('preferences')->mark();+ event(new UpdatedTransactionGroup($transactionGroup, $data['apply_rules'] ?? true));++ /** @var User $admin */+ $admin = auth()->user();+ // use new group collector:+ /** @var GroupCollectorInterface $collector */+ $collector = app(GroupCollectorInterface::class);+ $collector+ ->setUser($admin)+ // filter on transaction group.+ ->setTransactionGroup($transactionGroup)+ // all info needed for the API:+ ->withAPIInformation();++ $selectedGroup = $collector->getGroups()->first();+ if (null === $selectedGroup) {+ throw new NotFoundHttpException(); // @codeCoverageIgnore+ }+ /** @var TransactionGroupTransformer $transformer */+ $transformer = app(TransactionGroupTransformer::class);+ $transformer->setParameters($this->parameters);+ $resource = new Item($selectedGroup, $transformer, 'transactions');++ return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);+ }++}\ No newline at end of filediff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.phpdeleted file mode 100644index f7addd8d0e..0000000000--- a/app/Api/V1/Controllers/TransactionController.php+++ /dev/null@@ -1,394 +0,0 @@-<?php--/**- * TransactionController.php- * Copyright (c) 2019 [email protected]- *- * This file is part of Firefly III (https://github.com/firefly-iii).- *- * This program is free software: you can redistribute it and/or modify- * it under the terms of the GNU Affero General Public License as- * published by the Free Software Foundation, either version 3 of the- * License, or (at your option) any later version.- *- * This program is distributed in the hope that it will be useful,- * but WITHOUT ANY WARRANTY; without even the implied warranty of- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- * GNU Affero General Public License for more details.- *- * You should have received a copy of the GNU Affero General Public License- * along with this program. If not, see <https://www.gnu.org/licenses/>.- */--declare(strict_types=1);--namespace FireflyIII\Api\V1\Controllers;--use FireflyIII\Api\V1\Requests\TransactionStoreRequest;-use FireflyIII\Api\V1\Requests\TransactionUpdateRequest;-use FireflyIII\Events\DestroyedTransactionGroup;-use FireflyIII\Events\StoredTransactionGroup;-use FireflyIII\Events\UpdatedTransactionGroup;-use FireflyIII\Exceptions\DuplicateTransactionException;-use FireflyIII\Exceptions\FireflyException;-use FireflyIII\Helpers\Collector\GroupCollectorInterface;-use FireflyIII\Models\TransactionGroup;-use FireflyIII\Models\TransactionJournal;-use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;-use FireflyIII\Repositories\Journal\JournalRepositoryInterface;-use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;-use FireflyIII\Rules\IsDuplicateTransaction;-use FireflyIII\Support\Http\Api\TransactionFilter;-use FireflyIII\Transformers\AttachmentTransformer;-use FireflyIII\Transformers\PiggyBankEventTransformer;-use FireflyIII\Transformers\TransactionGroupTransformer;-use FireflyIII\Transformers\TransactionLinkTransformer;-use FireflyIII\User;-use Illuminate\Http\JsonResponse;-use Illuminate\Http\Request;-use Illuminate\Support\Collection;-use Illuminate\Validation\ValidationException;-use League\Fractal\Pagination\IlluminatePaginatorAdapter;-use League\Fractal\Resource\Collection as FractalCollection;-use League\Fractal\Resource\Item;-use Log;-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;-use Validator;--/**- * Class TransactionController- */-class TransactionController extends Controller-{- use TransactionFilter;-- private TransactionGroupRepositoryInterface $groupRepository;- private JournalAPIRepositoryInterface $journalAPIRepository;- private JournalRepositoryInterface $repository;--- /**- * TransactionController constructor.- *- * @codeCoverageIgnore- */- public function __construct()- {- parent::__construct();- $this->middleware(- function ($request, $next) {- /** @var User $admin */- $admin = auth()->user();-- $this->repository = app(JournalRepositoryInterface::class);- $this->groupRepository = app(TransactionGroupRepositoryInterface::class);- $this->journalAPIRepository = app(JournalAPIRepositoryInterface::class);- $this->repository->setUser($admin);- $this->groupRepository->setUser($admin);- $this->journalAPIRepository->setUser($admin);-- return $next($request);- }- );- }-- /**- * @param TransactionGroup $transactionGroup- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function attachments(TransactionGroup $transactionGroup): JsonResponse- {- $manager = $this->getManager();- $attachments = new Collection;- foreach ($transactionGroup->transactionJournals as $transactionJournal) {- $attachments = $this->journalAPIRepository->getAttachments($transactionJournal)->merge($attachments);- }-- /** @var AttachmentTransformer $transformer */- $transformer = app(AttachmentTransformer::class);- $transformer->setParameters($this->parameters);-- $resource = new FractalCollection($attachments, $transformer, 'attachments');-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-- /**- * @param TransactionJournal $transactionJournal- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function transactionLinks(TransactionJournal $transactionJournal): JsonResponse- {- $manager = $this->getManager();- $journalLinks = $this->journalAPIRepository->getJournalLinks($transactionJournal);-- /** @var TransactionLinkTransformer $transformer */- $transformer = app(TransactionLinkTransformer::class);- $transformer->setParameters($this->parameters);-- $resource = new FractalCollection($journalLinks, $transformer, 'transaction_links');-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-- /**- * Remove the specified resource from storage.- *- * @param TransactionGroup $transactionGroup- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function delete(TransactionGroup $transactionGroup): JsonResponse- {- $this->repository->destroyGroup($transactionGroup);- // trigger just after destruction- event(new DestroyedTransactionGroup($transactionGroup));-- return response()->json([], 204);- }-- /**- * Remove the specified resource from storage.- *- * @param TransactionJournal $transactionJournal- *- * @codeCoverageIgnore- * @return JsonResponse- */- public function deleteJournal(TransactionJournal $transactionJournal): JsonResponse- {- $this->repository->destroyJournal($transactionJournal);-- return response()->json([], 204);- }-- /**- * Show all transactions.- *- * @param Request $request- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function index(Request $request): JsonResponse- {- $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;- $type = $request->get('type') ?? 'default';- $this->parameters->set('type', $type);-- $types = $this->mapTransactionTypes($this->parameters->get('type'));- $manager = $this->getManager();- /** @var User $admin */- $admin = auth()->user();-- // use new group collector:- /** @var GroupCollectorInterface $collector */- $collector = app(GroupCollectorInterface::class);- $collector- ->setUser($admin)- // all info needed for the API:- ->withAPIInformation()- // set page size:- ->setLimit($pageSize)- // set page to retrieve- ->setPage($this->parameters->get('page'))- // set types of transactions to return.- ->setTypes($types);--- if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {- $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));- }- $paginator = $collector->getPaginatedGroups();- $paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());- $transactions = $paginator->getCollection();-- /** @var TransactionGroupTransformer $transformer */- $transformer = app(TransactionGroupTransformer::class);- $transformer->setParameters($this->parameters);-- $resource = new FractalCollection($transactions, $transformer, 'transactions');- $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-- /**- * @param TransactionGroup $transactionGroup- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function piggyBankEvents(TransactionGroup $transactionGroup): JsonResponse- {- $manager = $this->getManager();- $events = new Collection;- foreach ($transactionGroup->transactionJournals as $transactionJournal) {- $events = $this->journalAPIRepository->getPiggyBankEvents($transactionJournal)->merge($events);- }-- /** @var PiggyBankEventTransformer $transformer */- $transformer = app(PiggyBankEventTransformer::class);- $transformer->setParameters($this->parameters);-- $resource = new FractalCollection($events, $transformer, 'piggy_bank_events');-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-- /**- * Show a single transaction, by transaction journal.- *- * @param TransactionJournal $transactionJournal- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function showByJournal(TransactionJournal $transactionJournal): JsonResponse- {- return $this->show($transactionJournal->transactionGroup);- }-- /**- * Show a single transaction.- *- * @param TransactionGroup $transactionGroup- *- * @return JsonResponse- * @codeCoverageIgnore- */- public function show(TransactionGroup $transactionGroup): JsonResponse- {- $manager = $this->getManager();- /** @var User $admin */- $admin = auth()->user();- // use new group collector:- /** @var GroupCollectorInterface $collector */- $collector = app(GroupCollectorInterface::class);- $collector- ->setUser($admin)- // filter on transaction group.- ->setTransactionGroup($transactionGroup)- // all info needed for the API:- ->withAPIInformation();-- $selectedGroup = $collector->getGroups()->first();- if (null === $selectedGroup) {- throw new NotFoundHttpException();- }- /** @var TransactionGroupTransformer $transformer */- $transformer = app(TransactionGroupTransformer::class);- $transformer->setParameters($this->parameters);- $resource = new Item($selectedGroup, $transformer, 'transactions');-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-- /**- * Store a new transaction.- *- * @param TransactionStoreRequest $request- *- * @return JsonResponse- * @throws FireflyException|ValidationException- */- public function store(TransactionStoreRequest $request): JsonResponse- {- Log::debug('Now in API TransactionController::store()');- $data = $request->getAll();- $data['user'] = auth()->user()->id;-- Log::channel('audit')- ->info('Store new transaction over API.', $data);-- try {- $transactionGroup = $this->groupRepository->store($data);- } catch (DuplicateTransactionException $e) {- Log::warning('Caught a duplicate transaction. Return error message.');- $validator = Validator::make(- ['transactions' => [['description' => $e->getMessage()]]], ['transactions.0.description' => new IsDuplicateTransaction]- );- throw new ValidationException($validator);- } catch (FireflyException $e) {- Log::warning('Caught an exception. Return error message.');- Log::error($e->getMessage());- $message = sprintf('Internal exception: %s', $e->getMessage());- $validator = Validator::make(['transactions' => [['description' => $message]]], ['transactions.0.description' => new IsDuplicateTransaction]);- throw new ValidationException($validator);- }- app('preferences')->mark();- event(new StoredTransactionGroup($transactionGroup, $data['apply_rules'] ?? true));-- $manager = $this->getManager();- /** @var User $admin */- $admin = auth()->user();- // use new group collector:- /** @var GroupCollectorInterface $collector */- $collector = app(GroupCollectorInterface::class);- $collector- ->setUser($admin)- // filter on transaction group.- ->setTransactionGroup($transactionGroup)- // all info needed for the API:- ->withAPIInformation();-- $selectedGroup = $collector->getGroups()->first();- if (null === $selectedGroup) {- throw new FireflyException('Cannot find transaction. Possibly, a rule deleted this transaction after its creation.');- }- /** @var TransactionGroupTransformer $transformer */- $transformer = app(TransactionGroupTransformer::class);- $transformer->setParameters($this->parameters);- $resource = new Item($selectedGroup, $transformer, 'transactions');-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }--- /**- * Update a transaction.- *- * @param TransactionUpdateRequest $request- * @param TransactionGroup $transactionGroup- *- * @return JsonResponse- */- public function update(TransactionUpdateRequest $request, TransactionGroup $transactionGroup): JsonResponse- {- Log::debug('Now in update routine.');- $data = $request->getAll();- $transactionGroup = $this->groupRepository->update($transactionGroup, $data);- $manager = $this->getManager();-- app('preferences')->mark();- event(new UpdatedTransactionGroup($transactionGroup, $data['apply_rules'] ?? true));-- /** @var User $admin */- $admin = auth()->user();- // use new group collector:- /** @var GroupCollectorInterface $collector */- $collector = app(GroupCollectorInterface::class);- $collector- ->setUser($admin)- // filter on transaction group.- ->setTransactionGroup($transactionGroup)- // all info needed for the API:- ->withAPIInformation();-- $selectedGroup = $collector->getGroups()->first();- if (null === $selectedGroup) {- throw new NotFoundHttpException(); // @codeCoverageIgnore- }- /** @var TransactionGroupTransformer $transformer */- $transformer = app(TransactionGroupTransformer::class);- $transformer->setParameters($this->parameters);- $resource = new Item($selectedGroup, $transformer, 'transactions');-- return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);- }-}diff --git a/routes/api.php b/routes/api.phpindex 1a3fc71c0d..471e648678 100644--- a/routes/api.php+++ b/routes/api.php@@ -44,9 +44,9 @@ Route::group(static function () {// Accounts API routes:- Route::get('', ['uses' => 'Models\Account\ListController@index', 'as' => 'index']);+ Route::get('', ['uses' => 'Models\Account\ShowController@index', 'as' => 'index']);Route::post('', ['uses' => 'Models\Account\StoreController@store', 'as' => 'store']);- Route::get('{account}', ['uses' => 'Models\Account\ListController@show', 'as' => 'show']);+ Route::get('{account}', ['uses' => 'Models\Account\ShowController@show', 'as' => 'show']);Route::put('{account}', ['uses' => 'Models\Account\UpdateController@update', 'as' => 'update']);Route::delete('{account}', ['uses' => 'Models\Account\DestroyController@destroy', 'as' => 'delete']);@@ -162,9 +162,9 @@ Route::group(static function () {// Budget API routes:- Route::get('', ['uses' => 'Models\Budget\ListController@index', 'as' => 'index']);+ Route::get('', ['uses' => 'Models\Budget\ShowController@index', 'as' => 'index']);Route::post('', ['uses' => 'Models\Budget\StoreController@store', 'as' => 'store']);- Route::get('{budget}', ['uses' => 'Models\Budget\ListController@show', 'as' => 'show']);+ Route::get('{budget}', ['uses' => 'Models\Budget\ShowController@show', 'as' => 'show']);Route::put('{budget}', ['uses' => 'Models\Budget\UpdateController@update', 'as' => 'update']);Route::delete('{budget}', ['uses' => 'Models\Budget\DestroyController@destroy', 'as' => 'delete']);Route::post('{budget}/limits', ['uses' => 'BudgetLimitController@store', 'as' => 'store_budget_limit']);@@ -499,15 +499,15 @@ Route::group(static function () {// Transaction API routes:- Route::get('', ['uses' => 'TransactionController@index', 'as' => 'index']);- Route::post('', ['uses' => 'TransactionController@store', 'as' => 'store']);- Route::get('{transactionGroup}', ['uses' => 'TransactionController@show', 'as' => 'show']);- Route::get('{transactionGroup}/attachments', ['uses' => 'TransactionController@attachments', 'as' => 'attachments']);- Route::get('{transactionGroup}/piggy_bank_events', ['uses' => 'TransactionController@piggyBankEvents', 'as' => 'piggy_bank_events']);- Route::get('{tj}/transaction_links', ['uses' => 'TransactionController@transactionLinks', 'as' => 'transaction_links']);- Route::put('{transactionGroup}', ['uses' => 'TransactionController@update', 'as' => 'update']);- Route::delete('{transactionGroup}/{tj}', ['uses' => 'TransactionController@deleteJournal', 'as' => 'delete-journal']);- Route::delete('{transactionGroup}', ['uses' => 'TransactionController@delete', 'as' => 'delete']);+ Route::get('', ['uses' => 'Models\Transaction\ShowController@index', 'as' => 'index']);+ Route::post('', ['uses' => 'Models\Transaction\StoreController@store', 'as' => 'store']);+ Route::get('{transactionGroup}', ['uses' => 'Models\Transaction\ShowController@show', 'as' => 'show']);+ Route::get('{transactionGroup}/attachments', ['uses' => 'Models\Transaction\ListController@attachments', 'as' => 'attachments']);+ Route::get('{transactionGroup}/piggy_bank_events', ['uses' => 'Models\Transaction\ListController@piggyBankEvents', 'as' => 'piggy_bank_events']);+ Route::get('{tj}/transaction_links', ['uses' => 'Models\Transaction\ListController@transactionLinks', 'as' => 'transaction_links']);+ Route::put('{transactionGroup}', ['uses' => 'Models\Transaction\UpdateController@update', 'as' => 'update']);+ Route::delete('{transactionGroup}/{tj}', ['uses' => 'Models\Transaction\DestroyController@destroyJournal', 'as' => 'delete-journal']);+ Route::delete('{transactionGroup}', ['uses' => 'Models\Transaction\DestroyController@destroy', 'as' => 'delete']);});