From b8c67aa5edfcb950435d66211976e26ddadcde91 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 17 Nov 2021 23:40:47 +0100 Subject: [PATCH] Fixed pagination in search --- src/Api.php | 33 ++++++++++++++++----------- src/Backend/FakeBackend.php | 45 ++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/Api.php b/src/Api.php index d9ac0a0..36d0644 100644 --- a/src/Api.php +++ b/src/Api.php @@ -7,6 +7,7 @@ use App\Nami\Exceptions\TooManyLoginAttemptsException; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; +use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Log; use Zoomyboy\LaravelNami\Backend\Backend; @@ -44,21 +45,27 @@ class Api { /** * @param array $payload - * @return Collection + * @return LazyCollection */ - public function search(array $payload): Collection + public function search(array $payload): LazyCollection { - $url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode($payload) ?: '[]').'&page=1&start=0&limit=10'; - $response = $this->http()->get($url); - - if ($response->json()['success'] !== true) { - $this->exception('Search failed', ['url' => $url], $response->json()); - } - - return collect($response->json()['data'])->map(function($member) { - return Member::fromNami(collect($member)->mapWithKeys(function($value, $key) { - return [ str_replace('entries_', '', (string) $key) => $value ]; - })); + return LazyCollection::make(function() use ($payload) { + $page = 1; + while (!isset ($totalEntries) || ($page-1) * 100 + 1 <= $totalEntries) { + $start = ($page-1) * 100; + $url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode((object) $payload) ?: '{}').'&page='.$page.'&start='.$start.'&limit=100'; + $response = $this->http()->get($url); + $totalEntries = $response->json()['totalEntries']; + if ($response->json()['success'] !== true) { + $this->exception('Search failed', ['url' => $url], $response->json()); + } + foreach ($response->json()['data'] as $member) { + yield Member::fromNami(collect($member)->mapWithKeys(function($value, $key) { + return [ str_replace('entries_', '', (string) $key) => $value ]; + })); + } + $page++; + } }); } diff --git a/src/Backend/FakeBackend.php b/src/Backend/FakeBackend.php index 5c5e489..42e6ae9 100644 --- a/src/Backend/FakeBackend.php +++ b/src/Backend/FakeBackend.php @@ -170,7 +170,7 @@ class FakeBackend { public function addSearch(int $mitgliedsNr, array $data): self { Http::fake(function($request) use ($data, $mitgliedsNr) { - if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode(['mitgliedsNummber' => $mitgliedsNr]) ?: '{}').'&page=1&start=0&limit=10') { + if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode(['mitgliedsNummber' => $mitgliedsNr]) ?: '{}').'&page=1&start=0&limit=100') { $content = [ 'success' => true, 'data' => [$data], @@ -202,25 +202,38 @@ class FakeBackend { * @param array $data */ public function fakeMember(array $data): self + { + return $this->fakeMembers([$data]); + } + + /** + * @param array> $data + */ + public function fakeMembers(array $data): self { Http::fake(function($request) use ($data) { - if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('[]').'&page=1&start=0&limit=10') { - return Http::response(json_encode([ - 'success' => true, - 'data' => [[ - 'entries_id' => $data['id'], - 'entries_gruppierungId' => $data['gruppierungId'], - ]] - ]) ?: '{}', 200); + foreach ($data as $member) { + if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$member['gruppierungId']}/{$member['id']}") { + $content = [ + 'success' => true, + 'data' => $member, + ]; + + return Http::response(json_encode($content) ?: '{}', 200); + } } - if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$data['gruppierungId']}/{$data['id']}") { - $content = [ - 'success' => true, - 'data' => $data, - ]; - - return Http::response(json_encode($content) ?: '{}', 200); + foreach (collect($data)->chunk(100) as $i => $chunk) { + if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.($i+1).'&start='.($i*100).'&limit=100') { + return Http::response(json_encode([ + 'success' => true, + 'totalEntries' => count($data), + 'data' => collect($chunk)->map(fn ($member) => [ + 'entries_id' => $member['id'], + 'entries_gruppierungId' => $member['gruppierungId'], + ])->toArray(), + ]) ?: '{}', 200); + } } });