Skip to content

Firefly III: a personal finances manager

Refactor currency API

James Cole83d64262c839
diff --git a/app/Api/V2/Controllers/Model/Account/ShowController.php b/app/Api/V2/Controllers/Model/Account/ShowController.php
index 8f62d0b94d..85a9845da2 100644
--- a/app/Api/V2/Controllers/Model/Account/ShowController.php
+++ b/app/Api/V2/Controllers/Model/Account/ShowController.php
@@ -31,6 +31,8 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
+ * Show = show a single account.
+ * Index = show all accounts
* Class ShowController
*/
class ShowController extends Controller
diff --git a/app/Api/V2/Controllers/Model/Bill/IndexController.php b/app/Api/V2/Controllers/Model/Bill/IndexController.php
new file mode 100644
index 0000000000..6ccb5e0da6
--- /dev/null
+++ b/app/Api/V2/Controllers/Model/Bill/IndexController.php
@@ -0,0 +1,87 @@
+<?php
+
+
+/*
+ * ShowController.php
+ * Copyright (c) 2023 [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\V2\Controllers\Model\Bill;
+
+use FireflyIII\Api\V2\Controllers\Controller;
+use FireflyIII\Exceptions\FireflyException;
+use FireflyIII\Models\Bill;
+use FireflyIII\Repositories\UserGroups\Bill\BillRepositoryInterface;
+use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
+use FireflyIII\Transformers\V2\BillTransformer;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
+use Illuminate\Pagination\LengthAwarePaginator;
+
+/**
+ * Class ShowController
+ */
+class IndexController extends Controller
+{
+ use ValidatesUserGroupTrait;
+
+ private BillRepositoryInterface $repository;
+
+ public function __construct()
+ {
+ parent::__construct();
+ $this->middleware(
+ function ($request, $next) {
+ $this->repository = app(BillRepositoryInterface::class);
+
+ // new way of user group validation
+ $userGroup = $this->validateUserGroup($request);
+ if (null !== $userGroup) {
+ $this->repository->setUserGroup($userGroup);
+ }
+
+ return $next($request);
+ }
+ );
+ }
+
+ /**
+ * @param Request $request
+ *
+ * TODO see autocomplete/accountcontroller for list.
+ *
+ * @return JsonResponse
+ */
+ public function index(Request $request): JsonResponse
+ {
+ $this->repository->correctOrder();
+ $bills = $this->repository->getBills();
+ $pageSize = $this->parameters->get('limit');
+ $count = $bills->count();
+ $bills = $bills->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
+ $paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page'));
+ $transformer = new BillTransformer();
+ $transformer->setParameters($this->parameters); // give params to transformer
+
+ return response()
+ ->json($this->jsonApiList('subscriptions', $paginator, $transformer))
+ ->header('Content-Type', self::CONTENT_TYPE);
+ }
+}
diff --git a/app/Api/V2/Controllers/Model/Bill/ShowController.php b/app/Api/V2/Controllers/Model/Bill/ShowController.php
index ab7781065e..e43107e412 100644
--- a/app/Api/V2/Controllers/Model/Bill/ShowController.php
+++ b/app/Api/V2/Controllers/Model/Bill/ShowController.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V2\Controllers\Model\Bill;
use FireflyIII\Api\V2\Controllers\Controller;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\UserGroups\Bill\BillRepositoryInterface;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
@@ -61,29 +62,6 @@ class ShowController extends Controller
);
}
- /**
- * @param Request $request
- *
- * TODO see autocomplete/accountcontroller for list.
- *
- * @return JsonResponse
- */
- public function index(Request $request): JsonResponse
- {
- $this->repository->correctOrder();
- $bills = $this->repository->getBills();
- $pageSize = $this->parameters->get('limit');
- $count = $bills->count();
- $bills = $bills->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
- $paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page'));
- $transformer = new BillTransformer();
- $transformer->setParameters($this->parameters); // give params to transformer
-
- return response()
- ->json($this->jsonApiList('subscriptions', $paginator, $transformer))
- ->header('Content-Type', self::CONTENT_TYPE);
- }
-
/**
* TODO this endpoint is not documented
*/
diff --git a/app/Api/V2/Controllers/Model/Budget/ListController.php b/app/Api/V2/Controllers/Model/Budget/ListController.php
index b7bbaff1c7..7b92c751ae 100644
--- a/app/Api/V2/Controllers/Model/Budget/ListController.php
+++ b/app/Api/V2/Controllers/Model/Budget/ListController.php
@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V2\Controllers\Model\Budget;
use FireflyIII\Api\V2\Controllers\Controller;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Transformers\V2\BudgetTransformer;
use Illuminate\Http\JsonResponse;
@@ -61,7 +62,7 @@ class ListController extends Controller
public function index(Request $request): JsonResponse
{
echo 'this needs move to Administration';
- exit;
+ throw new FireflyException('Needs migration to IndexController');
$collection = $this->repository->getActiveBudgets();
$total = $collection->count();
$collection->slice($this->pageXSize * $this->parameters->get('page'), $this->pXageSize);
diff --git a/app/Api/V2/Controllers/Model/Budget/ShowController.php b/app/Api/V2/Controllers/Model/Budget/ShowController.php
index c238848359..561be1e45d 100644
--- a/app/Api/V2/Controllers/Model/Budget/ShowController.php
+++ b/app/Api/V2/Controllers/Model/Budget/ShowController.php
@@ -27,6 +27,7 @@ namespace FireflyIII\Api\V2\Controllers\Model\Budget;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\DateRequest;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
@@ -64,6 +65,7 @@ class ShowController extends Controller
*/
public function budgeted(DateRequest $request, Budget $budget): JsonResponse
{
+ throw new FireflyException('Needs refactoring, uses deprecated method.');
$data = $request->getAll();
$result = $this->repository->budgetedInPeriodForBudget($budget, $data['start'], $data['end']);
$converted = $this->cerSum(array_values($result));
@@ -78,6 +80,7 @@ class ShowController extends Controller
*/
public function spent(DateRequest $request, Budget $budget): JsonResponse
{
+ throw new FireflyException('Needs refactoring, uses deprecated method.');
$data = $request->getAll();
$result = $this->repository->spentInPeriodForBudget($budget, $data['start'], $data['end']);
$converted = $this->cerSum(array_values($result));
diff --git a/app/Api/V2/Controllers/Model/Budget/SumController.php b/app/Api/V2/Controllers/Model/Budget/SumController.php
index 15de6ca665..9f5c8f1ff6 100644
--- a/app/Api/V2/Controllers/Model/Budget/SumController.php
+++ b/app/Api/V2/Controllers/Model/Budget/SumController.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V2\Controllers\Model\Budget;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\DateRequest;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
use Illuminate\Http\JsonResponse;
@@ -64,6 +65,7 @@ class SumController extends Controller
*/
public function budgeted(DateRequest $request): JsonResponse
{
+ throw new FireflyException('Needs refactoring, uses deprecated method.');
$data = $request->getAll();
$result = $this->repository->budgetedInPeriod($data['start'], $data['end']);
$converted = $this->cerSum(array_values($result));
@@ -81,6 +83,7 @@ class SumController extends Controller
*/
public function spent(DateRequest $request): JsonResponse
{
+ throw new FireflyException('Needs refactoring, uses deprecated method.');
$data = $request->getAll();
$result = $this->repository->spentInPeriod($data['start'], $data['end']);
$converted = $this->cerSum(array_values($result));
diff --git a/app/Api/V2/Controllers/Model/BudgetLimit/ListController.php b/app/Api/V2/Controllers/Model/BudgetLimit/ListController.php
index cf7ef4f9d7..9b72294183 100644
--- a/app/Api/V2/Controllers/Model/BudgetLimit/ListController.php
+++ b/app/Api/V2/Controllers/Model/BudgetLimit/ListController.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V2\Controllers\Model\BudgetLimit;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\DateRequest;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Transformers\V2\BudgetLimitTransformer;
@@ -57,6 +58,7 @@ class ListController extends Controller
*/
public function index(DateRequest $request, Budget $budget): JsonResponse
{
+ throw new FireflyException('Needs refactoring, move to IndexController.');
$pageSize = $this->parameters->get('limit');
$dates = $request->getAll();
$collection = $this->repository->getBudgetLimits($budget, $dates['start'], $dates['end']);
@@ -67,7 +69,7 @@ class ListController extends Controller
$transformer = new BudgetLimitTransformer();
return response()
- ->api($this->jsonApiList('budget_limits', $paginator, $transformer))
+ ->api($this->jsonApiList('budget-limits', $paginator, $transformer))
->header('Content-Type', self::CONTENT_TYPE);
}
}
diff --git a/app/Api/V2/Controllers/Model/Currency/IndexController.php b/app/Api/V2/Controllers/Model/Currency/IndexController.php
new file mode 100644
index 0000000000..0e71ad2d27
--- /dev/null
+++ b/app/Api/V2/Controllers/Model/Currency/IndexController.php
@@ -0,0 +1,53 @@
+<?php
+declare(strict_types=1);
+
+namespace FireflyIII\Api\V2\Controllers\Model\Currency;
+
+use FireflyIII\Api\V2\Controllers\Controller;
+use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
+use FireflyIII\Transformers\V2\CurrencyTransformer;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Pagination\LengthAwarePaginator;
+
+/**
+ * Class IndexController
+ */
+class IndexController extends Controller
+{
+ private CurrencyRepositoryInterface $repository;
+
+ public function __construct()
+ {
+ parent::__construct();
+ $this->middleware(
+ function ($request, $next) {
+ $this->repository = app(CurrencyRepositoryInterface::class);
+
+ return $next($request);
+ }
+ );
+ }
+
+ /**
+ * TODO This endpoint is not yet documented.
+ *
+ * Display a listing of the resource.
+ *
+ * @return JsonResponse
+ */
+ public function index(): JsonResponse
+ {
+ $bills = $this->repository->getAll();
+ $pageSize = $this->parameters->get('limit');
+ $count = $bills->count();
+ $bills = $bills->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
+ $paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page'));
+ $transformer = new CurrencyTransformer();
+ $transformer->setParameters($this->parameters); // give params to transformer
+
+ return response()
+ ->json($this->jsonApiList('currencies', $paginator, $transformer))
+ ->header('Content-Type', self::CONTENT_TYPE);
+ }
+
+}
diff --git a/app/Api/V2/Controllers/Model/PiggyBank/ShowController.php b/app/Api/V2/Controllers/Model/PiggyBank/IndexController.php
similarity index 97%
rename from app/Api/V2/Controllers/Model/PiggyBank/ShowController.php
rename to app/Api/V2/Controllers/Model/PiggyBank/IndexController.php
index e3a74c16bf..55d73bb84a 100644
--- a/app/Api/V2/Controllers/Model/PiggyBank/ShowController.php
+++ b/app/Api/V2/Controllers/Model/PiggyBank/IndexController.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V2\Controllers\Model\PiggyBank;
use FireflyIII\Api\V2\Controllers\Controller;
+use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\UserGroups\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use FireflyIII\Transformers\V2\PiggyBankTransformer;
@@ -36,7 +37,7 @@ use Illuminate\Pagination\LengthAwarePaginator;
/**
* Class ShowController
*/
-class ShowController extends Controller
+class IndexController extends Controller
{
use ValidatesUserGroupTrait;
diff --git a/app/Models/TransactionCurrency.php b/app/Models/TransactionCurrency.php
index 81bf291294..9825eb5562 100644
--- a/app/Models/TransactionCurrency.php
+++ b/app/Models/TransactionCurrency.php
@@ -109,6 +109,7 @@ class TransactionCurrency extends Model
$currencyId = (int)$value;
$currency = self::find($currencyId);
if (null !== $currency) {
+ $currency->refreshForUser(auth()->user());
return $currency;
}
}
diff --git a/app/Transformers/V2/CurrencyTransformer.php b/app/Transformers/V2/CurrencyTransformer.php
new file mode 100644
index 0000000000..b79b6ee8e4
--- /dev/null
+++ b/app/Transformers/V2/CurrencyTransformer.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * CurrencyTransformer.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\Transformers\V2;
+
+use FireflyIII\Models\TransactionCurrency;
+use Illuminate\Support\Collection;
+
+/**
+ * Class CurrencyTransformer
+ */
+class CurrencyTransformer extends AbstractTransformer
+{
+ /**
+ * @inheritDoc
+ */
+ public function collectMetaData(Collection $objects): void
+ {
+
+ }
+
+ /**
+ * Transform the currency.
+ *
+ * @param TransactionCurrency $currency
+ *
+ * @return array
+ */
+ public function transform(TransactionCurrency $currency): array
+ {
+ return [
+ 'id' => (int)$currency->id,
+ 'created_at' => $currency->created_at->toAtomString(),
+ 'updated_at' => $currency->updated_at->toAtomString(),
+ 'default' => $currency->userDefault,
+ 'enabled' => $currency->userEnabled,
+ 'name' => $currency->name,
+ 'code' => $currency->code,
+ 'symbol' => $currency->symbol,
+ 'decimal_places' => (int)$currency->decimal_places,
+ 'links' => [
+ [
+ 'rel' => 'self',
+ 'uri' => '/currencies/' . $currency->id,
+ ],
+ ],
+ ];
+ }
+}