import { HttpClient } 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 { Period } from './period'; const url = '/api/periods'; const serviceName = 'PeriodService'; @Injectable({ providedIn: 'root' }) export class PeriodService { 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; } save(period: Period): Observable { return this.http .post(`${url}`, period) .pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable; } update(period: Period): Observable { return this.http .put(`${url}/${period.id}`, period) .pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable; } saveOrUpdate(period: Period): Observable { if (!period.id) { return this.save(period); } return this.update(period); } delete(id: string): Observable { return this.http .delete(`${url}/${id}`) .pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable; } }