diff --git a/extend.php b/extend.php index ae3ea819..8b7b50c2 100755 --- a/extend.php +++ b/extend.php @@ -37,6 +37,7 @@ (new Extend\Routes('api')) ->post('/fof/polls', 'fof.polls.create', Controllers\CreatePollController::class) + ->get('/fof/polls', 'fof.polls.index', Controllers\ListPollsController::class) ->get('/fof/polls/{id}', 'fof.polls.show', Controllers\ShowPollController::class) ->patch('/fof/polls/{id}', 'fof.polls.edit', Controllers\EditPollController::class) ->delete('/fof/polls/{id}', 'fof.polls.delete', Controllers\DeletePollController::class) diff --git a/src/Api/Controllers/ListPollsController.php b/src/Api/Controllers/ListPollsController.php new file mode 100644 index 00000000..e744b644 --- /dev/null +++ b/src/Api/Controllers/ListPollsController.php @@ -0,0 +1,70 @@ +url = $url; + } + + public function data(ServerRequestInterface $request, Document $document): Collection + { + $actor = RequestUtil::getActor($request); + + // Not yet needed, but here if/when we do. + // $filters = $this->extractFilter($request); + // $sort = $this->extractSort($request); + // $sortIsDefault = $this->sortIsDefault($request); + + $limit = $this->extractLimit($request); + $offset = $this->extractOffset($request); + $include = $this->extractInclude($request); + + $results = Poll::query() + ->select('polls.*') + ->whereVisibleTo($actor) + ->orderBy('id') + ->skip($offset) + ->take($limit); + + $totalItems = $results->count(); + $results = $results->get(); + + $document->addPaginationLinks( + $this->url->to('api')->route('fof.polls.index'), + $request->getQueryParams(), + $offset, + $limit, + $totalItems - ($offset + $limit) > 0 ? null : 0 + ); + + $this->loadRelations($results, $include, $request); + + return $results; + } +}