Skip to content

Firefly III: a personal finances manager

Migrate one of the API endpoints to the new unified API thing.

James Coleffc91d5eadd6
diff --git a/app/Api/V1/Controllers/Autocomplete/AccountController.php b/app/Api/V1/Controllers/Autocomplete/AccountController.php
index 7ab92ca47b..95e74fcba8 100644
--- a/app/Api/V1/Controllers/Autocomplete/AccountController.php
+++ b/app/Api/V1/Controllers/Autocomplete/AccountController.php
@@ -25,7 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
-use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
+use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteApiRequest;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\Exceptions\FireflyException;
@@ -79,20 +79,20 @@ class AccountController extends Controller
* @throws FireflyException
* @throws FireflyException
*/
- public function accounts(AutocompleteRequest $request): JsonResponse
+ public function accounts(AutocompleteApiRequest $request): JsonResponse
{
- $data = $request->getData();
- $types = $data['types'];
- $query = $data['query'];
- $date = $data['date'];
- $return = [];
- $timer = Timer::getInstance();
- $timer->start(sprintf('AC accounts "%s"', $query));
- $result = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit'));
+ $types = $request->attributes->get('types');
+ $query = $request->attributes->get('query');
+ $date = $request->attributes->get('date');
+ $date = $date ?? today(config('app.timezone'));
// set date to end-of-day for account balance. so it is at $date 23:59:59
$date->endOfDay();
+ $return = [];
+ $timer = Timer::getInstance();
+ $timer->start(sprintf('AC accounts "%s"', $query));
+ $result = $this->repository->searchAccount((string)$query, $types, $this->parameters->get('limit'));
$allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary);
/** @var Account $account */
@@ -110,18 +110,18 @@ class AccountController extends Controller
$nameWithBalance = sprintf('%s (%s)', $account->name, Amount::formatAnything($useCurrency, $amount, false));
}
- $return[] = [
- 'id' => (string) $account->id,
+ $return[] = [
+ 'id' => (string)$account->id,
'name' => $account->name,
'name_with_balance' => $nameWithBalance,
'active' => $account->active,
'type' => $account->accountType->type,
- 'currency_id' => (string) $useCurrency->id,
+ 'currency_id' => (string)$useCurrency->id,
'currency_name' => $useCurrency->name,
'currency_code' => $useCurrency->code,
'currency_symbol' => $useCurrency->symbol,
'currency_decimal_places' => $useCurrency->decimal_places,
- 'account_currency_id' => (string) $currency->id,
+ 'account_currency_id' => (string)$currency->id,
'account_currency_name' => $currency->name,
'account_currency_code' => $currency->code,
'account_currency_symbol' => $currency->symbol,
@@ -134,8 +134,8 @@ class AccountController extends Controller
$return,
static function (array $left, array $right) {
$order = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value];
- $posA = (int) array_search($left['type'], $order, true);
- $posB = (int) array_search($right['type'], $order, true);
+ $posA = (int)array_search($left['type'], $order, true);
+ $posB = (int)array_search($right['type'], $order, true);
return $posA - $posB;
}
diff --git a/app/Api/V1/Requests/Autocomplete/AutocompleteApiRequest.php b/app/Api/V1/Requests/Autocomplete/AutocompleteApiRequest.php
new file mode 100644
index 0000000000..dc497e8183
--- /dev/null
+++ b/app/Api/V1/Requests/Autocomplete/AutocompleteApiRequest.php
@@ -0,0 +1,43 @@
+<?php
+/*
+ * AutocompleteApiRequest.php
+ * Copyright (c) 2025 [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\Requests\Autocomplete;
+
+use FireflyIII\Api\V1\Requests\AggregateFormRequest;
+use FireflyIII\Api\V1\Requests\DateRequest;
+use FireflyIII\Api\V1\Requests\Generic\QueryRequest;
+use FireflyIII\Api\V1\Requests\Models\Account\AccountTypeApiRequest;
+use FireflyIII\Api\V1\Requests\Models\Account\AccountTypesApiRequest;
+
+class AutocompleteApiRequest extends AggregateFormRequest
+
+{
+
+ #[\Override]
+ protected function getRequests(): array
+ {
+ return [
+ DateRequest::class,
+ AccountTypesApiRequest::class,
+ QueryRequest::class,
+ ];
+ }
+}
diff --git a/app/Api/V1/Requests/Generic/QueryRequest.php b/app/Api/V1/Requests/Generic/QueryRequest.php
new file mode 100644
index 0000000000..74005871b9
--- /dev/null
+++ b/app/Api/V1/Requests/Generic/QueryRequest.php
@@ -0,0 +1,54 @@
+<?php
+/*
+ * QueryRequest.php
+ * Copyright (c) 2025 [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\Requests\Generic;
+
+use FireflyIII\Api\V1\Requests\ApiRequest;
+use FireflyIII\Support\Request\ChecksLogin;
+use FireflyIII\Support\Request\ConvertsDataTypes;
+use Illuminate\Validation\Validator;
+
+class QueryRequest extends ApiRequest
+{
+ use ChecksLogin;
+ use ConvertsDataTypes;
+
+ public function rules(): array
+ {
+ return [
+ 'query' => sprintf('min:1|max:50|%s', $this->required),
+ ];
+ }
+
+ public function withValidator(Validator $validator): void
+ {
+ $validator->after(
+ function (Validator $validator): void {
+ if ($validator->failed()) {
+ return;
+ }
+ $query = $this->convertString('query');
+ $this->attributes->set('query', $query);
+ }
+ );
+ }
+
+}
diff --git a/app/Api/V1/Requests/Generic/SingleDateRequest.php b/app/Api/V1/Requests/Generic/SingleDateRequest.php
index 14e3c5fc02..02c48b0537 100644
--- a/app/Api/V1/Requests/Generic/SingleDateRequest.php
+++ b/app/Api/V1/Requests/Generic/SingleDateRequest.php
@@ -33,6 +33,7 @@ use Illuminate\Foundation\Http\FormRequest;
* Request class for end points that require a date parameter.
*
* Class SingleDateRequest
+ * @deprecated
*/
class SingleDateRequest extends FormRequest
{
diff --git a/app/Api/V1/Requests/Models/Account/AccountTypesApiRequest.php b/app/Api/V1/Requests/Models/Account/AccountTypesApiRequest.php
new file mode 100644
index 0000000000..638dc284e5
--- /dev/null
+++ b/app/Api/V1/Requests/Models/Account/AccountTypesApiRequest.php
@@ -0,0 +1,56 @@
+<?php
+
+declare(strict_types=1);
+/*
+ * AccountTypeApiRequest.php
+ * Copyright (c) 2025 https://github.com/ctrl-f5
+ *
+ * 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\Requests\Models\Account;
+
+use FireflyIII\Api\V1\Requests\ApiRequest;
+use FireflyIII\Support\Http\Api\AccountFilter;
+use Illuminate\Validation\Validator;
+
+class AccountTypesApiRequest extends ApiRequest
+{
+ use AccountFilter;
+
+ public function rules(): array
+ {
+ return [
+ 'types' => sprintf('in:%s', implode(',', array_keys($this->types))),
+ ];
+ }
+
+ public function withValidator(Validator $validator): void
+ {
+ $validator->after(
+ function (Validator $validator): void {
+ if ($validator->failed()) {
+ return;
+ }
+
+ $type = $this->convertString('types', 'all');
+ $this->attributes->add([
+ 'types' => $this->mapAccountTypes($type),
+ ]);
+ }
+ );
+ }
+}