mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-11 23:28:54 +00:00
Add new request in style of aggregated requests, fix issue with missing field.
This commit is contained in:
@@ -28,6 +28,7 @@ use Carbon\Carbon;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Api\V1\Controllers\Controller;
|
use FireflyIII\Api\V1\Controllers\Controller;
|
||||||
use FireflyIII\Api\V1\Requests\DateRangeRequest;
|
use FireflyIII\Api\V1\Requests\DateRangeRequest;
|
||||||
|
use FireflyIII\Api\V1\Requests\Summary\BasicRequest;
|
||||||
use FireflyIII\Enums\AccountTypeEnum;
|
use FireflyIII\Enums\AccountTypeEnum;
|
||||||
use FireflyIII\Enums\TransactionTypeEnum;
|
use FireflyIII\Enums\TransactionTypeEnum;
|
||||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||||
@@ -88,19 +89,10 @@ class BasicController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function basic(BasicRequest $request): JsonResponse
|
||||||
* 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
|
|
||||||
{
|
{
|
||||||
// parameters for boxes:
|
// parameters for boxes:
|
||||||
$dates = $request->attributes->all();
|
['start' => $start, 'end' => $end, 'code' => $code] = $request->attributes->all();
|
||||||
$start = $dates['start'];
|
|
||||||
$end = $dates['end'];
|
|
||||||
$code = $request->get('currency_code');
|
|
||||||
// balance information:
|
// balance information:
|
||||||
$balanceData = $this->getBalanceInformation($start, $end);
|
$balanceData = $this->getBalanceInformation($start, $end);
|
||||||
$billData = $this->getSubscriptionInformation($start, $end);
|
$billData = $this->getSubscriptionInformation($start, $end);
|
||||||
@@ -115,7 +107,7 @@ class BasicController extends Controller
|
|||||||
// give new keys
|
// give new keys
|
||||||
$return = [];
|
$return = [];
|
||||||
foreach ($total as $entry) {
|
foreach ($total as $entry) {
|
||||||
if (null === $code || ($code === $entry['currency_code'])) {
|
if ('' === $code || ($code === $entry['currency_code'])) {
|
||||||
$return[$entry['key']] = $entry;
|
$return[$entry['key']] = $entry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -25,6 +25,7 @@ namespace FireflyIII\Api\V1\Requests;
|
|||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Validation\Validator;
|
use Illuminate\Validation\Validator;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
@@ -43,12 +44,14 @@ abstract class AggregateFormRequest extends ApiRequest
|
|||||||
parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||||
|
|
||||||
// instantiate all subrequests and share current requests' bags with them
|
// instantiate all subrequests and share current requests' bags with them
|
||||||
|
Log::debug('Initializing AggregateFormRequest.');
|
||||||
foreach ($this->getRequests() as $config) {
|
foreach ($this->getRequests() as $config) {
|
||||||
$requestClass = is_array($config) ? array_shift($config) : $config;
|
$requestClass = is_array($config) ? array_shift($config) : $config;
|
||||||
|
|
||||||
if (!is_a($requestClass, Request::class, true)) {
|
if (!is_a($requestClass, Request::class, true)) {
|
||||||
throw new RuntimeException('getRequests() must return class-strings of subclasses of Request');
|
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 = $this->requests[] = new $requestClass();
|
||||||
$instance->request = $this->request;
|
$instance->request = $this->request;
|
||||||
@@ -63,6 +66,7 @@ abstract class AggregateFormRequest extends ApiRequest
|
|||||||
$instance->handleConfig(is_array($config) ? $config : []);
|
$instance->handleConfig(is_array($config) ? $config : []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log::debug('Done initializing AggregateFormRequest.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
@@ -85,6 +89,7 @@ abstract class AggregateFormRequest extends ApiRequest
|
|||||||
// register all subrequests' validators
|
// register all subrequests' validators
|
||||||
foreach ($this->requests as $request) {
|
foreach ($this->requests as $request) {
|
||||||
if (method_exists($request, 'withValidator')) {
|
if (method_exists($request, 'withValidator')) {
|
||||||
|
Log::debug(sprintf('Process withValidator from class %s', get_class($request)));
|
||||||
$request->withValidator($validator);
|
$request->withValidator($validator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -39,8 +39,6 @@ class DateRangeRequest extends ApiRequest
|
|||||||
{
|
{
|
||||||
$validator->after(
|
$validator->after(
|
||||||
function (Validator $validator): void {
|
function (Validator $validator): void {
|
||||||
$this->attributes->set('start', null);
|
|
||||||
$this->attributes->set('end', null);
|
|
||||||
if (!$validator->valid()) {
|
if (!$validator->valid()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* CurrencyCodeRequest.php
|
||||||
|
* Copyright (c) 2025 james@firefly-iii.org
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
37
app/Api/V1/Requests/Summary/BasicRequest.php
Normal file
37
app/Api/V1/Requests/Summary/BasicRequest.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* BasicRequest.php
|
||||||
|
* Copyright (c) 2025 james@firefly-iii.org
|
||||||
|
*
|
||||||
|
* 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user