1 |
4bcef705
|
Hung Hoang
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {HttpClient, HttpErrorResponse, HttpParams} from '@angular/common/http';
|
3 |
41741550
|
Hung Hoang
|
import {throwError} from 'rxjs';
|
4 |
b919891c
|
Hung Hoang
|
import {environment} from '../../environments/environment';
|
5 |
41741550
|
Hung Hoang
|
|
6 |
|
|
@Injectable({
|
7 |
|
|
providedIn: 'root'
|
8 |
|
|
})
|
9 |
|
|
export class BasicService {
|
10 |
b919891c
|
Hung Hoang
|
protected baseUrl = environment.apiUrl;
|
11 |
41741550
|
Hung Hoang
|
|
12 |
4bcef705
|
Hung Hoang
|
constructor(protected http: HttpClient) { }
|
13 |
|
|
|
14 |
41741550
|
Hung Hoang
|
protected handleError(error: HttpErrorResponse) {
|
15 |
|
|
if (error.error instanceof ErrorEvent) {
|
16 |
|
|
console.error('An error occurred:', error.error.message);
|
17 |
|
|
} else {
|
18 |
|
|
console.error(
|
19 |
|
|
`Backend returned code ${error.status}, ` +
|
20 |
|
|
`body was: ${error.error}`);
|
21 |
|
|
}
|
22 |
|
|
return throwError(
|
23 |
|
|
'Something bad happened; please try again later.');
|
24 |
|
|
}
|
25 |
|
|
|
26 |
4bcef705
|
Hung Hoang
|
protected createParams(params: any) {
|
27 |
|
|
let httpParams = new HttpParams();
|
28 |
|
|
for (const key in params) {
|
29 |
|
|
if (params.hasOwnProperty(key)) {
|
30 |
|
|
if (params[key] != null) {
|
31 |
|
|
httpParams = httpParams.set(key, params[key]);
|
32 |
|
|
}
|
33 |
|
|
}
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
return httpParams;
|
37 |
|
|
}
|
38 |
41741550
|
Hung Hoang
|
}
|