Replace getAllPaginated with new helper function

pull/10358/head
Oliver Günther 3 years ago
parent fcc7f134ef
commit db973ebdf9
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 4
      frontend/src/app/core/apiv3/endpoints/work_packages/api-v3-work-packages-paths.ts
  2. 50
      frontend/src/app/features/hal/services/hal-resource.service.ts

@ -159,11 +159,11 @@ export class ApiV3WorkPackagesPaths extends ApiV3Collection<WorkPackageResource,
.halResourceService .halResourceService
.getAllPaginated<WorkPackageCollectionResource>( .getAllPaginated<WorkPackageCollectionResource>(
this.path, this.path,
ids.length,
{ {
filters: ApiV3Filter('id', '=', ids).toJson(), filters: ApiV3Filter('id', '=', ids).toJson(),
}, },
); )
.toPromise();
} }
protected createCache():WorkPackageCache { protected createCache():WorkPackageCache {

@ -65,6 +65,7 @@ import {
} from 'core-app/features/hal/resources/hal-resource'; } from 'core-app/features/hal/resources/hal-resource';
import { initializeHalProperties } from '../helpers/hal-resource-builder'; import { initializeHalProperties } from '../helpers/hal-resource-builder';
import { HalError } from 'core-app/features/hal/services/hal-error'; import { HalError } from 'core-app/features/hal/services/hal-error';
import { getPaginatedResults } from 'core-app/core/apiv3/helpers/get-paginated-results';
export interface HalResourceFactoryConfigInterface { export interface HalResourceFactoryConfigInterface {
cls?:any; cls?:any;
@ -142,46 +143,23 @@ export class HalResourceService {
* Return all potential pages to the request, when the elements returned from API is smaller * Return all potential pages to the request, when the elements returned from API is smaller
* than the expected. * than the expected.
* *
* @param href * @param href The URL to request
* @param expected The expected number of elements * @param params Parameters to pass to each paged request
* @param params * @param headers Headers to pass to each paged request
* @param headers * @return {Observable<CollectionResource[]>}
* @return {Promise<CollectionResource[]>}
*/ */
public async getAllPaginated<T extends CollectionResource>( public getAllPaginated<T extends CollectionResource>(
href:string, href:string,
expected:number,
params:Record<string, string|number> = {}, params:Record<string, string|number> = {},
headers:HTTPClientHeaders = {}, headers:HTTPClientHeaders = {},
):Promise<T[]> { ):Observable<T[]> {
// Total number retrieved return getPaginatedResults(
let retrieved = 0; (pageParams) => {
// Current offset page const requestParams = { ...params, pageParams };
let page = 1; return this.request<CollectionResource<T>>('get', href, this.toEprops(requestParams), headers);
// Accumulated results },
const allResults:T[] = []; (params.pageSize as number|undefined) || -1,
// If possible, request all at once. );
const requestParams = { ...params };
requestParams.pageSize = expected;
while (retrieved < expected) {
requestParams.offset = page;
const promise = this.request<T>('get', href, this.toEprops(requestParams), headers).toPromise();
// eslint-disable-next-line no-await-in-loop
const results = await promise;
if (results.count === 0) {
throw new Error('No more results for this query, but expected more.');
}
allResults.push(results);
retrieved += results.count;
page += 1;
}
return allResults;
} }
/** /**

Loading…
Cancel
Save