From 24a029ff5432605bd61a6a89a63ebd06b1622f5f Mon Sep 17 00:00:00 2001 From: trenc Date: Wed, 18 Sep 2024 10:58:01 +0200 Subject: [PATCH 1/6] refactor: use strictTypes --- src/app/Http/Controllers/ItemController.php | 25 ++++----------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/app/Http/Controllers/ItemController.php b/src/app/Http/Controllers/ItemController.php index bfee505..4b93760 100644 --- a/src/app/Http/Controllers/ItemController.php +++ b/src/app/Http/Controllers/ItemController.php @@ -5,16 +5,12 @@ use App\Http\Controllers\ResponseController; use App\Models\Item; use App\Http\Resources\ItemResource; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class ItemController extends ResponseController { - /** - * Display a paginated listing of the resource. - * - * @return \Illuminate\Http\Response - */ - public function index(Request $request) + public function index(Request $request): JsonResponse { $queryColumns = [ 'StoryId' => 'StoryId', @@ -36,13 +32,7 @@ public function index(Request $request) return $this->sendResponseWithMeta($collection, 'Items fetched.'); } - /** - * Display the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function show($id) + public function show(int $id): JsonResponse { try { $data = Item::findOrFail($id); @@ -54,14 +44,7 @@ public function show($id) } } - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param int $id - * @return \Illuminate\Http\Response - */ - public function update(Request $request, $id) + public function update(Request $request, int $id): JsonResponse { try { $item = Item::findOrfail($id); From afd7aef594863653c4e67808dd6b005539c8d2b4 Mon Sep 17 00:00:00 2001 From: trenc Date: Thu, 19 Sep 2024 08:47:30 +0200 Subject: [PATCH 2/6] docs: explain fieldList parameter in OpenAPI UI --- src/storage/api-docs/items-path.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/storage/api-docs/items-path.yaml b/src/storage/api-docs/items-path.yaml index 25428ab..c650943 100644 --- a/src/storage/api-docs/items-path.yaml +++ b/src/storage/api-docs/items-path.yaml @@ -21,6 +21,10 @@ get: description: If set determines that the query (StoryId, ItemId) is a multiple query string and separated by this separator schema: type: string + - in: query + name: fieldList + description: Comma-separated list of fields that will be shown in the response + type: string responses: 200: description: Ok From 93ef0ef9ace3e4387304638b95d9f752dbd80208 Mon Sep 17 00:00:00 2001 From: trenc Date: Thu, 19 Sep 2024 08:59:31 +0200 Subject: [PATCH 3/6] docs: rename fieldList to fieldlist --- src/storage/api-docs/items-path.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/api-docs/items-path.yaml b/src/storage/api-docs/items-path.yaml index c650943..f834327 100644 --- a/src/storage/api-docs/items-path.yaml +++ b/src/storage/api-docs/items-path.yaml @@ -22,7 +22,7 @@ get: schema: type: string - in: query - name: fieldList + name: fieldlist description: Comma-separated list of fields that will be shown in the response type: string responses: From 80961d2cf05701625db70e3cd9530f9f50c5b510 Mon Sep 17 00:00:00 2001 From: trenc Date: Thu, 19 Sep 2024 11:32:03 +0200 Subject: [PATCH 4/6] refactor: harmonize return values --- src/app/Models/Item.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/app/Models/Item.php b/src/app/Models/Item.php index 5947955..fb11eca 100644 --- a/src/app/Models/Item.php +++ b/src/app/Models/Item.php @@ -76,9 +76,9 @@ public function properties(): BelongsToMany // to harmonize the API regarding the existent database schema // we make use some custom accessors and mutators - public function getDescriptionLangAttribute(): object + public function getDescriptionLangAttribute(): Language { - return $this->language()->first() ?: (object)[]; + return $this->language()->first() ?: new Language(); } public function getCompletionStatusAttribute(): CompletionStatus @@ -92,10 +92,10 @@ public function getCompletionStatusAttribute(): CompletionStatus 'ColorCodeGradient' ]); - return $status; + return $status ?: new CompletionStatus(); } - public function getTranscriptionAttribute(): Transcription|HtrDataRevision|array + public function getTranscriptionAttribute(): Transcription|HtrDataRevision { if ($this->TranscriptionSource === 'manual') { $manualTranscription = $this @@ -108,7 +108,7 @@ public function getTranscriptionAttribute(): Transcription|HtrDataRevision|array ) ->firstWhere('CurrentVersion', 1); - return $manualTranscription ? $manualTranscription : []; + return $manualTranscription ?: new Transcription(); } if ($this->TranscriptionSource === 'htr') { @@ -120,7 +120,7 @@ public function getTranscriptionAttribute(): Transcription|HtrDataRevision|array ->latest() ->first(); - return $latest ? $latest->htrDataRevision->first() : []; + return $latest ? $latest->htrDataRevision->first() : new HtrData(); } return []; @@ -144,5 +144,4 @@ public function getPlacesAttribute(): Collection { return $this->places()->get() ?: []; } - } From 16d76ba3a8b24adf2dd0f20220fa91f4f3839f49 Mon Sep 17 00:00:00 2001 From: trenc Date: Thu, 19 Sep 2024 11:34:28 +0200 Subject: [PATCH 5/6] feat: provide a fieldlist to reduce response size --- src/app/Http/Controllers/ItemController.php | 2 ++ .../Http/Controllers/ResponseController.php | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/app/Http/Controllers/ItemController.php b/src/app/Http/Controllers/ItemController.php index 4b93760..6686009 100644 --- a/src/app/Http/Controllers/ItemController.php +++ b/src/app/Http/Controllers/ItemController.php @@ -23,6 +23,8 @@ public function index(Request $request): JsonResponse $data = $this->getDataByRequest($request, $model, $queryColumns, $initialSortColumn); + $data = $this->filterDataByFieldlist($data, $request, ['StoryId', 'ItemId'], $initialSortColumn); + if (!$data) { return $this->sendError('Invalid data', $request . ' not valid', 400); } diff --git a/src/app/Http/Controllers/ResponseController.php b/src/app/Http/Controllers/ResponseController.php index 91ff780..b13a33d 100644 --- a/src/app/Http/Controllers/ResponseController.php +++ b/src/app/Http/Controllers/ResponseController.php @@ -158,4 +158,34 @@ protected function filterDataByQueries( return $filtered; } + + protected function filterDataByFieldlist( + Collection $data, + Request $request, + array $defaults, + string $initialSortColumn + ): Collection + { + $queries = $request->query(); + + if (empty($queries['fieldlist'])) { + return $data; + } + + $fieldlist = explode(',', $queries['fieldlist']); + $fieldlist = array_map('trim', $fieldlist); + $fieldlist[] = $queries['orderBy'] ?? $initialSortColumn; + $fieldlist = array_merge($defaults, $fieldlist); + $fieldlist = array_unique($fieldlist); + $dataArray = $data->toArray()[0] ?: $data->toArray(); + $fieldsToRemove = array_filter(array_keys($dataArray), function($field) use ($fieldlist) { + return !in_array($field, $fieldlist); + }); + + $data->each(function($data) use ($fieldsToRemove) { + $data->makeHidden($fieldsToRemove); + }); + + return $data; + } } From 9e55fede19654bb47f9b5f9f6c6af7de457e300a Mon Sep 17 00:00:00 2001 From: trenc Date: Thu, 19 Sep 2024 11:35:49 +0200 Subject: [PATCH 6/6] build: bump version --- src/storage/api-docs/api-docs.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/api-docs/api-docs.yaml b/src/storage/api-docs/api-docs.yaml index dadecef..4f771a0 100644 --- a/src/storage/api-docs/api-docs.yaml +++ b/src/storage/api-docs/api-docs.yaml @@ -1,7 +1,7 @@ openapi: 3.0.3 info: - version: 1.49.0 + version: 1.50.0 title: Transcribathon Platform API v2 description: This is the documentation of the Transcribathon API v2 used by [https:transcribathon.eu](https://transcribathon.eu/).
For authorization you can use the the bearer token you are provided with.