60 lines
1.9 KiB
TypeScript
60 lines
1.9 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 { Case } from '../core/case';
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/cases';
|
|
const serviceName = 'CaseService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class CaseService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string | null): Observable<Case> {
|
|
const getUrl: string = id === null ? url : `${url}/${id}`;
|
|
return this.http
|
|
.get<Case>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Case>;
|
|
}
|
|
|
|
list(caseSource: string): Observable<Case[]> {
|
|
const listUrl: string = caseSource === '' ? `${url}/list` : `${url}/list/${caseSource}`;
|
|
return this.http
|
|
.get<Case[]>(listUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<Case[]>;
|
|
}
|
|
|
|
save(case_: Case): Observable<Case> {
|
|
return this.http
|
|
.post<Case>(url, case_, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Case>;
|
|
}
|
|
|
|
update(case_: Case): Observable<Case> {
|
|
return this.http
|
|
.put<Case>(`${url}/${case_.id}`, case_, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Case>;
|
|
}
|
|
|
|
saveOrUpdate(case_: Case): Observable<Case> {
|
|
if (!case_.id) {
|
|
return this.save(case_);
|
|
}
|
|
return this.update(case_);
|
|
}
|
|
|
|
delete(id: string): Observable<Case> {
|
|
return this.http
|
|
.delete<Case>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Case>;
|
|
}
|
|
}
|