Tax done
This commit is contained in:
65
bookie/src/app/taxes/tax.service.ts
Normal file
65
bookie/src/app/taxes/tax.service.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
|
||||
import {ErrorLoggerService} from '../core/error-logger.service';
|
||||
import {catchError} from 'rxjs/operators';
|
||||
import {Observable} from 'rxjs/internal/Observable';
|
||||
import {Tax} from '../core/tax';
|
||||
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
||||
};
|
||||
const url = '/v1/taxes';
|
||||
const serviceName = 'TaxService';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TaxService {
|
||||
constructor(private http: HttpClient, private log: ErrorLoggerService) {
|
||||
}
|
||||
|
||||
get(id: string): Observable<Tax> {
|
||||
const getUrl: string = (id === null) ? `${url}/new` : `${url}/${id}`;
|
||||
return <Observable<Tax>>this.http.get<Tax>(getUrl)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, `get id=${id}`))
|
||||
);
|
||||
}
|
||||
|
||||
list(): Observable<Tax[]> {
|
||||
const options = {params: new HttpParams().set('l', '')};
|
||||
return <Observable<Tax[]>>this.http.get<Tax[]>(url, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
save(tax: Tax): Observable<Tax> {
|
||||
return <Observable<Tax>>this.http.post<Tax>(`${url}/new`, tax, httpOptions)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'save'))
|
||||
);
|
||||
}
|
||||
|
||||
update(tax: Tax): Observable<Tax> {
|
||||
return <Observable<Tax>>this.http.put<Tax>(`${url}/${tax.id}`, tax, httpOptions)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'update'))
|
||||
);
|
||||
}
|
||||
|
||||
saveOrUpdate(tax: Tax): Observable<Tax> {
|
||||
if (!tax.id) {
|
||||
return this.save(tax);
|
||||
} else {
|
||||
return this.update(tax);
|
||||
}
|
||||
}
|
||||
|
||||
delete(id: string): Observable<Tax> {
|
||||
return <Observable<Tax>>this.http.delete<Tax>(`${url}/${id}`, httpOptions)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'delete'))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user