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 { Role } from './role'; const url = '/api/roles'; const serviceName = 'RoleService'; @Injectable({ providedIn: 'root', }) export class RoleService { 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 as Observable=${id}`))) as Observable; } list(): Observable { return this.http .get(`${url}/list`) .pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable; } save(role: Role): Observable { return this.http .post(`${url}`, role) .pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable; } update(role: Role): Observable { return this.http .put(`${url}/${role.id}`, role) .pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable; } saveOrUpdate(role: Role): Observable { if (!role.id) { return this.save(role); } return this.update(role); } delete(id: string): Observable { return this.http .delete(`${url}/${id}`) .pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable; } }