Projekt

Obecné

Profil

Stáhnout (1.39 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse, HttpParams} from '@angular/common/http';
3
import {throwError} from 'rxjs';
4
import {environment} from '../../../environments/environment';
5
import {MatSnackBar} from '@angular/material';
6

    
7
@Injectable({
8
  providedIn: 'root'
9
})
10
export class BasicService {
11
  protected baseUrl = environment.apiUrl;
12

    
13
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar) { }
14

    
15
  protected handleError(error: HttpErrorResponse) {
16
    if (error.error instanceof ErrorEvent) {
17
      console.error('An error occurred:', error.error.message);
18
    } else {
19
      console.error(
20
        `Backend returned code ${error.status}, ` +
21
        `body was: ${error.error}`);
22
    }
23

    
24
    this.snackBar.open('Komunikace se serverem se nezdařila', 'Zavřít');
25
    return throwError(
26
      'Something bad happened; please try again later.');
27
  }
28

    
29
  /**
30
   * Creates http parameters (query for request) for given
31
   * object (parameter - value), if the value is null
32
   * it's not added into the query
33
   * @param params object from which the query is created
34
   */
35
  protected createParams(params: any) {
36
    let httpParams = new HttpParams();
37
    for (const key in params) {
38
      if (params.hasOwnProperty(key)) {
39
        if (params[key] != null) {
40
          httpParams = httpParams.set(key, params[key]);
41
        }
42
      }
43
    }
44

    
45
    return httpParams;
46
  }
47
}
(1-1/5)