Projekt

Obecné

Profil

Stáhnout (1.39 KB) Statistiky
| Větev: | Tag: | Revize:
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 fd5ab42e Hung Hoang
import {environment} from '../../../environments/environment';
5 1d169f6d Hung Hoang
import {MatSnackBar} from '@angular/material';
6 41741550 Hung Hoang
7
@Injectable({
8
  providedIn: 'root'
9
})
10
export class BasicService {
11 b919891c Hung Hoang
  protected baseUrl = environment.apiUrl;
12 41741550 Hung Hoang
13 1d169f6d Hung Hoang
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar) { }
14 4bcef705 Hung Hoang
15 41741550 Hung Hoang
  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 1d169f6d Hung Hoang
24
    this.snackBar.open('Komunikace se serverem se nezdařila', 'Zavřít');
25 41741550 Hung Hoang
    return throwError(
26
      'Something bad happened; please try again later.');
27
  }
28
29 30989178 Hung Hoang
  /**
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 4bcef705 Hung Hoang
  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 41741550 Hung Hoang
}