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 |
1d169f6d
|
Hung Hoang
|
import {MatSnackBar} from '@angular/material';
|
5 |
5d32cbea
|
Václav Jirák
|
import {TranslateService} from '@ngx-translate/core';
|
6 |
4ccb6b62
|
Pavel Fidransky
|
import { Config } from '../util/config.service';
|
7 |
|
|
|
8 |
41741550
|
Hung Hoang
|
@Injectable({
|
9 |
|
|
providedIn: 'root'
|
10 |
|
|
})
|
11 |
|
|
export class BasicService {
|
12 |
4ccb6b62
|
Pavel Fidransky
|
protected baseUrl: string;
|
13 |
41741550
|
Hung Hoang
|
|
14 |
4ccb6b62
|
Pavel Fidransky
|
constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
|
15 |
|
|
this.baseUrl = config.baseUrl;
|
16 |
|
|
}
|
17 |
4bcef705
|
Hung Hoang
|
|
18 |
41741550
|
Hung Hoang
|
protected handleError(error: HttpErrorResponse) {
|
19 |
5d32cbea
|
Václav Jirák
|
let errMsg;
|
20 |
|
|
if (!error.error.error) {
|
21 |
|
|
this.translateService.get('error.serverCommunication').subscribe((res: string) => {
|
22 |
|
|
errMsg = res;
|
23 |
|
|
});
|
24 |
41741550
|
Hung Hoang
|
} else {
|
25 |
5d32cbea
|
Václav Jirák
|
errMsg = error.error.message;
|
26 |
41741550
|
Hung Hoang
|
}
|
27 |
1d169f6d
|
Hung Hoang
|
|
28 |
5d32cbea
|
Václav Jirák
|
this.snackBar.open(errMsg, 'X');
|
29 |
|
|
|
30 |
|
|
return throwError(errMsg);
|
31 |
41741550
|
Hung Hoang
|
}
|
32 |
|
|
|
33 |
30989178
|
Hung Hoang
|
/**
|
34 |
|
|
* Creates http parameters (query for request) for given
|
35 |
|
|
* object (parameter - value), if the value is null
|
36 |
|
|
* it's not added into the query
|
37 |
|
|
* @param params object from which the query is created
|
38 |
|
|
*/
|
39 |
4bcef705
|
Hung Hoang
|
protected createParams(params: any) {
|
40 |
|
|
let httpParams = new HttpParams();
|
41 |
|
|
for (const key in params) {
|
42 |
|
|
if (params.hasOwnProperty(key)) {
|
43 |
|
|
if (params[key] != null) {
|
44 |
|
|
httpParams = httpParams.set(key, params[key]);
|
45 |
|
|
}
|
46 |
|
|
}
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
return httpParams;
|
50 |
|
|
}
|
51 |
41741550
|
Hung Hoang
|
}
|