-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace FoF\Polls\Api\Controllers; | ||
|
||
use Flarum\Api\Controller\AbstractListController; | ||
use Flarum\Database\Eloquent\Collection; | ||
use Flarum\Http\RequestUtil; | ||
use Flarum\Http\UrlGenerator; | ||
use FoF\Polls\Api\Serializers\PollSerializer; | ||
use FoF\Polls\Poll; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Tobscure\JsonApi\Document; | ||
|
||
class ListPollsController extends AbstractListController | ||
{ | ||
public $serializer = PollSerializer::class; | ||
|
||
public $include = [ | ||
'options', | ||
'votes', | ||
'myVotes', | ||
'myVotes.option', | ||
]; | ||
|
||
/** | ||
* @var UrlGenerator | ||
*/ | ||
protected $url; | ||
|
||
public function __construct(UrlGenerator $url) | ||
{ | ||
$this->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; | ||
Check failure on line 68 in src/Api/Controllers/ListPollsController.php GitHub Actions / run / PHPStan PHP 7.3
Check failure on line 68 in src/Api/Controllers/ListPollsController.php GitHub Actions / run / PHPStan PHP 7.4
Check failure on line 68 in src/Api/Controllers/ListPollsController.php GitHub Actions / run / PHPStan PHP 8.0
Check failure on line 68 in src/Api/Controllers/ListPollsController.php GitHub Actions / run / PHPStan PHP 8.1
Check failure on line 68 in src/Api/Controllers/ListPollsController.php GitHub Actions / run / PHPStan PHP 8.2
Check failure on line 68 in src/Api/Controllers/ListPollsController.php GitHub Actions / run / PHPStan PHP 8.3
|
||
} | ||
} |