X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fvorgang.service.ts;fp=src%2Fapp%2Fvorgang.service.ts;h=dff07d2586f8075c405bb2e5d352770d7e01f5b7;hb=ebd9d8841452da2adb2d0c00357af646a97afcd8;hp=0000000000000000000000000000000000000000;hpb=5bf0ea327ad86bcadc47bce2c0d389511587ea0d;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/vorgang.service.ts b/src/app/vorgang.service.ts new file mode 100644 index 0000000..dff07d2 --- /dev/null +++ b/src/app/vorgang.service.ts @@ -0,0 +1,67 @@ +import { Injectable } from '@angular/core'; +import { Observable, of, EMPTY } from 'rxjs'; +import { Vorgang } from './vorgang'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { catchError, tap } from 'rxjs/operators'; + + +@Injectable({ + providedIn: 'root' +}) +export class VorgangService { + + gpsUrl = 'http://localhost:1991/las/'; // URL to web api + httpOptions = { + headers: new HttpHeaders({ + 'Content-Type': 'text/plain', + 'Accept': 'application/json' + }) + }; + + constructor(private http: HttpClient) { } + + /** POST: Einen neuen Vorgang erzeugen */ + create(vorgang: Vorgang): Observable { + return this + .http + .post( + this.getUrlSave(vorgang.vbId), + vorgang.zustand, + this.httpOptions) + .pipe( + tap((result: Vorgang) => this.log(`Result for ${JSON.stringify(vorgang)}: ${JSON.stringify(result)}`)), + catchError(this.handleError('create'))); + } + + getUrlSave(vbId: string): string { + return this.gpsUrl + vbId + '/save'; + } + + /** + * 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(operation = 'operation', result?: T) { + return (error: any): Observable => { + + // TODO: send the error to remote logging infrastructure + console.error(error); // log to console instead + + // TODO: better job of transforming error for user consumption + this.log(`${operation} failed: ${error.message}`); + + // Let the app keep running by returning an empty result. + if (result === undefined) { + return EMPTY as Observable; + } else { + return of(result as T); + } + }; + } + + private log(message: string) { + console.log(message); + } +}