This commit is contained in:
Amritanshu
2019-06-15 23:09:43 +05:30
parent 3dcf1c4c42
commit 459ab244ff
70 changed files with 2140 additions and 103 deletions

View 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'))
);
}
}