Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Add new request in style of aggregated requests, fix issue with missing field.
James Cole1870345ddf15
diff --git a/app/Api/V1/Controllers/Summary/BasicController.php b/app/Api/V1/Controllers/Summary/BasicController.phpindex 6822674139..9c44083daa 100644--- a/app/Api/V1/Controllers/Summary/BasicController.php+++ b/app/Api/V1/Controllers/Summary/BasicController.php@@ -28,6 +28,7 @@ use Carbon\Carbon;use Exception;use FireflyIII\Api\V1\Controllers\Controller;use FireflyIII\Api\V1\Requests\DateRangeRequest;+use FireflyIII\Api\V1\Requests\Summary\BasicRequest;use FireflyIII\Enums\AccountTypeEnum;use FireflyIII\Enums\TransactionTypeEnum;use FireflyIII\Helpers\Collector\GroupCollectorInterface;@@ -88,19 +89,10 @@ class BasicController extends Controller);}- /**- * This endpoint is documented at:- * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/summary/getBasicSummary- *- * @throws Exception- */- public function basic(DateRangeRequest $request): JsonResponse+ public function basic(BasicRequest $request): JsonResponse{// parameters for boxes:- $dates = $request->attributes->all();- $start = $dates['start'];- $end = $dates['end'];- $code = $request->get('currency_code');+ ['start' => $start, 'end' => $end, 'code' => $code] = $request->attributes->all();// balance information:$balanceData = $this->getBalanceInformation($start, $end);$billData = $this->getSubscriptionInformation($start, $end);@@ -115,7 +107,7 @@ class BasicController extends Controller// give new keys$return = [];foreach ($total as $entry) {- if (null === $code || ($code === $entry['currency_code'])) {+ if ('' === $code || ($code === $entry['currency_code'])) {$return[$entry['key']] = $entry;}}diff --git a/app/Api/V1/Requests/AggregateFormRequest.php b/app/Api/V1/Requests/AggregateFormRequest.phpindex 85579fffc3..001c902fda 100644--- a/app/Api/V1/Requests/AggregateFormRequest.php+++ b/app/Api/V1/Requests/AggregateFormRequest.php@@ -25,6 +25,7 @@ namespace FireflyIII\Api\V1\Requests;use Illuminate\Foundation\Http\FormRequest;use Illuminate\Http\Request;+use Illuminate\Support\Facades\Log;use Illuminate\Validation\Validator;use RuntimeException;@@ -43,12 +44,14 @@ abstract class AggregateFormRequest extends ApiRequestparent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);// instantiate all subrequests and share current requests' bags with them+ Log::debug('Initializing AggregateFormRequest.');foreach ($this->getRequests() as $config) {$requestClass = is_array($config) ? array_shift($config) : $config;if (!is_a($requestClass, Request::class, true)) {throw new RuntimeException('getRequests() must return class-strings of subclasses of Request');}+ Log::debug(sprintf('Initializing subrequest %s', $requestClass));$instance = $this->requests[] = new $requestClass();$instance->request = $this->request;@@ -63,6 +66,7 @@ abstract class AggregateFormRequest extends ApiRequest$instance->handleConfig(is_array($config) ? $config : []);}}+ Log::debug('Done initializing AggregateFormRequest.');}public function rules(): array@@ -85,6 +89,7 @@ abstract class AggregateFormRequest extends ApiRequest// register all subrequests' validatorsforeach ($this->requests as $request) {if (method_exists($request, 'withValidator')) {+ Log::debug(sprintf('Process withValidator from class %s', get_class($request)));$request->withValidator($validator);}}diff --git a/app/Api/V1/Requests/DateRangeRequest.php b/app/Api/V1/Requests/DateRangeRequest.phpindex a2e3738489..831e7b625c 100644--- a/app/Api/V1/Requests/DateRangeRequest.php+++ b/app/Api/V1/Requests/DateRangeRequest.php@@ -39,8 +39,6 @@ class DateRangeRequest extends ApiRequest{$validator->after(function (Validator $validator): void {- $this->attributes->set('start', null);- $this->attributes->set('end', null);if (!$validator->valid()) {return;}diff --git a/app/Api/V1/Requests/Models/TransactionCurrency/CurrencyCodeRequest.php b/app/Api/V1/Requests/Models/TransactionCurrency/CurrencyCodeRequest.phpnew file mode 100644index 0000000000..56d434b57a--- /dev/null+++ b/app/Api/V1/Requests/Models/TransactionCurrency/CurrencyCodeRequest.php@@ -0,0 +1,49 @@+<?php+/*+ * CurrencyCodeRequest.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\Models\TransactionCurrency;++use FireflyIII\Api\V1\Requests\ApiRequest;+use Illuminate\Validation\Validator;++class CurrencyCodeRequest extends ApiRequest+{++ public function rules(): array+ {+ return [+ 'code' => sprintf('exists:transaction_currencies,code|%s', $this->required),+ ];+ }++ public function withValidator(Validator $validator): void+ {+ $validator->after(+ function (Validator $validator): void {+ if (!$validator->valid()) {+ return;+ }+ $code = $this->convertString('code', '');+ $this->attributes->set('code', $code);+ }+ );+ }+}diff --git a/app/Api/V1/Requests/Summary/BasicRequest.php b/app/Api/V1/Requests/Summary/BasicRequest.phpnew file mode 100644index 0000000000..0725b129c9--- /dev/null+++ b/app/Api/V1/Requests/Summary/BasicRequest.php@@ -0,0 +1,37 @@+<?php+/*+ * BasicRequest.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\Summary;++use FireflyIII\Api\V1\Requests\AggregateFormRequest;+use FireflyIII\Api\V1\Requests\DateRangeRequest;+use FireflyIII\Api\V1\Requests\Models\TransactionCurrency\CurrencyCodeRequest;++class BasicRequest extends AggregateFormRequest+{+ protected function getRequests(): array+ {+ return [+ [DateRangeRequest::class, 'required'],+ CurrencyCodeRequest::class,+ ];+ }+}