import { HttpClient, HttpHeaders, HttpParams } 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 { GuestBook } from './guest-book'; import { GuestBookList } from './guest-book-list'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }), }; const url = '/api/guest-book'; const serviceName = 'GuestBookService'; @Injectable({ providedIn: 'root' }) export class GuestBookService { 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(date: string): Observable { const options = { params: new HttpParams().set('q', date === null ? '' : date) }; return >( this.http .get(`${url}/list`, options) .pipe(catchError(this.log.handleError(serviceName, 'list'))) ); } save(guestBook: GuestBook): Observable { return >( this.http .post(url, guestBook, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'save'))) ); } update(guestBook: GuestBook): Observable { return >( this.http .put(`${url}/${guestBook.id}`, guestBook, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'update'))) ); } saveOrUpdate(guestBook: GuestBook): Observable { if (!guestBook.id) { return this.save(guestBook); } return this.update(guestBook); } delete(id: string): Observable { return >( this.http .delete(`${url}/${id}`, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'delete'))) ); } }