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