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