From 61fb1a40063e27f335b0345552b8874eba2ac899 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Wed, 6 Oct 2021 01:06:58 +0300 Subject: [PATCH] update services --- src/lib/listing/services/entities.service.ts | 22 +++++----- src/lib/services/generic.service.ts | 42 +++++++++++--------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/lib/listing/services/entities.service.ts b/src/lib/listing/services/entities.service.ts index 9bd0823..95194c2 100644 --- a/src/lib/listing/services/entities.service.ts +++ b/src/lib/listing/services/entities.service.ts @@ -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 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 extends GenericService< private get _noData$(): Observable { return this.allLength$.pipe( map(length => length === 0), - distinctUntilChanged(), + distinctUntilChanged() ); } loadAll(): Observable { 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 | void { + loadAllIfEmpty(): Promise | void { if (!this.all.length) { return this.loadAll().toPromise(); } diff --git a/src/lib/services/generic.service.ts b/src/lib/services/generic.service.ts index 0bd5988..ff87130 100644 --- a/src/lib/services/generic.service.ts +++ b/src/lib/services/generic.service.ts @@ -15,25 +15,31 @@ export interface QueryParam { readonly value: string | number | boolean; } -export abstract class GenericService { +/** + * I for interface + * R for response + */ +export abstract class GenericService { protected readonly _http = this._injector.get(HttpClient); protected constructor(protected readonly _injector: Injector, protected readonly _defaultModelPath: string) {} + get(): Observable; + get(id: string, ...args: unknown[]): Observable; + get(id?: string, ...args: unknown[]): Observable { + return id ? this._getOne([id]) : this.getAll(); + } + @Validate() - getAll(modelPath = this._defaultModelPath): Observable { + getAll(modelPath = this._defaultModelPath): Observable { return this._http.get(`/${encodeURI(modelPath)}`, { headers: HeadersConfiguration.getHeaders({ contentType: false }), - observe: 'body', + observe: 'body' }); } @Validate() - delete( - @RequiredParam() body: unknown, - modelPath = this._defaultModelPath, - queryParams?: List, - ): Observable { + delete(@RequiredParam() body: unknown, modelPath = this._defaultModelPath, queryParams?: List): Observable { let path = `/${encodeURI(modelPath)}`; if (typeof body === 'string') { @@ -44,48 +50,48 @@ export abstract class GenericService { body: body, params: this._queryParams(queryParams), headers: HeadersConfiguration.getHeaders({ contentType: false }), - observe: 'body', + observe: 'body' }); } @Validate() - protected _post( + protected _post( @RequiredParam() body: unknown, modelPath = this._defaultModelPath, - queryParams?: List, + queryParams?: List ): Observable { return this._http.post(`/${encodeURI(modelPath)}`, body, { params: this._queryParams(queryParams), headers: HeadersConfiguration.getHeaders(), - observe: 'body', + observe: 'body' }); } @Validate() - protected _put( + protected _put( @RequiredParam() body: unknown, modelPath = this._defaultModelPath, - queryParams?: List, + queryParams?: List ): Observable { return this._http.put(`/${encodeURI(modelPath)}`, body, { params: this._queryParams(queryParams), headers: HeadersConfiguration.getHeaders(), - observe: 'body', + observe: 'body' }); } @Validate() - protected _getOne( + protected _getOne( @RequiredParam() path: List, modelPath = this._defaultModelPath, - queryParams?: List, + queryParams?: List ): Observable { const entityPath = path.map(item => encodeURIComponent(item)).join('/'); return this._http.get(`/${encodeURI(modelPath)}/${entityPath}`, { headers: HeadersConfiguration.getHeaders({ contentType: false }), params: this._queryParams(queryParams), - observe: 'body', + observe: 'body' }); }