mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 02:24:11 +00:00
54472: Working post request with form-data (contains test code)
This commit is contained in:
@@ -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);
|
||||
// });
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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>> {
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user