import {Injectable} from '@angular/core'; import {Observable} from 'rxjs/internal/Observable'; import {catchError} from 'rxjs/operators'; import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {Employee} from './employee'; import {ErrorLoggerService} from '../core/error-logger.service'; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) }; const url = '/api/employees'; const serviceName = 'EmployeeService'; @Injectable({providedIn: 'root'}) export class EmployeeService { 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(employee: Employee): Observable { return >this.http.post(`${url}`, employee, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'save')) ); } update(employee: Employee): Observable { return >this.http.put(`${url}/${employee.id}`, employee, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'update')) ); } saveOrUpdate(employee: Employee): Observable { if (!employee.id) { return this.save(employee); } else { return this.update(employee); } } delete(id: string): Observable { return >this.http.delete(`${url}/${id}`, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'delete')) ); } autocomplete(term: string): Observable { const options = {params: new HttpParams().set('q', term)}; return >this.http.get(`${url}/query`, options) .pipe( catchError(this.log.handleError(serviceName, 'autocomplete')) ); } }