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 { ProductGroup } from '../core/product-group'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }), }; const url = '/api/product-groups'; const serviceName = 'ProductGroupService'; @Injectable({ providedIn: 'root', }) export class ProductGroupService { 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'))) ); } save(productGroup: ProductGroup): Observable { return >( this.http .post(`${url}`, productGroup, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'save'))) ); } update(productGroup: ProductGroup): Observable { return >( this.http .put(`${url}/${productGroup.id}`, productGroup, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'update'))) ); } saveOrUpdate(productGroup: ProductGroup): Observable { if (!productGroup.id) { return this.save(productGroup); } return this.update(productGroup); } delete(id: string): Observable { return >( this.http .delete(`${url}/${id}`, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'delete'))) ); } }