Skip to content

Commit

Permalink
feat: list polls controller
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jan 30, 2024
1 parent 8fb8cc6 commit d49c1f9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 70 additions & 0 deletions src/Api/Controllers/ListPollsController.php
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

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.3

Method FoF\Polls\Api\Controllers\ListPollsController::data() should return Flarum\Database\Eloquent\Collection but returns Illuminate\Database\Eloquent\Collection<FoF\Polls\Poll>.

Check failure on line 68 in src/Api/Controllers/ListPollsController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Method FoF\Polls\Api\Controllers\ListPollsController::data() should return Flarum\Database\Eloquent\Collection but returns Illuminate\Database\Eloquent\Collection<FoF\Polls\Poll>.

Check failure on line 68 in src/Api/Controllers/ListPollsController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Method FoF\Polls\Api\Controllers\ListPollsController::data() should return Flarum\Database\Eloquent\Collection but returns Illuminate\Database\Eloquent\Collection<FoF\Polls\Poll>.

Check failure on line 68 in src/Api/Controllers/ListPollsController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Method FoF\Polls\Api\Controllers\ListPollsController::data() should return Flarum\Database\Eloquent\Collection but returns Illuminate\Database\Eloquent\Collection<FoF\Polls\Poll>.

Check failure on line 68 in src/Api/Controllers/ListPollsController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Method FoF\Polls\Api\Controllers\ListPollsController::data() should return Flarum\Database\Eloquent\Collection but returns Illuminate\Database\Eloquent\Collection<FoF\Polls\Poll>.

Check failure on line 68 in src/Api/Controllers/ListPollsController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Method FoF\Polls\Api\Controllers\ListPollsController::data() should return Flarum\Database\Eloquent\Collection but returns Illuminate\Database\Eloquent\Collection<FoF\Polls\Poll>.
}
}

0 comments on commit d49c1f9

Please sign in to comment.