61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs/internal/Observable';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { CaseSource } from '../core/case-source';
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/case-sources';
|
|
const serviceName = 'CaseSourceService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class CaseSourceService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string | null): Observable<CaseSource> {
|
|
const getUrl: string = id === null ? url : `${url}/${id}`;
|
|
return this.http
|
|
.get<CaseSource>(getUrl)
|
|
.pipe(
|
|
catchError(this.log.handleError(serviceName, `get id=${id}`)),
|
|
) as Observable<CaseSource>;
|
|
}
|
|
|
|
list(): Observable<CaseSource[]> {
|
|
return this.http
|
|
.get<CaseSource[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<CaseSource[]>;
|
|
}
|
|
|
|
save(caseSource: CaseSource): Observable<CaseSource> {
|
|
return this.http
|
|
.post<CaseSource>(url, caseSource, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<CaseSource>;
|
|
}
|
|
|
|
update(caseSource: CaseSource): Observable<CaseSource> {
|
|
return this.http
|
|
.put<CaseSource>(`${url}/${caseSource.id}`, caseSource, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<CaseSource>;
|
|
}
|
|
|
|
saveOrUpdate(caseSource: CaseSource): Observable<CaseSource> {
|
|
if (!caseSource.id) {
|
|
return this.save(caseSource);
|
|
}
|
|
return this.update(caseSource);
|
|
}
|
|
|
|
delete(id: string): Observable<CaseSource> {
|
|
return this.http
|
|
.delete<CaseSource>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<CaseSource>;
|
|
}
|
|
}
|