Changed DataService's create method in the way to accept array of request params

This commit is contained in:
Giuseppe Digilio
2020-04-02 19:54:30 +02:00
parent 41d6255998
commit b9de6a7a7d
2 changed files with 33 additions and 5 deletions

View File

@@ -152,6 +152,33 @@ export abstract class DataService<T extends CacheableObject> {
} }
} }
/**
* Turn an array of RequestParam into a query string and combine it with the given HREF
*
* @param href The HREF to which the query string should be appended
* @param params Array with additional params to combine with query string
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved
*
* @return {Observable<string>}
* Return an observable that emits created HREF
*/
protected buildHrefWithParams(href: string, params: RequestParam[], ...linksToFollow: Array<FollowLinkConfig<T>>): string {
let args = [];
if (hasValue(params)) {
params.forEach((param: RequestParam) => {
args.push(`${param.fieldName}=${param.fieldValue}`);
})
}
args = this.addEmbedParams(args, ...linksToFollow);
if (isNotEmpty(args)) {
return new URLCombiner(href, `?${args.join('&')}`).toString();
} else {
return href;
}
}
/** /**
* Adds the embed options to the link for the request * Adds the embed options to the link for the request
* @param args params for the query string * @param args params for the query string
@@ -379,15 +406,15 @@ export abstract class DataService<T extends CacheableObject> {
* *
* @param {DSpaceObject} dso * @param {DSpaceObject} dso
* The object to create * The object to create
* @param {string} parentUUID * @param {RequestParam[]} params
* The UUID of the parent to create the new object under * Array with additional params to combine with query string
*/ */
create(dso: T, parentUUID: string): Observable<RemoteData<T>> { create(dso: T, ...params: RequestParam[]): Observable<RemoteData<T>> {
const requestId = this.requestService.generateRequestId(); const requestId = this.requestService.generateRequestId();
const endpoint$ = this.halService.getEndpoint(this.linkPath).pipe( const endpoint$ = this.halService.getEndpoint(this.linkPath).pipe(
isNotEmptyOperator(), isNotEmptyOperator(),
distinctUntilChanged(), distinctUntilChanged(),
map((endpoint: string) => parentUUID ? `${endpoint}?parent=${parentUUID}` : endpoint) map((endpoint: string) => this.buildHrefWithParams(endpoint, params))
); );
const serializedDso = new DSpaceSerializer(getClassForType((dso as any).type)).serialize(dso); const serializedDso = new DSpaceSerializer(getClassForType((dso as any).type)).serialize(dso);

View File

@@ -13,6 +13,7 @@ import { getSucceededRemoteData } from '../../../core/shared/operators';
import { ResourceType } from '../../../core/shared/resource-type'; import { ResourceType } from '../../../core/shared/resource-type';
import { hasValue, isNotEmpty, isNotUndefined } from '../../empty.util'; import { hasValue, isNotEmpty, isNotUndefined } from '../../empty.util';
import { NotificationsService } from '../../notifications/notifications.service'; import { NotificationsService } from '../../notifications/notifications.service';
import { RequestParam } from '../../../core/cache/models/request-param.model';
/** /**
* Component representing the create page for communities and collections * Component representing the create page for communities and collections
@@ -76,7 +77,7 @@ export class CreateComColPageComponent<TDomain extends DSpaceObject> implements
const uploader = event.uploader; const uploader = event.uploader;
this.parentUUID$.pipe(take(1)).subscribe((uuid: string) => { this.parentUUID$.pipe(take(1)).subscribe((uuid: string) => {
this.dsoDataService.create(dso, uuid) this.dsoDataService.create(dso, new RequestParam('parent', uuid))
.pipe(getSucceededRemoteData()) .pipe(getSucceededRemoteData())
.subscribe((dsoRD: RemoteData<TDomain>) => { .subscribe((dsoRD: RemoteData<TDomain>) => {
if (isNotUndefined(dsoRD)) { if (isNotUndefined(dsoRD)) {