import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { ErrorLoggerService } from '../core/error-logger.service'; import { catchError } from 'rxjs/operators'; import { Observable } from 'rxjs/internal/Observable'; import { SaleCategory } from '../core/sale-category'; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) }; const url = '/api/sale-categories'; const serviceName = 'SaleCategoryService'; @Injectable({ providedIn: 'root' }) export class SaleCategoryService { constructor(private http: HttpClient, private log: ErrorLoggerService) { } get(id: string): Observable { const getUrl: string = (id === null) ? url : `${url}/${id}`; return >this.http.get(getUrl) .pipe( catchError(this.log.handleError(serviceName, `get id=${id}`)) ); } list(): Observable { return >this.http.get(`${url}/list`) .pipe( catchError(this.log.handleError(serviceName, 'list')) ); } listForDiscount(): Observable<{name: string, discount: number}[]> { return >this.http.get<{name: string, discount: number}[]>(`${url}/for-discount`) .pipe( catchError(this.log.handleError(serviceName, 'list')) ); } save(saleCategory: SaleCategory): Observable { return >this.http.post(url, saleCategory, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'save')) ); } update(saleCategory: SaleCategory): Observable { return >this.http.put(`${url}/${saleCategory.id}`, saleCategory, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'update')) ); } saveOrUpdate(saleCategory: SaleCategory): Observable { if (!saleCategory.id) { return this.save(saleCategory); } else { return this.update(saleCategory); } } delete(id: string): Observable { return >this.http.delete(`${url}/${id}`, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'delete')) ); } }