import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { ErrorLoggerService } from '../core/error-logger.service'; import { SettleOption } from '../core/settle-option'; import { VoucherType } from '../sales/bills/voucher-type'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }), }; const url = '/api/settle-options'; const serviceName = 'SettleOptionService'; @Injectable({ providedIn: 'root', }) export class SettleOptionService { constructor( private http: HttpClient, private log: ErrorLoggerService, ) {} get(id: string | null): Observable { const getUrl: string = id === null ? url : `${url}/${id}`; return this.http .get(getUrl) .pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable; } list(): Observable { return this.http .get(`${url}/list`) .pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable; } listForType(voucherType: VoucherType): Observable { return this.http .get(`${url}/for-type/${voucherType}`) .pipe(catchError(this.log.handleError(serviceName, `listForType voucherType=${voucherType}`))) as Observable< SettleOption[] >; } save(settleOption: SettleOption): Observable { return this.http .post(url, settleOption, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable; } update(settleOption: SettleOption): Observable { return this.http .put(`${url}/${settleOption.id}`, settleOption, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable; } saveOrUpdate(settleOption: SettleOption): Observable { if (!settleOption.id) { return this.save(settleOption); } return this.update(settleOption); } delete(id: number): Observable { return this.http .delete(`${url}/${id}`, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable; } }