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