mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 02:45:58 +00:00
Fix sort params
This commit is contained in:
@@ -35,13 +35,13 @@ class AccountSchema extends Schema
|
|||||||
Attribute::make('updated_at'),
|
Attribute::make('updated_at'),
|
||||||
|
|
||||||
// basic info and meta data
|
// basic info and meta data
|
||||||
Attribute::make('name'),
|
Attribute::make('name')->sortable(),
|
||||||
Attribute::make('active'),
|
Attribute::make('active')->sortable(),
|
||||||
Attribute::make('order'),
|
Attribute::make('order')->sortable(),
|
||||||
Attribute::make('iban'),
|
Attribute::make('iban')->sortable(),
|
||||||
Attribute::make('type'),
|
Attribute::make('type'),
|
||||||
Attribute::make('account_role'),
|
Attribute::make('account_role'),
|
||||||
Attribute::make('account_number'),
|
Attribute::make('account_number')->sortable(),
|
||||||
|
|
||||||
// currency
|
// currency
|
||||||
Attribute::make('currency_id'),
|
Attribute::make('currency_id'),
|
||||||
@@ -52,17 +52,18 @@ class AccountSchema extends Schema
|
|||||||
Attribute::make('is_multi_currency'),
|
Attribute::make('is_multi_currency'),
|
||||||
|
|
||||||
// balance
|
// balance
|
||||||
Attribute::make('balance'),
|
Attribute::make('balance')->sortable(),
|
||||||
Attribute::make('native_balance'),
|
Attribute::make('native_balance')->sortable(),
|
||||||
|
|
||||||
// liability things
|
// liability things
|
||||||
Attribute::make('liability_direction'),
|
Attribute::make('liability_direction'),
|
||||||
Attribute::make('interest'),
|
Attribute::make('interest'),
|
||||||
Attribute::make('interest_period'),
|
Attribute::make('interest_period'),
|
||||||
Attribute::make('current_debt'),
|
Attribute::make('current_debt')->sortable(),
|
||||||
|
|
||||||
// dynamic data
|
// dynamic data
|
||||||
Attribute::make('last_activity'),
|
Attribute::make('last_activity'),
|
||||||
|
Attribute::make('balance_difference')->sortable(), // only used for sort.
|
||||||
|
|
||||||
// group
|
// group
|
||||||
Attribute::make('object_group_id'),
|
Attribute::make('object_group_id'),
|
||||||
|
@@ -62,6 +62,7 @@ class AccountQuery extends QueryAll implements HasPagination
|
|||||||
|
|
||||||
// collect sort options
|
// collect sort options
|
||||||
$sort = $this->queryParameters->sortFields();
|
$sort = $this->queryParameters->sortFields();
|
||||||
|
|
||||||
// collect pagination based on the page
|
// collect pagination based on the page
|
||||||
$pagination = $this->filtersPagination($this->queryParameters->page());
|
$pagination = $this->filtersPagination($this->queryParameters->page());
|
||||||
// check if we need all accounts, regardless of pagination
|
// check if we need all accounts, regardless of pagination
|
||||||
@@ -76,14 +77,14 @@ class AccountQuery extends QueryAll implements HasPagination
|
|||||||
|
|
||||||
// add pagination to the query, limiting the results.
|
// add pagination to the query, limiting the results.
|
||||||
if (!$needsAll) {
|
if (!$needsAll) {
|
||||||
|
Log::debug('Need full dataset');
|
||||||
$query = $this->addPagination($query, $pagination);
|
$query = $this->addPagination($query, $pagination);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add sort and filter parameters to the query.
|
// add sort and filter parameters to the query.
|
||||||
$query = $this->addSortParams($query, $sort);
|
$query = $this->addSortParams(Account::class, $query, $sort);
|
||||||
$query = $this->addFilterParams(Account::class, $query, $filters);
|
$query = $this->addFilterParams(Account::class, $query, $filters);
|
||||||
|
|
||||||
|
|
||||||
// collect the result.
|
// collect the result.
|
||||||
$collection = $query->get(['accounts.*']);
|
$collection = $query->get(['accounts.*']);
|
||||||
|
|
||||||
@@ -93,10 +94,10 @@ class AccountQuery extends QueryAll implements HasPagination
|
|||||||
$enrichment->setEnd($otherParams['end'] ?? null);
|
$enrichment->setEnd($otherParams['end'] ?? null);
|
||||||
$collection = $enrichment->enrich($collection);
|
$collection = $enrichment->enrich($collection);
|
||||||
|
|
||||||
// TODO add filters after the query, if there are filters that cannot be applied to the database but only
|
// TODO add filters after the query, if there are filters that cannot be applied to the database
|
||||||
// to the enriched results.
|
// TODO same for sort things.
|
||||||
|
|
||||||
// sort the data after the query, and return it right away.
|
// sort the data after the query, and return it right away.
|
||||||
return $this->sortCollection($collection, $sort);
|
return $this->sortCollection(Account::class, $collection, $sort);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -41,14 +41,17 @@ trait ExpandsQuery
|
|||||||
return $query->skip($skip)->take($pagination['size']);
|
return $query->skip($skip)->take($pagination['size']);
|
||||||
}
|
}
|
||||||
|
|
||||||
final protected function addSortParams(Builder $query, ?SortFields $sort): Builder
|
final protected function addSortParams(string $class, Builder $query, ?SortFields $sort): Builder
|
||||||
{
|
{
|
||||||
|
$config = config('api.valid_query_sort')[$class] ?? [];
|
||||||
if (null === $sort) {
|
if (null === $sort) {
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
foreach ($sort->all() as $sortField) {
|
foreach ($sort->all() as $sortField) {
|
||||||
|
if (in_array($sortField->name(), $config, true)) {
|
||||||
$query->orderBy($sortField->name(), $sortField->isAscending() ? 'ASC' : 'DESC');
|
$query->orderBy($sortField->name(), $sortField->isAscending() ? 'ASC' : 'DESC');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
@@ -24,18 +24,24 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Support\JsonApi;
|
namespace FireflyIII\Support\JsonApi;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use LaravelJsonApi\Core\Query\SortFields;
|
use LaravelJsonApi\Core\Query\SortFields;
|
||||||
|
|
||||||
trait SortsCollection
|
trait SortsCollection
|
||||||
{
|
{
|
||||||
protected function sortCollection(Collection $collection, ?SortFields $sortFields): Collection
|
protected function sortCollection(string $class, Collection $collection, ?SortFields $sortFields): Collection
|
||||||
{
|
{
|
||||||
|
Log::debug(__METHOD__);
|
||||||
|
$config = config('api.valid_api_sort')[$class] ?? [];
|
||||||
if (null === $sortFields) {
|
if (null === $sortFields) {
|
||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
foreach ($sortFields->all() as $sortField) {
|
foreach ($sortFields->all() as $sortField) {
|
||||||
|
if (in_array($sortField->name(), $config, true)) {
|
||||||
|
Log::debug(sprintf('Sort collection by "%s"', $sortField->name()));
|
||||||
$collection = $sortField->isAscending() ? $collection->sortBy($sortField->name()) : $collection->sortByDesc($sortField->name());
|
$collection = $sortField->isAscending() ? $collection->sortBy($sortField->name()) : $collection->sortByDesc($sortField->name());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
@@ -23,12 +23,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Support\JsonApi;
|
namespace FireflyIII\Support\JsonApi;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use LaravelJsonApi\Core\Query\SortFields;
|
use LaravelJsonApi\Core\Query\SortFields;
|
||||||
|
|
||||||
trait ValidateSortParameters
|
trait ValidateSortParameters
|
||||||
{
|
{
|
||||||
public function needsFullDataset(string $class, ?SortFields $params): bool
|
public function needsFullDataset(string $class, ?SortFields $params): bool
|
||||||
{
|
{
|
||||||
|
Log::debug(__METHOD__);
|
||||||
if (null === $params) {
|
if (null === $params) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -37,6 +39,7 @@ trait ValidateSortParameters
|
|||||||
|
|
||||||
foreach ($params->all() as $field) {
|
foreach ($params->all() as $field) {
|
||||||
if (in_array($field->name(), $config, true)) {
|
if (in_array($field->name(), $config, true)) {
|
||||||
|
Log::debug('TRUE');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -43,14 +43,21 @@ return [
|
|||||||
'sorting' => [
|
'sorting' => [
|
||||||
'allowed' => [
|
'allowed' => [
|
||||||
'transactions' => ['description', 'amount'],
|
'transactions' => ['description', 'amount'],
|
||||||
'accounts' => ['name', 'active', 'iban', 'balance', 'last_activity', 'balance_difference', 'current_debt'],
|
'accounts' => ['name', 'active', 'iban', 'order', 'account_number', 'balance', 'last_activity', 'balance_difference', 'current_debt'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
// valid query columns for sorting:
|
||||||
|
'valid_query_sort' => [
|
||||||
|
Account::class => ['id','name', 'active', 'iban', 'order'],
|
||||||
|
],
|
||||||
|
'valid_api_sort' => [
|
||||||
|
Account::class => [],
|
||||||
|
],
|
||||||
'full_data_set' => [
|
'full_data_set' => [
|
||||||
'account' => ['last_activity', 'balance_difference', 'current_balance', 'current_debt'],
|
'account' => ['last_activity', 'balance_difference', 'current_balance', 'current_debt'],
|
||||||
],
|
],
|
||||||
'valid_query_filters' => [
|
'valid_query_filters' => [
|
||||||
Account::class => ['id','name', 'iban', 'active'],
|
Account::class => ['id', 'name', 'iban', 'active'],
|
||||||
],
|
],
|
||||||
'valid_api_filters' => [
|
'valid_api_filters' => [
|
||||||
Account::class => ['id', 'name', 'iban', 'active', 'type'],
|
Account::class => ['id', 'name', 'iban', 'active', 'type'],
|
||||||
|
Reference in New Issue
Block a user