Add ability to paginate.

This commit is contained in:
James Cole 2024-08-03 07:28:56 +02:00
parent ff80cedd6b
commit 9da10459d6
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
6 changed files with 64 additions and 39 deletions

View File

@ -46,7 +46,7 @@ class AccountController extends Controller
use Actions\Destroy;
use Actions\DetachRelationship;
// use Actions\FetchMany;
use Actions\FetchMany;
// use Actions\FetchOne;
use Actions\FetchRelated;
use Actions\FetchRelationship;
@ -66,8 +66,7 @@ class AccountController extends Controller
->repository()
->queryAll()
->withRequest($request)
->get()
;
->get();
// do something custom...

View File

@ -62,15 +62,6 @@ class AccountCollectionQuery extends ResourceQuery
'array',
JsonApiRule::page(),
],
'page.number' => [
'integer',
'min:1',
],
'page.size' => [
'integer',
'min:1',
],
'sort' => [
'nullable',
'string',

View File

@ -19,10 +19,7 @@ class AccountResource extends JsonApiResource
*/
public function id(): string
{
$id = (string) $this->resource->id;
Log::debug(sprintf('%s: "%s"', __METHOD__, $id));
return $id;
return (string) $this->resource->id;
}
/**
@ -32,7 +29,7 @@ class AccountResource extends JsonApiResource
*/
public function attributes($request): iterable
{
Log::debug(__METHOD__);
//Log::debug(__METHOD__);
return [
'created_at' => $this->resource->created_at,

View File

@ -12,6 +12,7 @@ use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;
use LaravelJsonApi\NonEloquent\Fields\Attribute;
use LaravelJsonApi\NonEloquent\Fields\ID;
use LaravelJsonApi\NonEloquent\Filters\Filter;
use LaravelJsonApi\NonEloquent\Pagination\EnumerablePagination;
class AccountSchema extends Schema
{
@ -27,7 +28,7 @@ class AccountSchema extends Schema
*/
public function fields(): array
{
// Log::debug(__METHOD__);
Log::debug(__METHOD__);
return [
ID::make(),
@ -91,12 +92,20 @@ class AccountSchema extends Schema
public function repository(): AccountRepository
{
Log::debug(__METHOD__);
$this->setUserGroup($this->server->getUsergroup());
$repository = AccountRepository::make()
return AccountRepository::make()
->withServer($this->server)
->withSchema($this)
->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();
}
}

View File

@ -32,7 +32,9 @@ use FireflyIII\Support\JsonApi\ExpandsQuery;
use FireflyIII\Support\JsonApi\FiltersPagination;
use FireflyIII\Support\JsonApi\SortsCollection;
use FireflyIII\Support\JsonApi\ValidateSortParameters;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Contracts\Pagination\Page;
use LaravelJsonApi\Contracts\Store\HasPagination;
use LaravelJsonApi\NonEloquent\Capabilities\QueryAll;
use LaravelJsonApi\NonEloquent\Concerns\PaginatesEnumerables;
@ -41,12 +43,12 @@ class AccountQuery extends QueryAll implements HasPagination
{
use ExpandsQuery;
use FiltersPagination;
use PaginatesEnumerables;
use SortsCollection;
use UsergroupAware;
use ValidateSortParameters;
use CollectsCustomParameters;
use AccountFilter;
//use PaginatesEnumerables;
#[\Override]
/**
@ -75,11 +77,10 @@ class AccountQuery extends QueryAll implements HasPagination
// start the query
$query = $this->userGroup->accounts();
// add pagination to the query, limiting the results.
if (!$needsAll) {
Log::debug('Need full dataset');
$query = $this->addPagination($query, $pagination);
}
// if (!$needsAll) {
// Log::debug('Does not need full dataset, will paginate.');
// $query = $this->addPagination($query, $pagination);
// }
// add sort and filter parameters to the query.
$query = $this->addSortParams(Account::class, $query, $sort);
@ -87,17 +88,40 @@ class AccountQuery extends QueryAll implements HasPagination
// collect the result.
$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->setStart($otherParams['start'] ?? 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 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.
}
}

View File

@ -337,8 +337,12 @@ let index = function () {
},
loadAccounts() {
this.pageOptions.isLoading = true;
// sort instructions
const sorting = [{column: this.pageOptions.sortingColumn, direction: this.pageOptions.sortDirection}];
// sort instructions (only one column)
let sorting = this.pageOptions.sortingColumn;
if('ASC' === this.pageOptions.sortDirection) {
sorting = '-' + sorting;
}
//const sorting = [{column: this.pageOptions.sortingColumn, direction: this.pageOptions.sortDirection}];
// filter instructions
let filters = [];
@ -356,11 +360,11 @@ let index = function () {
let params = {
sorting: sorting,
filters: filters,
today: today,
type: type,
page: this.page,
start: start,
end: end
// today: today,
// type: type,
page: {number: this.page},
startPeriod: start,
endPeriod: end
};
if (!this.tableColumns.balance_difference.enabled) {
@ -371,7 +375,8 @@ let index = function () {
let groupedAccounts = {};
// one page only.o
(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++) {
if (response.data.hasOwnProperty(i)) {
let current = response.data[i];