From 3c12739067509e07a2352bdf101f1569a62b2f4f Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 27 Jul 2022 22:10:10 +0500 Subject: [PATCH] magazines: add detailed pagination + update docs --- .../Controllers/V4DB/MagazineController.php | 49 +++++++++++---- .../Controllers/V4DB/ProducerController.php | 25 ++++++++ app/Http/Resources/V4/MagazineCollection.php | 38 +++--------- app/Http/Resources/V4/ProducerCollection.php | 28 --------- storage/api-docs/api-docs.json | 61 +++++++++++++++++++ 5 files changed, 130 insertions(+), 71 deletions(-) diff --git a/app/Http/Controllers/V4DB/MagazineController.php b/app/Http/Controllers/V4DB/MagazineController.php index bc9eae9..7f1cd64 100644 --- a/app/Http/Controllers/V4DB/MagazineController.php +++ b/app/Http/Controllers/V4DB/MagazineController.php @@ -11,9 +11,6 @@ use Illuminate\Http\Request; class MagazineController extends Controller { - - const MAX_RESULTS_PER_PAGE = 25; - /** * @OA\Get( * path="/magazines", @@ -21,6 +18,32 @@ class MagazineController extends Controller * tags={"magazines"}, * * @OA\Parameter(ref="#/components/parameters/page"), + * @OA\Parameter(ref="#/components/parameters/limit"), + * + * @OA\Parameter( + * name="q", + * in="query", + * @OA\Schema(type="string") + * ), + * + * @OA\Parameter( + * name="order_by", + * in="query", + * @OA\Schema(ref="#/components/schemas/magazines_query_orderby") + * ), + * + * @OA\Parameter( + * name="sort", + * in="query", + * @OA\Schema(ref="#/components/schemas/search_query_sort") + * ), + * + * @OA\Parameter( + * name="letter", + * in="query", + * description="Return entries starting with the given letter", + * @OA\Schema(type="string") + * ), * * @OA\Response( * response="200", @@ -36,21 +59,21 @@ class MagazineController extends Controller * ), * ) */ - public function main(Request $request) + public function main(Request $request): MagazineCollection { + $maxResultsPerPage = (int) env('MAX_RESULTS_PER_PAGE', 25); + $page = $request->get('page') ?? 1; - $limit = $request->get('limit') ?? self::MAX_RESULTS_PER_PAGE; + $limit = $request->get('limit') ?? $maxResultsPerPage; - if (!empty($limit)) { - $limit = (int) $limit; + $limit = (int) $limit; - if ($limit <= 0) { - $limit = 1; - } + if ($limit <= 0) { + $limit = 1; + } - if ($limit > self::MAX_RESULTS_PER_PAGE) { - $limit = self::MAX_RESULTS_PER_PAGE; - } + if ($limit > $maxResultsPerPage) { + $limit = $maxResultsPerPage; } $results = SearchQueryBuilderMagazine::query( diff --git a/app/Http/Controllers/V4DB/ProducerController.php b/app/Http/Controllers/V4DB/ProducerController.php index d9a7032..5b9c5f8 100644 --- a/app/Http/Controllers/V4DB/ProducerController.php +++ b/app/Http/Controllers/V4DB/ProducerController.php @@ -24,6 +24,31 @@ class ProducerController extends Controller * @OA\Parameter(ref="#/components/parameters/page"), * @OA\Parameter(ref="#/components/parameters/limit"), * + * @OA\Parameter( + * name="q", + * in="query", + * @OA\Schema(type="string") + * ), + * + * @OA\Parameter( + * name="order_by", + * in="query", + * @OA\Schema(ref="#/components/schemas/producers_query_orderby") + * ), + * + * @OA\Parameter( + * name="sort", + * in="query", + * @OA\Schema(ref="#/components/schemas/search_query_sort") + * ), + * + * @OA\Parameter( + * name="letter", + * in="query", + * description="Return entries starting with the given letter", + * @OA\Schema(type="string") + * ), + * * @OA\Response( * response="200", * description="Returns producers collection", diff --git a/app/Http/Resources/V4/MagazineCollection.php b/app/Http/Resources/V4/MagazineCollection.php index 8d4226e..4f4bf4a 100644 --- a/app/Http/Resources/V4/MagazineCollection.php +++ b/app/Http/Resources/V4/MagazineCollection.php @@ -26,34 +26,6 @@ class MagazineCollection extends ResourceCollection * schema="magazines", * description="Magazine Collection Resource", * - * @OA\Parameter(ref="#/components/parameters/page"), - * @OA\Parameter(ref="#/components/parameters/limit"), - * - * @OA\Parameter( - * name="q", - * in="query", - * @OA\Schema(type="string") - * ), - * - * @OA\Parameter( - * name="order_by", - * in="query", - * @OA\Schema(ref="#/components/schemas/magazines_query_orderby") - * ), - * - * @OA\Parameter( - * name="sort", - * in="query", - * @OA\Schema(ref="#/components/schemas/search_query_sort") - * ), - * - * @OA\Parameter( - * name="letter", - * in="query", - * description="Return entries starting with the given letter", - * @OA\Schema(type="string") - * ), - * * allOf={ * @OA\Schema(ref="#/components/schemas/pagination"), * @OA\Schema( @@ -76,7 +48,13 @@ class MagazineCollection extends ResourceCollection { $this->pagination = [ 'last_visible_page' => $resource->lastPage(), - 'has_next_page' => $resource->hasMorePages() + 'has_next_page' => $resource->hasMorePages(), + 'current_page' => $resource->currentPage(), + 'items' => [ + 'count' => $resource->count(), + 'total' => $resource->total(), + 'per_page' => $resource->perPage(), + ], ]; $this->collection = $resource->getCollection(); @@ -104,4 +82,4 @@ class MagazineCollection extends ResourceCollection unset($jsonResponse['links'],$jsonResponse['meta']); $response->setContent(json_encode($jsonResponse)); } -} \ No newline at end of file +} diff --git a/app/Http/Resources/V4/ProducerCollection.php b/app/Http/Resources/V4/ProducerCollection.php index 1b69e42..435854d 100644 --- a/app/Http/Resources/V4/ProducerCollection.php +++ b/app/Http/Resources/V4/ProducerCollection.php @@ -26,34 +26,6 @@ class ProducerCollection extends ResourceCollection * schema="producers", * description="Producer Collection Resource", * - * @OA\Parameter(ref="#/components/parameters/page"), - * @OA\Parameter(ref="#/components/parameters/limit"), - * - * @OA\Parameter( - * name="q", - * in="query", - * @OA\Schema(type="string") - * ), - * - * @OA\Parameter( - * name="order_by", - * in="query", - * @OA\Schema(ref="#/components/schemas/producers_query_orderby") - * ), - * - * @OA\Parameter( - * name="sort", - * in="query", - * @OA\Schema(ref="#/components/schemas/search_query_sort") - * ), - * - * @OA\Parameter( - * name="letter", - * in="query", - * description="Return entries starting with the given letter", - * @OA\Schema(type="string") - * ), - * * allOf={ * @OA\Schema(ref="#/components/schemas/pagination"), * @OA\Schema( diff --git a/storage/api-docs/api-docs.json b/storage/api-docs/api-docs.json index 5c4c0a7..275e1af 100644 --- a/storage/api-docs/api-docs.json +++ b/storage/api-docs/api-docs.json @@ -1166,6 +1166,38 @@ "parameters": [ { "$ref": "#/components/parameters/page" + }, + { + "$ref": "#/components/parameters/limit" + }, + { + "name": "q", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "order_by", + "in": "query", + "schema": { + "$ref": "#/components/schemas/magazines_query_orderby" + } + }, + { + "name": "sort", + "in": "query", + "schema": { + "$ref": "#/components/schemas/search_query_sort" + } + }, + { + "name": "letter", + "in": "query", + "description": "Return entries starting with the given letter", + "schema": { + "type": "string" + } } ], "responses": { @@ -1875,6 +1907,35 @@ }, { "$ref": "#/components/parameters/limit" + }, + { + "name": "q", + "in": "query", + "schema": { + "type": "string" + } + }, + { + "name": "order_by", + "in": "query", + "schema": { + "$ref": "#/components/schemas/producers_query_orderby" + } + }, + { + "name": "sort", + "in": "query", + "schema": { + "$ref": "#/components/schemas/search_query_sort" + } + }, + { + "name": "letter", + "in": "query", + "description": "Return entries starting with the given letter", + "schema": { + "type": "string" + } } ], "responses": {