Fixed pagination in search

This commit is contained in:
philipp lang 2021-11-17 23:40:47 +01:00
parent fca5884baa
commit b8c67aa5ed
2 changed files with 49 additions and 29 deletions

View File

@ -7,6 +7,7 @@ use App\Nami\Exceptions\TooManyLoginAttemptsException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Log; use Log;
use Zoomyboy\LaravelNami\Backend\Backend; use Zoomyboy\LaravelNami\Backend\Backend;
@ -44,21 +45,27 @@ class Api {
/** /**
* @param array<string, mixed> $payload * @param array<string, mixed> $payload
* @return Collection<int, Member> * @return LazyCollection<int, Member>
*/ */
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'; return LazyCollection::make(function() use ($payload) {
$response = $this->http()->get($url); $page = 1;
while (!isset ($totalEntries) || ($page-1) * 100 + 1 <= $totalEntries) {
if ($response->json()['success'] !== true) { $start = ($page-1) * 100;
$this->exception('Search failed', ['url' => $url], $response->json()); $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'];
return collect($response->json()['data'])->map(function($member) { if ($response->json()['success'] !== true) {
return Member::fromNami(collect($member)->mapWithKeys(function($value, $key) { $this->exception('Search failed', ['url' => $url], $response->json());
return [ str_replace('entries_', '', (string) $key) => $value ]; }
})); foreach ($response->json()['data'] as $member) {
yield Member::fromNami(collect($member)->mapWithKeys(function($value, $key) {
return [ str_replace('entries_', '', (string) $key) => $value ];
}));
}
$page++;
}
}); });
} }

View File

@ -170,7 +170,7 @@ class FakeBackend {
public function addSearch(int $mitgliedsNr, array $data): self public function addSearch(int $mitgliedsNr, array $data): self
{ {
Http::fake(function($request) use ($data, $mitgliedsNr) { 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 = [ $content = [
'success' => true, 'success' => true,
'data' => [$data], 'data' => [$data],
@ -202,25 +202,38 @@ class FakeBackend {
* @param array<string, string> $data * @param array<string, string> $data
*/ */
public function fakeMember(array $data): self public function fakeMember(array $data): self
{
return $this->fakeMembers([$data]);
}
/**
* @param array<int, array<string, string>> $data
*/
public function fakeMembers(array $data): self
{ {
Http::fake(function($request) use ($data) { 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') { foreach ($data as $member) {
return Http::response(json_encode([ if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$member['gruppierungId']}/{$member['id']}") {
'success' => true, $content = [
'data' => [[ 'success' => true,
'entries_id' => $data['id'], 'data' => $member,
'entries_gruppierungId' => $data['gruppierungId'], ];
]]
]) ?: '{}', 200); 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']}") { foreach (collect($data)->chunk(100) as $i => $chunk) {
$content = [ if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.($i+1).'&start='.($i*100).'&limit=100') {
'success' => true, return Http::response(json_encode([
'data' => $data, 'success' => true,
]; 'totalEntries' => count($data),
'data' => collect($chunk)->map(fn ($member) => [
return Http::response(json_encode($content) ?: '{}', 200); 'entries_id' => $member['id'],
'entries_gruppierungId' => $member['gruppierungId'],
])->toArray(),
]) ?: '{}', 200);
}
} }
}); });