54472: Passing parent as parameter and fixed duplicate collection created

This commit is contained in:
Kristof De Langhe
2018-09-12 08:11:33 +02:00
parent 758127e409
commit 0197b4a9dc
6 changed files with 26 additions and 68 deletions

View File

@@ -10,7 +10,7 @@ import { Router } from '@angular/router';
import { DSOSuccessResponse, ErrorResponse } from '../../core/cache/response-cache.models';
import { Observable } from 'rxjs/Observable';
import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer';
import { map } from 'rxjs/operators';
import { map, take } from 'rxjs/operators';
import { RemoteData } from '../../core/data/remote-data';
import { isNotEmpty } from '../../shared/empty.util';
import { DSpaceObject } from '../../core/shared/dspace-object.model';
@@ -36,7 +36,7 @@ export class CreateCollectionPageComponent {
}
onSubmit(data: any) {
this.parentUUID$.subscribe((uuid: string) => {
this.parentUUID$.pipe(take(1)).subscribe((uuid: string) => {
const collection = Object.assign(new Collection(), {
name: data.name,
metadata: [
@@ -45,20 +45,13 @@ export class CreateCollectionPageComponent {
{ key: 'dc.rights', value: data.copyright },
{ key: 'dc.rights.license', value: data.license }
// TODO: metadata for news and provenance
],
owner: Observable.of(new RemoteData(false, false, true, null, Object.assign(new Community(), {
id: uuid,
uuid: uuid
})))
]
});
this.collectionDataService.create(collection).subscribe((rd: RemoteData<DSpaceObject>) => {
this.collectionDataService.create(collection, uuid).pipe(take(1)).subscribe((rd: RemoteData<DSpaceObject>) => {
if (rd.hasSucceeded) {
this.router.navigateByUrl('');
}
});
// this.collectionDataService.createSimple(collection).subscribe((httpEvent: HttpEvent<{}>) => {
// console.log(httpEvent);
// });
});
}

View File

@@ -30,7 +30,7 @@ export class CreateCommunityPageComponent {
}
onSubmit(data: any) {
this.parentUUID$.subscribe((uuid: string) => {
this.parentUUID$.pipe(take(1)).subscribe((uuid: string) => {
const community = Object.assign(new Community(), {
name: data.name,
metadata: [
@@ -38,13 +38,9 @@ export class CreateCommunityPageComponent {
{ key: 'dc.description.abstract', value: data.description },
{ key: 'dc.rights', value: data.copyright }
// TODO: metadata for news
],
owner: Observable.of(new RemoteData(false, false, true, null, Object.assign(new Community(), {
id: uuid,
uuid: uuid
})))
]
});
this.communityDataService.create(community).pipe(take(1)).subscribe((rd: RemoteData<DSpaceObject>) => {
this.communityDataService.create(community, uuid).pipe(take(1)).subscribe((rd: RemoteData<DSpaceObject>) => {
if (rd.hasSucceeded) {
this.router.navigateByUrl('');
}

View File

@@ -66,25 +66,20 @@ export abstract class ComColDataService<TNormalized extends NormalizedObject, TD
}
}
public buildCreateBody(comcol): Observable<any> {
return comcol.owner.pipe(
map((rd: RemoteData<Community | Collection>) => {
const form: any = {
name: comcol.name
};
if (rd.payload.id) {
form.parent = rd.payload.id;
public buildFormData(comcol, parentUUID): FormData {
const form: FormData = new FormData();
form.append('name', comcol.name);
if (isNotEmpty(parentUUID)) {
form.append('parent', parentUUID);
}
if (comcol.metadata) {
for (const i of Object.keys(comcol.metadata)) {
if (isNotEmpty(comcol.metadata[i].value)) {
form.append(comcol.metadata[i].key, comcol.metadata[i].value);
}
if (comcol.metadata) {
for (const i of Object.keys(comcol.metadata)) {
if (isNotEmpty(comcol.metadata[i].value)) {
form[comcol.metadata[i].key] = comcol.metadata[i].value;
}
}
}
return form;
})
);
}
}
return form;
}
}

View File

@@ -117,7 +117,7 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain>
return this.rdbService.buildSingle<TNormalized, TDomain>(href);
}
public create(dso: TDomain): Observable<RemoteData<TDomain>> {
public create(dso: TDomain, parentUUID: string): Observable<RemoteData<TDomain>> {
const requestId = this.requestService.generateRequestId();
const endpoint$ = this.halService.getEndpoint(this.linkPath).pipe(
isNotEmptyOperator(),
@@ -126,8 +126,7 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain>
const request$ = endpoint$.pipe(
take(1),
withLatestFrom(this.buildCreateBody(dso)),
map(([endpoint, formdata]) => new CreateRequest(requestId, endpoint, this.buildFormData(formdata))),
map((endpoint: string) => new CreateRequest(requestId, endpoint, this.buildFormData(dso, parentUUID))),
configureRequest(this.requestService)
);
@@ -154,31 +153,6 @@ 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;
}
public abstract buildFormData(dso: TDomain, parentUUID: string): FormData;
}

View File

@@ -38,7 +38,7 @@ class DataServiceImpl extends DataService<NormalizedDSpaceObject, DSpaceObject>
return endpoint.replace(/\{\?uuid\}/,`?uuid=${resourceID}`);
}
buildCreateBody(dso: DSpaceObject): Observable<FormData> {
buildFormData(dso: DSpaceObject, parentUUID: string): FormData {
return undefined;
}
}

View File

@@ -47,8 +47,8 @@ export class ItemDataService extends DataService<NormalizedItem, Item> {
}
}
buildCreateBody(dso: Item): Observable<FormData> {
// TODO: Build http body for creating an Item on the REST service
buildFormData(dso: Item, parentUUID: string): FormData {
// TODO: build FormData for creating an Item
return undefined;
}