mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Add ability to paginate.
This commit is contained in:
		| @@ -62,15 +62,6 @@ class AccountCollectionQuery extends ResourceQuery | ||||
|                 'array', | ||||
|                 JsonApiRule::page(), | ||||
|             ], | ||||
|             'page.number' => [ | ||||
|                 'integer', | ||||
|                 'min:1', | ||||
|             ], | ||||
| 
 | ||||
|             'page.size' => [ | ||||
|                 'integer', | ||||
|                 'min:1', | ||||
|             ], | ||||
|             'sort'          => [ | ||||
|                 'nullable', | ||||
|                 'string', | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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.
 | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user