methods.service.ts 2.2 KB
Newer Older
Vitali Stupin's avatar
Vitali Stupin committed
1
import { Injectable, Output, EventEmitter } from '@angular/core';
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
2
3
4
5
6
7
8
9
10
11
12
13
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Subsystem } from './subsystem';

@Injectable({
  providedIn: 'root'
})
export class MethodsService {

  //private apiUrlBase = 'https://www.x-tee.ee/catalogue/EE/wsdls/';
  public apiUrlBase = 'http://localhost/';
14
  private limit: number = 10;
Vitali Stupin's avatar
Vitali Stupin committed
15
  private offset: number = 0;
16
  private nonEmpty: boolean = false;
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
17
18
19
  private apiService = 'index.json';
  private apiUrl = this.apiUrlBase + this.apiService;
  private subsystems: Subsystem[];
Vitali Stupin's avatar
Vitali Stupin committed
20
21

  @Output() subsystemsUpdated: EventEmitter<any> = new EventEmitter();
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
22
23

  constructor(private http: HttpClient) {
Vitali Stupin's avatar
Vitali Stupin committed
24
    this.http.get<Subsystem[]>(this.apiUrl)
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
25
26
    .pipe(
      catchError(this.handleError('getMethods', []))
Vitali Stupin's avatar
Vitali Stupin committed
27
28
29
30
31
32
33
34
35
36
37
    ).subscribe(subsystems => {
      this.subsystems = subsystems;
      this.signalRefresh();
    })
  }

  private signalRefresh() {
    this.subsystemsUpdated.emit(null);
  }

  private filtered(data: Subsystem[]): Subsystem[] {
38
39
40
    return data
      .filter(subsystem => (!this.nonEmpty || subsystem.methods.length))
      .slice(this.offset, this.limit);
Vitali Stupin's avatar
Vitali Stupin committed
41
42
43
44
  }

  getMethods(): Subsystem[] {
    return this.filtered(this.subsystems);
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
45
46
  }

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  setNonEmpty(nonEmpty: boolean) {
    this.nonEmpty = nonEmpty;
    this.signalRefresh();
  }

  setLimit (limit: string) {
    switch(limit) {
      case '20':
        this.limit = 20;
        break;
      case '50':
        this.limit = 50;
        break;
      case 'All':
        this.limit = 1000000;
        break;
      default:
        this.limit = 10;
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
65
    }
Vitali Stupin's avatar
Vitali Stupin committed
66
    this.signalRefresh();
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  }


  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}