mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-16 14:48:11 +00:00
Migrate one of the API endpoints to the new unified API thing.
This commit is contained in:
@@ -25,7 +25,7 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Api\V1\Controllers\Autocomplete;
|
namespace FireflyIII\Api\V1\Controllers\Autocomplete;
|
||||||
|
|
||||||
use FireflyIII\Api\V1\Controllers\Controller;
|
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\AccountTypeEnum;
|
||||||
use FireflyIII\Enums\UserRoleEnum;
|
use FireflyIII\Enums\UserRoleEnum;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
@@ -79,20 +79,20 @@ class AccountController extends Controller
|
|||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function accounts(AutocompleteRequest $request): JsonResponse
|
public function accounts(AutocompleteApiRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$data = $request->getData();
|
$types = $request->attributes->get('types');
|
||||||
$types = $data['types'];
|
$query = $request->attributes->get('query');
|
||||||
$query = $data['query'];
|
$date = $request->attributes->get('date');
|
||||||
$date = $data['date'];
|
$date = $date ?? today(config('app.timezone'));
|
||||||
$return = [];
|
|
||||||
$timer = Timer::getInstance();
|
|
||||||
$timer->start(sprintf('AC accounts "%s"', $query));
|
|
||||||
$result = $this->repository->searchAccount((string) $query, $types, $this->parameters->get('limit'));
|
|
||||||
|
|
||||||
// set date to end-of-day for account balance. so it is at $date 23:59:59
|
// set date to end-of-day for account balance. so it is at $date 23:59:59
|
||||||
$date->endOfDay();
|
$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);
|
$allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary);
|
||||||
|
|
||||||
/** @var Account $account */
|
/** @var Account $account */
|
||||||
@@ -110,18 +110,18 @@ class AccountController extends Controller
|
|||||||
$nameWithBalance = sprintf('%s (%s)', $account->name, Amount::formatAnything($useCurrency, $amount, false));
|
$nameWithBalance = sprintf('%s (%s)', $account->name, Amount::formatAnything($useCurrency, $amount, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
$return[] = [
|
$return[] = [
|
||||||
'id' => (string) $account->id,
|
'id' => (string)$account->id,
|
||||||
'name' => $account->name,
|
'name' => $account->name,
|
||||||
'name_with_balance' => $nameWithBalance,
|
'name_with_balance' => $nameWithBalance,
|
||||||
'active' => $account->active,
|
'active' => $account->active,
|
||||||
'type' => $account->accountType->type,
|
'type' => $account->accountType->type,
|
||||||
'currency_id' => (string) $useCurrency->id,
|
'currency_id' => (string)$useCurrency->id,
|
||||||
'currency_name' => $useCurrency->name,
|
'currency_name' => $useCurrency->name,
|
||||||
'currency_code' => $useCurrency->code,
|
'currency_code' => $useCurrency->code,
|
||||||
'currency_symbol' => $useCurrency->symbol,
|
'currency_symbol' => $useCurrency->symbol,
|
||||||
'currency_decimal_places' => $useCurrency->decimal_places,
|
'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_name' => $currency->name,
|
||||||
'account_currency_code' => $currency->code,
|
'account_currency_code' => $currency->code,
|
||||||
'account_currency_symbol' => $currency->symbol,
|
'account_currency_symbol' => $currency->symbol,
|
||||||
@@ -134,8 +134,8 @@ class AccountController extends Controller
|
|||||||
$return,
|
$return,
|
||||||
static function (array $left, array $right) {
|
static function (array $left, array $right) {
|
||||||
$order = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value];
|
$order = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value];
|
||||||
$posA = (int) array_search($left['type'], $order, true);
|
$posA = (int)array_search($left['type'], $order, true);
|
||||||
$posB = (int) array_search($right['type'], $order, true);
|
$posB = (int)array_search($right['type'], $order, true);
|
||||||
|
|
||||||
return $posA - $posB;
|
return $posA - $posB;
|
||||||
}
|
}
|
||||||
|
|||||||
43
app/Api/V1/Requests/Autocomplete/AutocompleteApiRequest.php
Normal file
43
app/Api/V1/Requests/Autocomplete/AutocompleteApiRequest.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* AutocompleteApiRequest.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\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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
54
app/Api/V1/Requests/Generic/QueryRequest.php
Normal file
54
app/Api/V1/Requests/Generic/QueryRequest.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* QueryRequest.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\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);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ use Illuminate\Foundation\Http\FormRequest;
|
|||||||
* Request class for end points that require a date parameter.
|
* Request class for end points that require a date parameter.
|
||||||
*
|
*
|
||||||
* Class SingleDateRequest
|
* Class SingleDateRequest
|
||||||
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
class SingleDateRequest extends FormRequest
|
class SingleDateRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user