github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/angular-app/src/app/data.service.ts (about)

     1  import { Injectable } from '@angular/core';
     2  import { Http, Response, Headers } from '@angular/http';
     3  import { Observable } from 'rxjs/Observable';
     4  import 'rxjs/add/operator/map';
     5  import 'rxjs/add/operator/catch';
     6  import { Configuration } from './configuration';
     7  
     8  @Injectable()
     9  export class DataService<Type> {
    10      private resolveSuffix: string = '?resolve=true';
    11      private actionUrl: string;
    12      private headers: Headers;
    13  
    14      //get rest api configuration from configuration.ts
    15      constructor(private http: Http, private _configuration: Configuration) {
    16          this.actionUrl = _configuration.ServerWithApiUrl;
    17          this.headers = new Headers();
    18          this.headers.append('Content-Type', 'application/json');
    19          this.headers.append('Accept', 'application/json');
    20      }
    21  
    22      public getAll(ns: string): Observable<Type[]> {
    23          console.log('GetAll ' + ns + ' to ' + this.actionUrl + ns);
    24          return this.http.get(`${this.actionUrl}${ns}`)
    25            .map(this.extractData)
    26            .catch(this.handleError);
    27      }
    28  
    29      public getSingle(ns: string, id: string): Observable<Type> {
    30          console.log('GetSingle ' + ns);
    31  
    32          return this.http.get(this.actionUrl + ns + '/' + id + this.resolveSuffix)
    33            .map(this.extractData)
    34            .catch(this.handleError);
    35      }
    36  
    37      public add(ns: string, asset: Type): Observable<Type> {
    38          console.log('Entered DataService add');
    39          console.log('Add ' + ns);
    40          console.log('asset', asset);
    41  
    42          return this.http.post(this.actionUrl + ns, asset)
    43            .map(this.extractData)
    44            .catch(this.handleError);
    45      }
    46  
    47      public update(ns: string, id: string, itemToUpdate: Type): Observable<Type> {
    48          console.log('Update ' + ns);
    49          console.log('what is the id?', id);
    50          console.log('what is the updated item?', itemToUpdate);
    51          console.log('what is the updated item?', JSON.stringify(itemToUpdate));
    52          return this.http.put(`${this.actionUrl}${ns}/${id}`, itemToUpdate)
    53            .map(this.extractData)
    54            .catch(this.handleError);
    55      }
    56      
    57      public delete(ns: string, id: string): Observable<Type> {
    58          console.log('Delete ' + ns);
    59  
    60          return this.http.delete(this.actionUrl + ns + '/' + id)
    61            .map(this.extractData)
    62            .catch(this.handleError);
    63      }
    64  
    65      //get all transactions from system historian
    66      public transactions(): Observable<Type[]> {
    67          console.log('Get transactions ');
    68  
    69          return this.http.get(this.actionUrl + 'system/historian')
    70          .map(this.extractData)
    71          .catch(this.handleError);
    72      }
    73  
    74      private handleError(error: any): Observable<string> {
    75          // In a real world app, we might use a remote logging infrastructure
    76          // We'd also dig deeper into the error to get a better message
    77          let errMsg = (error.message) ? error.message :
    78            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    79          console.error(errMsg); // log to console instead
    80          return Observable.throw(errMsg);
    81      }
    82  
    83      private extractData(res: Response): any {
    84          return res.json();
    85      }
    86  
    87  }