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\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<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';
$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++;
}
});
}

View File

@ -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<string, string> $data
*/
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) {
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);
}
}
});