1 import { Injectable } from '@angular/core';
2 import { HttpClient, HttpHeaders } from '@angular/common/http';
3 import { Observable, of, EMPTY } from 'rxjs';
4 import { catchError, map, tap } from 'rxjs/operators';
5 import { select, Store } from '@ngrx/store';
6 import { Vorgang } from './vorgang';
7 import { increment, decrement, reset } from './actions/vorgang';
8 import { State } from './reducers/vorgang';
14 export class VorgangService {
16 gpsUrl = 'http://localhost:1991/las/'; // URL to web api
18 headers: new HttpHeaders({
19 'Content-Type': 'text/plain',
20 'Accept': 'application/json'
25 private http: HttpClient,
26 private store: Store<{ vorgang: State }>) { }
28 /** POST: Einen neuen Vorgang erzeugen */
29 create(vorgang: Vorgang): Observable<Vorgang> {
33 this.getUrlSave(vorgang.vbId),
37 tap((result: Vorgang) => this.log(`Result for ${JSON.stringify(vorgang)}: ${JSON.stringify(result)}`)),
38 catchError(this.handleError<Vorgang>('create')));
41 getUrlSave(vbId: string): string {
42 return this.gpsUrl + vbId + '/save';
46 this.store.dispatch(increment());
50 this.store.dispatch(decrement());
54 this.store.dispatch(reset());
57 observe(): Observable<number> {
58 return this.store.pipe(select('vorgang'), map(vorgang => vorgang.counter));
62 * Handle Http operation that failed.
63 * Let the app continue.
64 * @param operation - name of the operation that failed
65 * @param result - optional value to return as the observable result
67 private handleError<T>(operation = 'operation', result?: T) {
68 return (error: any): Observable<T> => {
70 // TODO: send the error to remote logging infrastructure
71 console.error(error); // log to console instead
73 // TODO: better job of transforming error for user consumption
74 this.log(`${operation} failed: ${error.message}`);
76 // Let the app keep running by returning an empty result.
77 if (result === undefined) {
78 return EMPTY as Observable<T>;
80 return of(result as T);
85 private log(message: string) {