max_items depends on header application/vnd.phraseanet.story-paginate

This commit is contained in:
aynsix
2019-11-28 18:15:55 +04:00
parent 4acc98574b
commit c9356ec206
4 changed files with 19 additions and 7 deletions

View File

@@ -51,7 +51,8 @@ class ApiApplicationLoader extends BaseApplicationLoader
['application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'], ['application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'],
V1::$extendedContentTypes['json'], V1::$extendedContentTypes['json'],
V1::$extendedContentTypes['jsonp'], V1::$extendedContentTypes['jsonp'],
V1::$extendedContentTypes['yaml'] V1::$extendedContentTypes['yaml'],
[V1::HEADER_ACCEPT_STORY_PAGINATE]
); );
$app['phraseanet.content-negotiation.custom_formats'] = [ $app['phraseanet.content-negotiation.custom_formats'] = [

View File

@@ -1217,12 +1217,18 @@ class V1Controller extends Controller
$fractal->setSerializer(new TraceableArraySerializer($this->app['dispatcher'])); $fractal->setSerializer(new TraceableArraySerializer($this->app['dispatcher']));
$fractal->parseIncludes($this->resolveSearchIncludes($request)); $fractal->parseIncludes($this->resolveSearchIncludes($request));
$story_max_items = null;
// if the header accept story paginate, take account story_max_items parameter
if (in_array(V1::HEADER_ACCEPT_STORY_PAGINATE, $request->getAcceptableContentTypes())) {
$story_max_items = (int)$request->get('story_max_items') ?: 10;
}
$result = $this->doSearch($request); $result = $this->doSearch($request);
$searchView = $this->buildSearchView( $searchView = $this->buildSearchView(
$result, $result,
$includeResolver->resolve($fractal), $includeResolver->resolve($fractal),
$this->resolveSubdefUrlTTL($request), $this->resolveSubdefUrlTTL($request),
(int)$request->get('story_max_items')?:10 $story_max_items
); );
$ret = $fractal->createData(new Item($searchView, $searchTransformer))->toArray(); $ret = $fractal->createData(new Item($searchView, $searchTransformer))->toArray();
@@ -1676,10 +1682,13 @@ class V1Controller extends Controller
return Result::createError($request, 404, 'Story not found')->createResponse(); return Result::createError($request, 404, 'Story not found')->createResponse();
} }
$offset = 1;
$max_items = null;
if (in_array(V1::HEADER_ACCEPT_STORY_PAGINATE, $request->getAcceptableContentTypes())) {
$max_items = (int)$request->get('max_items')?:10; $max_items = (int)$request->get('max_items')?:10;
$page = (int)$request->get('page')?:1; $page = (int)$request->get('page')?:1;
$offset = ($max_items * ($page - 1)) + 1;
$offset = $max_items * ($page - 1) + 1; }
$caption = $story->get_caption(); $caption = $story->get_caption();

View File

@@ -25,6 +25,8 @@ class V1 extends Api implements ControllerProviderInterface, ServiceProviderInte
{ {
const VERSION = '2.0.0'; const VERSION = '2.0.0';
const HEADER_ACCEPT_STORY_PAGINATE = 'application/vnd.phraseanet.story-paginate';
public static $extendedContentTypes = [ public static $extendedContentTypes = [
'json' => ['application/vnd.phraseanet.record-extended+json'], 'json' => ['application/vnd.phraseanet.record-extended+json'],
'yaml' => ['application/vnd.phraseanet.record-extended+yaml'], 'yaml' => ['application/vnd.phraseanet.record-extended+yaml'],

View File

@@ -47,7 +47,7 @@ interface RecordRepository
* @param null|int $max_items * @param null|int $max_items
* @return \set_selection[] * @return \set_selection[]
*/ */
public function findChildren(array $storyIds, $user = null, $offset = 0, $max_items = null); public function findChildren(array $storyIds, $user = null, $offset = 1, $max_items = null);
/** /**