From 758127e40981752adfb0d425e2723f98fdaf76c9 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe <kristof.delanghe@atmire.com> Date: Tue, 11 Sep 2018 16:23:57 +0200 Subject: [PATCH] 54472: Working post request with form-data (contains test code) --- .../create-collection-page.component.ts | 4 ++ src/app/core/data/collection-data.service.ts | 4 +- src/app/core/data/community-data.service.ts | 4 +- src/app/core/data/data.service.ts | 39 ++++++++++++++----- .../core/data/dspace-object-data.service.ts | 9 +++-- src/app/core/data/item-data.service.ts | 4 +- src/app/core/data/request.effects.ts | 2 +- 7 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/app/+collection-page/create-collection-page/create-collection-page.component.ts b/src/app/+collection-page/create-collection-page/create-collection-page.component.ts index 0f79558a44..93c3c16646 100644 --- a/src/app/+collection-page/create-collection-page/create-collection-page.component.ts +++ b/src/app/+collection-page/create-collection-page/create-collection-page.component.ts @@ -14,6 +14,7 @@ import { map } from 'rxjs/operators'; import { RemoteData } from '../../core/data/remote-data'; import { isNotEmpty } from '../../shared/empty.util'; import { DSpaceObject } from '../../core/shared/dspace-object.model'; +import { HttpEvent } from '@angular/common/http'; @Component({ selector: 'ds-create-collection', @@ -55,6 +56,9 @@ export class CreateCollectionPageComponent { this.router.navigateByUrl(''); } }); + // this.collectionDataService.createSimple(collection).subscribe((httpEvent: HttpEvent<{}>) => { + // console.log(httpEvent); + // }); }); } diff --git a/src/app/core/data/collection-data.service.ts b/src/app/core/data/collection-data.service.ts index fd2404650f..4157d96a9e 100644 --- a/src/app/core/data/collection-data.service.ts +++ b/src/app/core/data/collection-data.service.ts @@ -14,6 +14,7 @@ import { HALEndpointService } from '../shared/hal-endpoint.service'; import { AuthService } from '../auth/auth.service'; import { Community } from '../shared/community.model'; import { NotificationsService } from '../../shared/notifications/notifications.service'; +import { HttpClient } from '@angular/common/http'; @Injectable() export class CollectionDataService extends ComColDataService<NormalizedCollection, Collection> { @@ -28,7 +29,8 @@ export class CollectionDataService extends ComColDataService<NormalizedCollectio protected objectCache: ObjectCacheService, protected halService: HALEndpointService, protected authService: AuthService, - protected notificationsService: NotificationsService + protected notificationsService: NotificationsService, + protected http: HttpClient ) { super(); } diff --git a/src/app/core/data/community-data.service.ts b/src/app/core/data/community-data.service.ts index 4689fac854..49638629e6 100644 --- a/src/app/core/data/community-data.service.ts +++ b/src/app/core/data/community-data.service.ts @@ -17,6 +17,7 @@ import { hasValue, isNotEmpty } from '../../shared/empty.util'; import { Observable } from 'rxjs/Observable'; import { PaginatedList } from './paginated-list'; import { NotificationsService } from '../../shared/notifications/notifications.service'; +import { HttpClient } from '@angular/common/http'; @Injectable() export class CommunityDataService extends ComColDataService<NormalizedCommunity, Community> { @@ -32,7 +33,8 @@ export class CommunityDataService extends ComColDataService<NormalizedCommunity, protected objectCache: ObjectCacheService, protected halService: HALEndpointService, protected authService: AuthService, - protected notificationsService: NotificationsService + protected notificationsService: NotificationsService, + protected http: HttpClient ) { super(); } diff --git a/src/app/core/data/data.service.ts b/src/app/core/data/data.service.ts index 3dc32e9e69..fb3002b5d7 100644 --- a/src/app/core/data/data.service.ts +++ b/src/app/core/data/data.service.ts @@ -18,7 +18,7 @@ import { } from './request.models'; import { RequestService } from './request.service'; import { NormalizedObject } from '../cache/models/normalized-object.model'; -import { distinctUntilChanged, map, take, withLatestFrom } from 'rxjs/operators'; +import { distinctUntilChanged, map, switchMap, take, withLatestFrom } from 'rxjs/operators'; import { configureRequest, filterSuccessfulResponses, @@ -26,7 +26,7 @@ import { } from '../shared/operators'; import { ResponseCacheEntry } from '../cache/response-cache.reducer'; import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service'; -import { HttpHeaders } from '@angular/common/http'; +import { HttpClient, HttpEvent, HttpHeaders, HttpRequest } from '@angular/common/http'; import { ErrorResponse, GenericSuccessResponse } from '../cache/response-cache.models'; import { AuthService } from '../auth/auth.service'; import { NotificationsService } from '../../shared/notifications/notifications.service'; @@ -41,6 +41,7 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain> protected abstract halService: HALEndpointService; protected abstract authService: AuthService; protected abstract notificationsService: NotificationsService; + protected abstract http: HttpClient; public abstract getScopedEndpoint(scope: string): Observable<string> @@ -126,17 +127,12 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain> const request$ = endpoint$.pipe( take(1), withLatestFrom(this.buildCreateBody(dso)), - map(([endpoint, formdata]) => { - const options: HttpOptions = Object.create({}); - let headers = new HttpHeaders(); - headers = headers.append('Content-Type','multipart/form-data'); - options.headers = headers; - return new CreateRequest(requestId, endpoint, formdata, options); - }), + map(([endpoint, formdata]) => new CreateRequest(requestId, endpoint, this.buildFormData(formdata))), configureRequest(this.requestService) ); const payload$ = request$.pipe( + take(1), map((request: RestRequest) => request.href), getResponseFromSelflink(this.responseCache), map((response: ResponseCacheEntry) => { @@ -158,6 +154,31 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain> return this.rdbService.toRemoteDataObservable(requestEntry$, responseCache$, payload$); } + public createSimple(dso: TDomain): Observable<HttpEvent<{}>> { + const endpoint$ = this.halService.getEndpoint(this.linkPath).pipe( + isNotEmptyOperator(), + distinctUntilChanged() + ); + + return endpoint$.pipe( + withLatestFrom(this.buildCreateBody(dso)), + switchMap(([endpoint, form]) => { + const req = new HttpRequest('POST', endpoint, this.buildFormData(form)); + return this.http.request(req); + }) + ); + } + public abstract buildCreateBody(dso: TDomain): Observable<any>; + protected buildFormData(form: any): FormData { + const formdata = new FormData(); + for (const param in form) { + if (form.hasOwnProperty(param)) { + formdata.append(param, form[param]); + } + } + return formdata; + } + } diff --git a/src/app/core/data/dspace-object-data.service.ts b/src/app/core/data/dspace-object-data.service.ts index 8bae4e56ed..ecf52098ee 100644 --- a/src/app/core/data/dspace-object-data.service.ts +++ b/src/app/core/data/dspace-object-data.service.ts @@ -12,6 +12,7 @@ import { RemoteData } from './remote-data'; import { RequestService } from './request.service'; import { AuthService } from '../auth/auth.service'; import { NotificationsService } from '../../shared/notifications/notifications.service'; +import { HttpClient } from '@angular/common/http'; /* tslint:disable:max-classes-per-file */ class DataServiceImpl extends DataService<NormalizedDSpaceObject, DSpaceObject> { @@ -24,7 +25,8 @@ class DataServiceImpl extends DataService<NormalizedDSpaceObject, DSpaceObject> protected store: Store<CoreState>, protected halService: HALEndpointService, protected authService: AuthService, - protected notificationsService: NotificationsService) { + protected notificationsService: NotificationsService, + protected http: HttpClient) { super(); } @@ -51,8 +53,9 @@ export class DSpaceObjectDataService { protected rdbService: RemoteDataBuildService, protected halService: HALEndpointService, protected authService: AuthService, - protected notificationsService: NotificationsService) { - this.dataService = new DataServiceImpl(null, requestService, rdbService, null, halService, authService, notificationsService); + protected notificationsService: NotificationsService, + protected http: HttpClient) { + this.dataService = new DataServiceImpl(null, requestService, rdbService, null, halService, authService, notificationsService, http); } findById(uuid: string): Observable<RemoteData<DSpaceObject>> { diff --git a/src/app/core/data/item-data.service.ts b/src/app/core/data/item-data.service.ts index 343d4efede..4c003e15c9 100644 --- a/src/app/core/data/item-data.service.ts +++ b/src/app/core/data/item-data.service.ts @@ -17,6 +17,7 @@ import { RequestService } from './request.service'; import { HALEndpointService } from '../shared/hal-endpoint.service'; import { NotificationsService } from '../../shared/notifications/notifications.service'; import { AuthService } from '../auth/auth.service'; +import { HttpClient } from '@angular/common/http'; @Injectable() export class ItemDataService extends DataService<NormalizedItem, Item> { @@ -30,7 +31,8 @@ export class ItemDataService extends DataService<NormalizedItem, Item> { private bs: BrowseService, protected halService: HALEndpointService, protected authService: AuthService, - protected notificationsService: NotificationsService) { + protected notificationsService: NotificationsService, + protected http: HttpClient) { super(); } diff --git a/src/app/core/data/request.effects.ts b/src/app/core/data/request.effects.ts index 5fadd316f4..9d27717dc6 100644 --- a/src/app/core/data/request.effects.ts +++ b/src/app/core/data/request.effects.ts @@ -38,7 +38,7 @@ export class RequestEffects { }), map((entry: RequestEntry) => entry.request), flatMap((request: RestRequest) => { - let body; + let body = request.body; if (isNotEmpty(request.body)) { const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type)); body = serializer.serialize(request.body); -- GitLab