1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient, HttpErrorResponse, HttpParams} from '@angular/common/http';
|
3
|
import {throwError} from 'rxjs';
|
4
|
import {MatSnackBar} from '@angular/material';
|
5
|
import {TranslateService} from '@ngx-translate/core';
|
6
|
import { Config } from '../util/config.service';
|
7
|
|
8
|
@Injectable({
|
9
|
providedIn: 'root'
|
10
|
})
|
11
|
export class BasicService {
|
12
|
protected baseUrl: string;
|
13
|
|
14
|
constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
|
15
|
this.baseUrl = config.baseUrl;
|
16
|
}
|
17
|
|
18
|
protected handleError(error: HttpErrorResponse) {
|
19
|
let errMsg;
|
20
|
if (!error.error.error) {
|
21
|
this.translateService.get('error.serverCommunication').subscribe((res: string) => {
|
22
|
errMsg = res;
|
23
|
});
|
24
|
} else {
|
25
|
errMsg = error.error.message;
|
26
|
}
|
27
|
|
28
|
this.snackBar.open(errMsg, 'X');
|
29
|
|
30
|
return throwError(errMsg);
|
31
|
}
|
32
|
|
33
|
/**
|
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
|
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
|
}
|