Fixed pagination in search
This commit is contained in:
parent
fca5884baa
commit
b8c67aa5ed
21
src/Api.php
21
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<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) {
|
||||
$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());
|
||||
}
|
||||
|
||||
return collect($response->json()['data'])->map(function($member) {
|
||||
return Member::fromNami(collect($member)->mapWithKeys(function($value, $key) {
|
||||
foreach ($response->json()['data'] as $member) {
|
||||
yield Member::fromNami(collect($member)->mapWithKeys(function($value, $key) {
|
||||
return [ str_replace('entries_', '', (string) $key) => $value ];
|
||||
}));
|
||||
}
|
||||
$page++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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],
|
||||
|
@ -203,25 +203,38 @@ class FakeBackend {
|
|||
*/
|
||||
public function fakeMember(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);
|
||||
return $this->fakeMembers([$data]);
|
||||
}
|
||||
|
||||
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$data['gruppierungId']}/{$data['id']}") {
|
||||
/**
|
||||
* @param array<int, array<string, string>> $data
|
||||
*/
|
||||
public function fakeMembers(array $data): self
|
||||
{
|
||||
Http::fake(function($request) use ($data) {
|
||||
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' => $data,
|
||||
'data' => $member,
|
||||
];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return $this;
|
||||
|
|
Loading…
Reference in New Issue