mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-10-16 02:46:16 -05:00
added paperless ui
This commit is contained in:
58
src-ui/src/app/services/rest/abstract-paperless-service.ts
Normal file
58
src-ui/src/app/services/rest/abstract-paperless-service.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { HttpClient, HttpParams } from '@angular/common/http'
|
||||
import { Observable } from 'rxjs'
|
||||
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}/`
|
||||
if (action) {
|
||||
url += `${action}/`
|
||||
}
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
list(page?: number, pageSize?: number, ordering?: string, extraParams?): Observable<Results<T>> {
|
||||
let httpParams = new HttpParams()
|
||||
if (page) {
|
||||
httpParams = httpParams.set('page', page.toString())
|
||||
}
|
||||
if (pageSize) {
|
||||
httpParams = httpParams.set('page_size', pageSize.toString())
|
||||
}
|
||||
if (ordering) {
|
||||
httpParams = httpParams.set('ordering', ordering)
|
||||
}
|
||||
for (let extraParamKey in extraParams) {
|
||||
if (extraParams[extraParamKey]) {
|
||||
httpParams = httpParams.set(extraParamKey, extraParams[extraParamKey])
|
||||
}
|
||||
}
|
||||
return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams})
|
||||
}
|
||||
|
||||
get(id: number): Observable<T> {
|
||||
return this.http.get<T>(this.getResourceUrl(id))
|
||||
}
|
||||
|
||||
create(o: T): Observable<T> {
|
||||
return this.http.post<T>(this.getResourceUrl(), o)
|
||||
}
|
||||
|
||||
delete(o: T): Observable<any> {
|
||||
return this.http.delete(this.getResourceUrl(o.id))
|
||||
}
|
||||
|
||||
update(o: T): Observable<T> {
|
||||
return this.http.put<T>(this.getResourceUrl(o.id), o)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user