2020-10-27 01:10:18 +01:00
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http'
|
2020-12-14 20:20:35 +01:00
|
|
|
import { Observable } from 'rxjs'
|
2020-12-04 01:22:14 +01:00
|
|
|
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
|
|
|
|
|
|
2023-07-20 11:02:33 -07:00
|
|
|
constructor(
|
|
|
|
|
protected http: HttpClient,
|
2023-09-14 13:32:43 -07:00
|
|
|
protected resourceName: string
|
2023-07-20 11:02:33 -07:00
|
|
|
) {}
|
2020-10-27 01:10:18 +01:00
|
|
|
|
2023-05-23 15:02:54 -07:00
|
|
|
protected getResourceUrl(id: number = null, action: string = null): string {
|
2020-10-27 01:10:18 +01:00
|
|
|
let url = `${this.baseUrl}${this.resourceName}/`
|
2023-05-23 15:02:54 -07:00
|
|
|
if (id !== null) {
|
2020-10-27 01:10:18 +01:00
|
|
|
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-12-14 19:26:36 +01:00
|
|
|
private getOrderingQueryParam(sortField: string, sortReverse: boolean) {
|
|
|
|
|
if (sortField) {
|
|
|
|
|
return (sortReverse ? '-' : '') + sortField
|
2020-11-08 16:58:06 +01:00
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 10:53:32 -08:00
|
|
|
list(
|
|
|
|
|
page?: number,
|
|
|
|
|
pageSize?: number,
|
|
|
|
|
sortField?: string,
|
|
|
|
|
sortReverse?: boolean,
|
|
|
|
|
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-12-14 19:26:36 +01:00
|
|
|
let ordering = this.getOrderingQueryParam(sortField, sortReverse)
|
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])
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-11 10:53:32 -08:00
|
|
|
return this.http.get<Results<T>>(this.getResourceUrl(), {
|
|
|
|
|
params: httpParams,
|
|
|
|
|
})
|
2020-10-27 01:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-04 01:22:14 +01:00
|
|
|
private _listAll: Observable<Results<T>>
|
|
|
|
|
|
2022-03-11 10:53:32 -08:00
|
|
|
listAll(
|
|
|
|
|
sortField?: string,
|
|
|
|
|
sortReverse?: boolean,
|
|
|
|
|
extraParams?
|
|
|
|
|
): Observable<Results<T>> {
|
2020-12-04 01:22:14 +01:00
|
|
|
if (!this._listAll) {
|
2022-03-11 10:53:32 -08:00
|
|
|
this._listAll = this.list(
|
|
|
|
|
1,
|
|
|
|
|
100000,
|
|
|
|
|
sortField,
|
|
|
|
|
sortReverse,
|
|
|
|
|
extraParams
|
|
|
|
|
).pipe(publishReplay(1), refCount())
|
2020-12-04 01:22:14 +01:00
|
|
|
}
|
|
|
|
|
return this._listAll
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCached(id: number): Observable<T> {
|
|
|
|
|
return this.listAll().pipe(
|
2022-03-11 10:53:32 -08:00
|
|
|
map((list) => list.results.find((o) => o.id == id))
|
2020-12-04 01:22:14 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCachedMany(ids: number[]): Observable<T[]> {
|
|
|
|
|
return this.listAll().pipe(
|
2022-03-11 10:53:32 -08:00
|
|
|
map((list) => ids.map((id) => list.results.find((o) => o.id == id)))
|
2020-12-04 01:22:14 +01:00
|
|
|
)
|
2020-11-04 17:23:36 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:31:50 +01:00
|
|
|
clearCache() {
|
|
|
|
|
this._listAll = null
|
|
|
|
|
}
|
|
|
|
|
|
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-28 12:31:50 +01:00
|
|
|
this.clearCache()
|
2020-10-27 01:10:18 +01:00
|
|
|
return this.http.post<T>(this.getResourceUrl(), o)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(o: T): Observable<any> {
|
2020-12-28 12:31:50 +01:00
|
|
|
this.clearCache()
|
2020-10-27 01:10:18 +01:00
|
|
|
return this.http.delete(this.getResourceUrl(o.id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(o: T): Observable<T> {
|
2020-12-28 12:31:50 +01:00
|
|
|
this.clearCache()
|
2020-10-27 01:10:18 +01:00
|
|
|
return this.http.put<T>(this.getResourceUrl(o.id), o)
|
|
|
|
|
}
|
2020-12-15 02:35:04 +01:00
|
|
|
|
|
|
|
|
patch(o: T): Observable<T> {
|
2020-12-28 12:31:50 +01:00
|
|
|
this.clearCache()
|
2020-12-15 02:35:04 +01:00
|
|
|
return this.http.patch<T>(this.getResourceUrl(o.id), o)
|
|
|
|
|
}
|
2020-12-11 14:46:38 +01:00
|
|
|
}
|