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 {Role} from './role'; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) }; const url = '/v1/roles'; const serviceName = 'RoleService'; @Injectable({ providedIn: 'root' }) export class RoleService { 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}/list`, options) .pipe( catchError(this.log.handleError(serviceName, 'list')) ); } save(role: Role): Observable { return >this.http.put(`${url}/new`, role, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'save')) ); } update(role: Role): Observable { return >this.http.post(`${url}/${role.id}`, role, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'update')) ); } saveOrUpdate(role: Role): Observable { if (!role.id) { return this.save(role); } else { return this.update(role); } } delete(id: string): Observable { return >this.http.delete(`${url}/${id}`, httpOptions) .pipe( catchError(this.log.handleError(serviceName, 'delete')) ); } }