paperless-ngx/src-ui/src/app/services/rest/abstract-paperless-service.ts

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-10-27 01:10:18 +01:00
import { HttpClient, HttpParams } from '@angular/common/http'
2020-12-04 01:22:14 +01:00
import { Observable, of, Subject } from 'rxjs'
import { map, publishReplay, refCount } from 'rxjs/operators'
2020-10-27 01:10:18 +01:00
import { ObjectWithId } from 'src/app/data/object-with-id'
import { Results } from 'src/app/data/results'
import { environment } from 'src/environments/environment'
export abstract class AbstractPaperlessService<T extends ObjectWithId> {
protected baseUrl: string = environment.apiBaseUrl
constructor(protected http: HttpClient, private resourceName: string) { }
protected getResourceUrl(id?: number, action?: string): string {
let url = `${this.baseUrl}${this.resourceName}/`
if (id) {
url += `${id}/`
2020-10-27 17:05:14 +01:00
}
if (action) {
url += `${action}/`
2020-10-27 01:10:18 +01:00
}
return url
}
2020-11-08 16:58:06 +01:00
private getOrderingQueryParam(sortField: string, sortDirection: string) {
if (sortField && sortDirection) {
return (sortDirection == 'des' ? '-' : '') + sortField
} else if (sortField) {
return sortField
} else {
return null
}
}
list(page?: number, pageSize?: number, sortField?: string, sortDirection?: string, extraParams?): Observable<Results<T>> {
2020-10-27 01:10:18 +01:00
let httpParams = new HttpParams()
if (page) {
httpParams = httpParams.set('page', page.toString())
}
if (pageSize) {
httpParams = httpParams.set('page_size', pageSize.toString())
}
2020-11-08 16:58:06 +01:00
let ordering = this.getOrderingQueryParam(sortField, sortDirection)
2020-10-27 01:10:18 +01:00
if (ordering) {
httpParams = httpParams.set('ordering', ordering)
}
for (let extraParamKey in extraParams) {
2020-11-04 00:01:08 +01:00
if (extraParams[extraParamKey] != null) {
2020-10-27 01:10:18 +01:00
httpParams = httpParams.set(extraParamKey, extraParams[extraParamKey])
}
}
return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams})
}
2020-12-04 01:22:14 +01:00
private _listAll: Observable<Results<T>>
listAll(ordering?: string, extraParams?): Observable<Results<T>> {
2020-12-04 01:22:14 +01:00
if (!this._listAll) {
this._listAll = this.list(1, 100000, ordering, extraParams).pipe(
publishReplay(1),
refCount()
)
}
return this._listAll
}
getCached(id: number): Observable<T> {
return this.listAll().pipe(
map(list => list.results.find(o => o.id == id))
)
}
getCachedMany(ids: number[]): Observable<T[]> {
return this.listAll().pipe(
map(list => ids.map(id => list.results.find(o => o.id == id)))
)
}
2020-10-27 01:10:18 +01:00
get(id: number): Observable<T> {
return this.http.get<T>(this.getResourceUrl(id))
}
create(o: T): Observable<T> {
2020-12-04 01:22:14 +01:00
this._listAll = null
2020-10-27 01:10:18 +01:00
return this.http.post<T>(this.getResourceUrl(), o)
}
delete(o: T): Observable<any> {
2020-12-04 01:22:14 +01:00
this._listAll = null
2020-10-27 01:10:18 +01:00
return this.http.delete(this.getResourceUrl(o.id))
}
update(o: T): Observable<T> {
2020-12-04 01:22:14 +01:00
this._listAll = null
2020-10-27 01:10:18 +01:00
return this.http.put<T>(this.getResourceUrl(o.id), o)
}
}