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 { Table } from '../core/table'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }), }; const url = '/api/tables'; const serviceName = 'TableService'; @Injectable({ providedIn: 'root', }) export class TableService { 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'))) ); } running(): Observable { return >( this.http .get(`${url}/running`) .pipe(catchError(this.log.handleError(serviceName, 'running'))) ); } save(tables: Table): Observable
{ return >( this.http .post
(url, tables, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'save'))) ); } update(tables: Table): Observable
{ return >( this.http .put
(`${url}/${tables.id}`, tables, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'update'))) ); } updateSortOrder(list: Table[]): Observable { return >( this.http .post(`${url}/list`, list, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'updateSortOrder'))) ); } saveOrUpdate(tables: Table): Observable
{ if (!tables.id) { return this.save(tables); } return this.update(tables); } delete(id: string): Observable
{ return >( this.http .delete
(`${url}/${id}`, httpOptions) .pipe(catchError(this.log.handleError(serviceName, 'delete'))) ); } }