import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { SharedConstants } from '../constants/shared.constants'; export class ApiService { baseUrl: string = SharedConstants.END_POINT_URL; entityUrl: string; setUrl(entityUrl: string): void { this.entityUrl = entityUrl; } getUrl(): string { return this.entityUrl; } constructor(protected http: HttpClient) { } get(): Observable { return this.http.get(this.baseUrl + this.entityUrl); } getById(id: number): Observable { return this.http.get(this.baseUrl + this.entityUrl + '/'+id); } create(data: any): Observable { return this.http.post(this.baseUrl + this.entityUrl, data); } update(obj: Object): Observable { return this.http.put(this.baseUrl + this.entityUrl, obj); } deleteById(id: number): Observable { return this.http.delete(this.baseUrl + this.entityUrl+"/" + id); } test() {} private handleError(error: HttpErrorResponse) { if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. console.error('An error occurred:', error.error.message); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong, console.error( `Backend returned code ${error.status}, ` + `body was: ${error.error}` ); } // return an observable with a user-facing error message throw new Error('Something bad happened; please try again later.'); } }