1
0
mirror of https://github.com/flarum/core.git synced 2025-07-29 12:40:40 +02:00

Add pagination links to JSON-API index actions

This commit is contained in:
Toby Zerner
2015-05-07 08:22:15 +09:30
parent 8e1f6db85d
commit 05e561d3d6
4 changed files with 49 additions and 7 deletions

View File

@@ -76,7 +76,7 @@ class IndexAction extends SerializeCollectionAction
$response->content->addMeta('total', $total); $response->content->addMeta('total', $total);
} }
// $response->content->addMeta('moreUrl', $moreUrl); static::addPaginationLinks($response, $request, route('flarum.api.discussions.index'), $total ?: $results->areMoreResults());
return $results->getDiscussions(); return $results->getDiscussions();
} }

View File

@@ -61,8 +61,8 @@ abstract class SerializeAction implements ActionInterface
/** /**
* Handle an API request and return an API response. * Handle an API request and return an API response.
* *
* @param Flarum\Api\Request $request * @param \Flarum\Api\Request $request
* @return Flarum\Api\Response * @return \Flarum\Api\Response
*/ */
public function handle(Request $request) public function handle(Request $request)
{ {
@@ -80,8 +80,8 @@ abstract class SerializeAction implements ActionInterface
/** /**
* Get the data to be serialized and assigned to the response document. * Get the data to be serialized and assigned to the response document.
* *
* @param Flarum\Api\JsonApiRequest $request * @param \Flarum\Api\JsonApiRequest $request
* @param Flarum\Api\JsonApiResponse $response * @param \Flarum\Api\JsonApiResponse $response
* @return array * @return array
*/ */
abstract protected function data(JsonApiRequest $request, JsonApiResponse $response); abstract protected function data(JsonApiRequest $request, JsonApiResponse $response);
@@ -99,7 +99,7 @@ abstract class SerializeAction implements ActionInterface
* Extract parameters from the request input and assign them to the * Extract parameters from the request input and assign them to the
* request, restricted by the action's specifications. * request, restricted by the action's specifications.
* *
* @param Flarum\Api\Request $request * @param \Flarum\Api\Request $request
* @return void * @return void
*/ */
protected static function buildJsonApiRequest(Request $request) protected static function buildJsonApiRequest(Request $request)
@@ -151,4 +151,41 @@ abstract class SerializeAction implements ActionInterface
{ {
return min($limit, static::$limitMax) ?: static::$limit; return min($limit, static::$limitMax) ?: static::$limit;
} }
/**
* Add pagination links to a JSON-API response, based on input parameters
* and the default parameters of this action.
*
* @param \Flarum\Api\JsonApiResponse $response
* @param \Flarum\Api\JsonApiRequest $request
* @param string $url The base URL to build pagination links with.
* @param integer|boolean $total The total number of results (used to build
* a 'last' link), or just true if there are more results but how many
* is unknown ('last' link is ommitted).
* @return void
*/
protected static function addPaginationLinks(JsonApiResponse $response, JsonApiRequest $request, $url, $total = true)
{
if ($request->limit != static::$limit) {
array_set($input, 'page.limit', $request->limit);
}
array_set($input, 'page.offset', 0);
$response->content->addLink('first', $url.'?'.http_build_query($input));
if ($request->offset > 0) {
array_set($input, 'page.offset', max(0, $request->offset - $request->limit));
$response->content->addLink('prev', $url.'?'.http_build_query($input));
}
if ($total === true || $request->offset + $request->limit < $total) {
array_set($input, 'page.offset', $request->offset + $request->limit);
$response->content->addLink('next', $url.'?'.http_build_query($input));
}
if ($total && $total !== true) {
array_set($input, 'page.offset', $total - $request->limit);
$response->content->addLink('last', $url.'?'.http_build_query($input));
}
}
} }

View File

@@ -71,7 +71,7 @@ class IndexAction extends SerializeCollectionAction
$response->content->addMeta('total', $total); $response->content->addMeta('total', $total);
} }
// $response->content->addMeta('moreUrl', $moreUrl); static::addPaginationLinks($response, $request, route('flarum.api.users.index'), $total ?: $results->areMoreResults());
return $results->getUsers(); return $results->getUsers();
} }

View File

@@ -22,4 +22,9 @@ class Request
{ {
return array_get($this->input, $key, $default); return array_get($this->input, $key, $default);
} }
public function all()
{
return $this->input;
}
} }