mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add ability to paginate.
This commit is contained in:
parent
ff80cedd6b
commit
9da10459d6
@ -46,7 +46,7 @@ class AccountController extends Controller
|
|||||||
use Actions\Destroy;
|
use Actions\Destroy;
|
||||||
use Actions\DetachRelationship;
|
use Actions\DetachRelationship;
|
||||||
|
|
||||||
// use Actions\FetchMany;
|
use Actions\FetchMany;
|
||||||
// use Actions\FetchOne;
|
// use Actions\FetchOne;
|
||||||
use Actions\FetchRelated;
|
use Actions\FetchRelated;
|
||||||
use Actions\FetchRelationship;
|
use Actions\FetchRelationship;
|
||||||
@ -66,8 +66,7 @@ class AccountController extends Controller
|
|||||||
->repository()
|
->repository()
|
||||||
->queryAll()
|
->queryAll()
|
||||||
->withRequest($request)
|
->withRequest($request)
|
||||||
->get()
|
->get();
|
||||||
;
|
|
||||||
|
|
||||||
// do something custom...
|
// do something custom...
|
||||||
|
|
||||||
|
@ -62,15 +62,6 @@ class AccountCollectionQuery extends ResourceQuery
|
|||||||
'array',
|
'array',
|
||||||
JsonApiRule::page(),
|
JsonApiRule::page(),
|
||||||
],
|
],
|
||||||
'page.number' => [
|
|
||||||
'integer',
|
|
||||||
'min:1',
|
|
||||||
],
|
|
||||||
|
|
||||||
'page.size' => [
|
|
||||||
'integer',
|
|
||||||
'min:1',
|
|
||||||
],
|
|
||||||
'sort' => [
|
'sort' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'string',
|
'string',
|
||||||
|
@ -19,10 +19,7 @@ class AccountResource extends JsonApiResource
|
|||||||
*/
|
*/
|
||||||
public function id(): string
|
public function id(): string
|
||||||
{
|
{
|
||||||
$id = (string) $this->resource->id;
|
return (string) $this->resource->id;
|
||||||
Log::debug(sprintf('%s: "%s"', __METHOD__, $id));
|
|
||||||
|
|
||||||
return $id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,7 +29,7 @@ class AccountResource extends JsonApiResource
|
|||||||
*/
|
*/
|
||||||
public function attributes($request): iterable
|
public function attributes($request): iterable
|
||||||
{
|
{
|
||||||
Log::debug(__METHOD__);
|
//Log::debug(__METHOD__);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'created_at' => $this->resource->created_at,
|
'created_at' => $this->resource->created_at,
|
||||||
|
@ -12,6 +12,7 @@ use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;
|
|||||||
use LaravelJsonApi\NonEloquent\Fields\Attribute;
|
use LaravelJsonApi\NonEloquent\Fields\Attribute;
|
||||||
use LaravelJsonApi\NonEloquent\Fields\ID;
|
use LaravelJsonApi\NonEloquent\Fields\ID;
|
||||||
use LaravelJsonApi\NonEloquent\Filters\Filter;
|
use LaravelJsonApi\NonEloquent\Filters\Filter;
|
||||||
|
use LaravelJsonApi\NonEloquent\Pagination\EnumerablePagination;
|
||||||
|
|
||||||
class AccountSchema extends Schema
|
class AccountSchema extends Schema
|
||||||
{
|
{
|
||||||
@ -27,7 +28,7 @@ class AccountSchema extends Schema
|
|||||||
*/
|
*/
|
||||||
public function fields(): array
|
public function fields(): array
|
||||||
{
|
{
|
||||||
// Log::debug(__METHOD__);
|
Log::debug(__METHOD__);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
ID::make(),
|
ID::make(),
|
||||||
@ -91,12 +92,20 @@ class AccountSchema extends Schema
|
|||||||
|
|
||||||
public function repository(): AccountRepository
|
public function repository(): AccountRepository
|
||||||
{
|
{
|
||||||
|
Log::debug(__METHOD__);
|
||||||
$this->setUserGroup($this->server->getUsergroup());
|
$this->setUserGroup($this->server->getUsergroup());
|
||||||
$repository = AccountRepository::make()
|
return AccountRepository::make()
|
||||||
->withServer($this->server)
|
->withServer($this->server)
|
||||||
->withSchema($this)
|
->withSchema($this)
|
||||||
->withUserGroup($this->userGroup);
|
->withUserGroup($this->userGroup);
|
||||||
Log::debug(sprintf('%s: %s', __METHOD__, get_class($repository)));
|
}
|
||||||
return $repository;
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function pagination(): EnumerablePagination
|
||||||
|
{
|
||||||
|
Log::debug(__METHOD__);
|
||||||
|
return EnumerablePagination::make();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,9 @@ use FireflyIII\Support\JsonApi\ExpandsQuery;
|
|||||||
use FireflyIII\Support\JsonApi\FiltersPagination;
|
use FireflyIII\Support\JsonApi\FiltersPagination;
|
||||||
use FireflyIII\Support\JsonApi\SortsCollection;
|
use FireflyIII\Support\JsonApi\SortsCollection;
|
||||||
use FireflyIII\Support\JsonApi\ValidateSortParameters;
|
use FireflyIII\Support\JsonApi\ValidateSortParameters;
|
||||||
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use LaravelJsonApi\Contracts\Pagination\Page;
|
||||||
use LaravelJsonApi\Contracts\Store\HasPagination;
|
use LaravelJsonApi\Contracts\Store\HasPagination;
|
||||||
use LaravelJsonApi\NonEloquent\Capabilities\QueryAll;
|
use LaravelJsonApi\NonEloquent\Capabilities\QueryAll;
|
||||||
use LaravelJsonApi\NonEloquent\Concerns\PaginatesEnumerables;
|
use LaravelJsonApi\NonEloquent\Concerns\PaginatesEnumerables;
|
||||||
@ -41,12 +43,12 @@ class AccountQuery extends QueryAll implements HasPagination
|
|||||||
{
|
{
|
||||||
use ExpandsQuery;
|
use ExpandsQuery;
|
||||||
use FiltersPagination;
|
use FiltersPagination;
|
||||||
use PaginatesEnumerables;
|
|
||||||
use SortsCollection;
|
use SortsCollection;
|
||||||
use UsergroupAware;
|
use UsergroupAware;
|
||||||
use ValidateSortParameters;
|
use ValidateSortParameters;
|
||||||
use CollectsCustomParameters;
|
use CollectsCustomParameters;
|
||||||
use AccountFilter;
|
use AccountFilter;
|
||||||
|
//use PaginatesEnumerables;
|
||||||
|
|
||||||
#[\Override]
|
#[\Override]
|
||||||
/**
|
/**
|
||||||
@ -75,11 +77,10 @@ class AccountQuery extends QueryAll implements HasPagination
|
|||||||
// start the query
|
// start the query
|
||||||
$query = $this->userGroup->accounts();
|
$query = $this->userGroup->accounts();
|
||||||
|
|
||||||
// add pagination to the query, limiting the results.
|
// if (!$needsAll) {
|
||||||
if (!$needsAll) {
|
// Log::debug('Does not need full dataset, will paginate.');
|
||||||
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(Account::class, $query, $sort);
|
$query = $this->addSortParams(Account::class, $query, $sort);
|
||||||
@ -87,17 +88,40 @@ class AccountQuery extends QueryAll implements HasPagination
|
|||||||
|
|
||||||
// collect the result.
|
// collect the result.
|
||||||
$collection = $query->get(['accounts.*']);
|
$collection = $query->get(['accounts.*']);
|
||||||
|
// sort the data after the query, and return it right away.
|
||||||
|
$sorted = $this->sortCollection(Account::class, $collection, $sort);
|
||||||
|
|
||||||
// enrich the collected data
|
// take from the collection the filtered page + page number:
|
||||||
|
$currentPage = $sorted->skip($pagination['number'] - 1 * $pagination['size'])->take($pagination['size']);
|
||||||
|
|
||||||
|
// enrich the current page.
|
||||||
$enrichment = new AccountEnrichment();
|
$enrichment = new AccountEnrichment();
|
||||||
$enrichment->setStart($otherParams['start'] ?? null);
|
$enrichment->setStart($otherParams['start'] ?? null);
|
||||||
$enrichment->setEnd($otherParams['end'] ?? null);
|
$enrichment->setEnd($otherParams['end'] ?? null);
|
||||||
$collection = $enrichment->enrich($collection);
|
$currentPage = $enrichment->enrich($currentPage);
|
||||||
|
|
||||||
// TODO add filters after the query, if there are filters that cannot be applied to the database
|
// TODO add filters after the query, if there are filters that cannot be applied to the database
|
||||||
// TODO same for sort things.
|
// TODO same for sort things.
|
||||||
|
|
||||||
// sort the data after the query, and return it right away.
|
|
||||||
return $this->sortCollection(Account::class, $collection, $sort);
|
return new LengthAwarePaginator($currentPage,$sorted->count(),$pagination['size'],$pagination['number']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
#[\Override] public function paginate(array $page): Page
|
||||||
|
{
|
||||||
|
die('here weare');
|
||||||
|
// TODO: Implement paginate() method.
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
#[\Override] public function getOrPaginate(?array $page): iterable
|
||||||
|
{
|
||||||
|
die('here weare');
|
||||||
|
// TODO: Implement getOrPaginate() method.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -337,8 +337,12 @@ let index = function () {
|
|||||||
},
|
},
|
||||||
loadAccounts() {
|
loadAccounts() {
|
||||||
this.pageOptions.isLoading = true;
|
this.pageOptions.isLoading = true;
|
||||||
// sort instructions
|
// sort instructions (only one column)
|
||||||
const sorting = [{column: this.pageOptions.sortingColumn, direction: this.pageOptions.sortDirection}];
|
let sorting = this.pageOptions.sortingColumn;
|
||||||
|
if('ASC' === this.pageOptions.sortDirection) {
|
||||||
|
sorting = '-' + sorting;
|
||||||
|
}
|
||||||
|
//const sorting = [{column: this.pageOptions.sortingColumn, direction: this.pageOptions.sortDirection}];
|
||||||
|
|
||||||
// filter instructions
|
// filter instructions
|
||||||
let filters = [];
|
let filters = [];
|
||||||
@ -356,11 +360,11 @@ let index = function () {
|
|||||||
let params = {
|
let params = {
|
||||||
sorting: sorting,
|
sorting: sorting,
|
||||||
filters: filters,
|
filters: filters,
|
||||||
today: today,
|
// today: today,
|
||||||
type: type,
|
// type: type,
|
||||||
page: this.page,
|
page: {number: this.page},
|
||||||
start: start,
|
startPeriod: start,
|
||||||
end: end
|
endPeriod: end
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!this.tableColumns.balance_difference.enabled) {
|
if (!this.tableColumns.balance_difference.enabled) {
|
||||||
@ -371,7 +375,8 @@ let index = function () {
|
|||||||
let groupedAccounts = {};
|
let groupedAccounts = {};
|
||||||
// one page only.o
|
// one page only.o
|
||||||
(new Get()).index(params).then(response => {
|
(new Get()).index(params).then(response => {
|
||||||
this.totalPages = response.meta.pagination.total_pages;
|
console.log(response);
|
||||||
|
this.totalPages = response.meta.lastPage;
|
||||||
for (let i = 0; i < response.data.length; i++) {
|
for (let i = 0; i < response.data.length; i++) {
|
||||||
if (response.data.hasOwnProperty(i)) {
|
if (response.data.hasOwnProperty(i)) {
|
||||||
let current = response.data[i];
|
let current = response.data[i];
|
||||||
|
Loading…
Reference in New Issue
Block a user