mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 15:33:04 +00:00
Merge branch 'response-cache-refactoring' into w2p-54472_Create-community-and-collection-pages
Conflicts: src/app/core/auth/auth-response-parsing.service.ts src/app/core/auth/auth.service.ts src/app/core/auth/models/auth-status.model.ts src/app/core/auth/models/normalized-auth-status.model.ts src/app/core/auth/server-auth.service.ts src/app/core/cache/builders/remote-data-build.service.ts src/app/core/data/collection-data.service.ts src/app/core/data/comcol-data.service.spec.ts src/app/core/data/comcol-data.service.ts src/app/core/data/community-data.service.ts src/app/core/data/data.service.spec.ts src/app/core/data/data.service.ts src/app/core/data/dspace-object-data.service.ts src/app/core/data/item-data.service.ts src/app/core/data/request.models.ts src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts src/app/core/shared/hal-endpoint.service.ts src/app/core/shared/operators.ts src/app/shared/testing/auth-request-service-stub.ts src/app/shared/testing/auth-service-stub.ts
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { distinctUntilChanged, filter, first, map, take } from 'rxjs/operators';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { hasValue, isNotEmpty, isNotEmptyOperator } from '../../shared/empty.util';
|
||||
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||
import { ResponseCacheService } from '../cache/response-cache.service';
|
||||
import { CoreState } from '../core.reducers';
|
||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||
import { URLCombiner } from '../url-combiner/url-combiner';
|
||||
@@ -13,78 +13,68 @@ import {
|
||||
FindAllOptions,
|
||||
FindAllRequest,
|
||||
FindByIDRequest,
|
||||
GetRequest,
|
||||
RestRequest
|
||||
GetRequest
|
||||
} from './request.models';
|
||||
import { RequestService } from './request.service';
|
||||
import { NormalizedObject } from '../cache/models/normalized-object.model';
|
||||
import { distinctUntilChanged, first, map, take, tap } from 'rxjs/operators';
|
||||
import { compare, Operation } from 'fast-json-patch';
|
||||
import { ObjectCacheService } from '../cache/object-cache.service';
|
||||
import { DSpaceObject } from '../shared/dspace-object.model';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { NotificationsService } from '../../shared/notifications/notifications.service';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import {
|
||||
configureRequest,
|
||||
filterSuccessfulResponses,
|
||||
getResponseFromSelflink
|
||||
getResponseFromEntry
|
||||
} from '../shared/operators';
|
||||
import { ResponseCacheEntry } from '../cache/response-cache.reducer';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { DSOSuccessResponse, ErrorResponse } from '../cache/response-cache.models';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { NotificationsService } from '../../shared/notifications/notifications.service';
|
||||
import { DSOSuccessResponse, ErrorResponse, RestResponse } from '../cache/response.models';
|
||||
import { NotificationOptions } from '../../shared/notifications/models/notification-options.model';
|
||||
|
||||
export abstract class DataService<TNormalized extends NormalizedObject, TDomain> {
|
||||
protected abstract responseCache: ResponseCacheService;
|
||||
protected abstract requestService: RequestService;
|
||||
protected abstract rdbService: RemoteDataBuildService;
|
||||
protected abstract store: Store<CoreState>;
|
||||
protected abstract linkPath: string;
|
||||
protected abstract halService: HALEndpointService;
|
||||
protected abstract objectCache: ObjectCacheService;
|
||||
protected abstract authService: AuthService;
|
||||
protected abstract notificationsService: NotificationsService;
|
||||
protected abstract http: HttpClient;
|
||||
|
||||
public abstract getScopedEndpoint(scope: string): Observable<string>
|
||||
public abstract getBrowseEndpoint(options: FindAllOptions, linkPath?: string): Observable<string>
|
||||
|
||||
protected getFindAllHref(endpoint, options: FindAllOptions = {}): Observable<string> {
|
||||
protected getFindAllHref(options: FindAllOptions = {}, linkPath?: string): Observable<string> {
|
||||
let result: Observable<string>;
|
||||
const args = [];
|
||||
|
||||
if (hasValue(options.scopeID)) {
|
||||
result = this.getScopedEndpoint(options.scopeID).distinctUntilChanged();
|
||||
} else {
|
||||
result = Observable.of(endpoint);
|
||||
}
|
||||
|
||||
result = this.getBrowseEndpoint(options, linkPath);
|
||||
if (hasValue(options.currentPage) && typeof options.currentPage === 'number') {
|
||||
/* TODO: this is a temporary fix for the pagination start index (0 or 1) discrepancy between the rest and the frontend respectively */
|
||||
args.push(`page=${options.currentPage - 1}`);
|
||||
}
|
||||
|
||||
if (hasValue(options.elementsPerPage)) {
|
||||
args.push(`size=${options.elementsPerPage}`);
|
||||
}
|
||||
|
||||
if (hasValue(options.sort)) {
|
||||
args.push(`sort=${options.sort.field},${options.sort.direction}`);
|
||||
}
|
||||
|
||||
if (hasValue(options.startsWith)) {
|
||||
args.push(`startsWith=${options.startsWith}`);
|
||||
}
|
||||
|
||||
if (isNotEmpty(args)) {
|
||||
return result.map((href: string) => new URLCombiner(href, `?${args.join('&')}`).toString());
|
||||
return result.pipe(map((href: string) => new URLCombiner(href, `?${args.join('&')}`).toString()));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
findAll(options: FindAllOptions = {}): Observable<RemoteData<PaginatedList<TDomain>>> {
|
||||
const hrefObs = this.halService.getEndpoint(this.linkPath).filter((href: string) => isNotEmpty(href))
|
||||
.flatMap((endpoint: string) => this.getFindAllHref(endpoint, options));
|
||||
const hrefObs = this.getFindAllHref(options);
|
||||
|
||||
hrefObs
|
||||
.filter((href: string) => hasValue(href))
|
||||
.take(1)
|
||||
hrefObs.pipe(
|
||||
filter((href: string) => hasValue(href)),
|
||||
take(1))
|
||||
.subscribe((href: string) => {
|
||||
const request = new FindAllRequest(this.requestService.generateRequestId(), href, options);
|
||||
this.requestService.configure(request);
|
||||
@@ -98,11 +88,11 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain>
|
||||
}
|
||||
|
||||
findById(id: string): Observable<RemoteData<TDomain>> {
|
||||
const hrefObs = this.halService.getEndpoint(this.linkPath)
|
||||
.map((endpoint: string) => this.getFindByIDHref(endpoint, id));
|
||||
const hrefObs = this.halService.getEndpoint(this.linkPath).pipe(
|
||||
map((endpoint: string) => this.getFindByIDHref(endpoint, id)));
|
||||
|
||||
hrefObs
|
||||
.first((href: string) => hasValue(href))
|
||||
hrefObs.pipe(
|
||||
first((href: string) => hasValue(href)))
|
||||
.subscribe((href: string) => {
|
||||
const request = new FindByIDRequest(this.requestService.generateRequestId(), href, id);
|
||||
this.requestService.configure(request);
|
||||
@@ -116,6 +106,28 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain>
|
||||
return this.rdbService.buildSingle<TNormalized, TDomain>(href);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new patch to the object cache to a specified object
|
||||
* @param {string} href The selflink of the object that will be patched
|
||||
* @param {Operation[]} operations The patch operations to be performed
|
||||
*/
|
||||
patch(href: string, operations: Operation[]) {
|
||||
this.objectCache.addPatch(href, operations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new patch to the object cache
|
||||
* The patch is derived from the differences between the given object and its version in the object cache
|
||||
* @param {DSpaceObject} object The given object
|
||||
*/
|
||||
update(object: DSpaceObject) {
|
||||
const oldVersion = this.objectCache.getBySelfLink(object.self);
|
||||
const operations = compare(oldVersion, object);
|
||||
if (isNotEmpty(operations)) {
|
||||
this.objectCache.addPatch(object.self, operations);
|
||||
}
|
||||
}
|
||||
|
||||
create(dso: TDomain, parentUUID: string): Observable<RemoteData<TDomain>> {
|
||||
const requestId = this.requestService.generateRequestId();
|
||||
const endpoint$ = this.halService.getEndpoint(this.linkPath).pipe(
|
||||
@@ -131,22 +143,20 @@ export abstract class DataService<TNormalized extends NormalizedObject, TDomain>
|
||||
);
|
||||
|
||||
const selfLink$ = request$.pipe(
|
||||
map((request: RestRequest) => request.href),
|
||||
getResponseFromSelflink(this.responseCache),
|
||||
map((response: ResponseCacheEntry) => {
|
||||
if (!response.response.isSuccessful && response.response instanceof ErrorResponse) {
|
||||
const errorResponse: ErrorResponse = response.response;
|
||||
this.notificationsService.error('Server Error:', errorResponse.errorMessage, new NotificationOptions(-1));
|
||||
getResponseFromEntry(),
|
||||
map((response: RestResponse) => {
|
||||
if (!response.isSuccessful && response instanceof ErrorResponse) {
|
||||
this.notificationsService.error('Server Error:', response.errorMessage, new NotificationOptions(-1));
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
return response;
|
||||
}),
|
||||
filterSuccessfulResponses(),
|
||||
map((entry: ResponseCacheEntry) => entry.response),
|
||||
map((response: DSOSuccessResponse) => {
|
||||
return response.resourceSelfLinks[0];
|
||||
}),
|
||||
distinctUntilChanged()
|
||||
);
|
||||
) as Observable<string>;
|
||||
return this.rdbService.buildSingle(selfLink$) as Observable<RemoteData<TDomain>>;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user