import {Injectable} from '@angular/core'; import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {ErrorLoggerService} from '../core/error-logger.service'; import {catchError} from 'rxjs/operators'; import {Observable} from 'rxjs/internal/Observable'; import {Section} from '../core/section'; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) }; const url = '/v1/sections'; const serviceName = 'SectionService'; @Injectable({ providedIn: 'root' }) export class SectionService { constructor(private http: HttpClient, private log: ErrorLoggerService) { } get(id: string): Observable
{ const getUrl: string = (id === null) ? `${url}/new` : `${url}/${id}`; return >this.http.get
(getUrl) .pipe( catchError(this.log.handleError(serviceName, `get id=${id}`)) ); } list(): Observable { const options = {params: new HttpParams().set('l', '')}; return >this.http.get(url, options) .pipe( catchError(this.log.handleError(serviceName, 'list')) ); } save(section: Section): Observable
{ return >this.http.post
(`${url}/new`, section, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'save')) ); } update(section: Section): Observable
{ return >this.http.put
(`${url}/${section.id}`, section, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'update')) ); } saveOrUpdate(section: Section): Observable
{ if (!section.id) { return this.save(section); } else { return this.update(section); } } delete(id: string): Observable
{ return >this.http.delete
(`${url}/${id}`, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'delete')) ); } }