update services

This commit is contained in:
Dan Percic 2021-10-06 01:06:58 +03:00
parent aed0b51a6d
commit 61fb1a4006
2 changed files with 35 additions and 29 deletions

View File

@ -1,9 +1,9 @@
import { Inject, Injectable, InjectionToken, Injector, Optional } from '@angular/core';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { distinctUntilChanged, map, tap } from 'rxjs/operators';
import { IListable } from '../models';
import { GenericService } from '../../services';
import { getLength } from '../../utils';
import { Inject, Injectable, InjectionToken, Injector, Optional } from "@angular/core";
import { BehaviorSubject, Observable, Subject } from "rxjs";
import { distinctUntilChanged, map, tap } from "rxjs/operators";
import { IListable } from "../models";
import { GenericService } from "../../services";
import { getLength } from "../../utils";
/**
* This should be removed when refactoring is done
@ -26,8 +26,8 @@ export class EntitiesService<E extends IListable, I = E> extends GenericService<
constructor(
protected readonly _injector: Injector,
@Optional() @Inject(ENTITY_CLASS) private readonly _entityClass: new (entityInterface: I) => E,
@Optional() @Inject(ENTITY_PATH) protected readonly _defaultModelPath = '',
@Optional() @Inject(ENTITY_CLASS) private readonly _entityClass: new (entityInterface: I, ...args: unknown[]) => E,
@Optional() @Inject(ENTITY_PATH) protected readonly _defaultModelPath = ''
) {
super(_injector, _defaultModelPath);
this.all$ = this._all$.asObservable();
@ -42,18 +42,18 @@ export class EntitiesService<E extends IListable, I = E> extends GenericService<
private get _noData$(): Observable<boolean> {
return this.allLength$.pipe(
map(length => length === 0),
distinctUntilChanged(),
distinctUntilChanged()
);
}
loadAll(): Observable<E[]> {
return this.getAll().pipe(
map((entities: I[]) => entities.map(entity => new this._entityClass(entity))),
tap((entities: E[]) => this.setEntities(entities)),
tap((entities: E[]) => this.setEntities(entities))
);
}
loadAllIfNecessary(): Promise<unknown> | void {
loadAllIfEmpty(): Promise<unknown> | void {
if (!this.all.length) {
return this.loadAll().toPromise();
}

View File

@ -15,25 +15,31 @@ export interface QueryParam {
readonly value: string | number | boolean;
}
export abstract class GenericService<Entity> {
/**
* I for interface
* R for response
*/
export abstract class GenericService<I> {
protected readonly _http = this._injector.get(HttpClient);
protected constructor(protected readonly _injector: Injector, protected readonly _defaultModelPath: string) {}
get(): Observable<I[]>;
get(id: string, ...args: unknown[]): Observable<I>;
get(id?: string, ...args: unknown[]): Observable<I | I[]> {
return id ? this._getOne([id]) : this.getAll();
}
@Validate()
getAll<R = Entity>(modelPath = this._defaultModelPath): Observable<R[]> {
getAll<R = I>(modelPath = this._defaultModelPath): Observable<R[]> {
return this._http.get<R[]>(`/${encodeURI(modelPath)}`, {
headers: HeadersConfiguration.getHeaders({ contentType: false }),
observe: 'body',
observe: 'body'
});
}
@Validate()
delete(
@RequiredParam() body: unknown,
modelPath = this._defaultModelPath,
queryParams?: List<QueryParam>,
): Observable<unknown> {
delete(@RequiredParam() body: unknown, modelPath = this._defaultModelPath, queryParams?: List<QueryParam>): Observable<unknown> {
let path = `/${encodeURI(modelPath)}`;
if (typeof body === 'string') {
@ -44,48 +50,48 @@ export abstract class GenericService<Entity> {
body: body,
params: this._queryParams(queryParams),
headers: HeadersConfiguration.getHeaders({ contentType: false }),
observe: 'body',
observe: 'body'
});
}
@Validate()
protected _post<R = Entity>(
protected _post<R = I>(
@RequiredParam() body: unknown,
modelPath = this._defaultModelPath,
queryParams?: List<QueryParam>,
queryParams?: List<QueryParam>
): Observable<R> {
return this._http.post<R>(`/${encodeURI(modelPath)}`, body, {
params: this._queryParams(queryParams),
headers: HeadersConfiguration.getHeaders(),
observe: 'body',
observe: 'body'
});
}
@Validate()
protected _put<R = Entity>(
protected _put<R = I>(
@RequiredParam() body: unknown,
modelPath = this._defaultModelPath,
queryParams?: List<QueryParam>,
queryParams?: List<QueryParam>
): Observable<R> {
return this._http.put<R>(`/${encodeURI(modelPath)}`, body, {
params: this._queryParams(queryParams),
headers: HeadersConfiguration.getHeaders(),
observe: 'body',
observe: 'body'
});
}
@Validate()
protected _getOne<R = Entity>(
protected _getOne<R = I>(
@RequiredParam() path: List,
modelPath = this._defaultModelPath,
queryParams?: List<QueryParam>,
queryParams?: List<QueryParam>
): Observable<R> {
const entityPath = path.map(item => encodeURIComponent(item)).join('/');
return this._http.get<R>(`/${encodeURI(modelPath)}/${entityPath}`, {
headers: HeadersConfiguration.getHeaders({ contentType: false }),
params: this._queryParams(queryParams),
observe: 'body',
observe: 'body'
});
}