File

src/services/rest.service.ts

Index

Properties
Methods

Constructor

constructor(http: Http)
Parameters :
Name Type Optional Description
http Http

Methods

Public add
add(item: any)
Parameters :
Name Type Optional Description
item any
Returns : Observable<number>
Public addAll
addAll(tab: Array)
Parameters :
Name Type Optional Description
tab Array<any>
Returns : Observable<>
Public delete
delete(id: number)
Parameters :
Name Type Optional Description
id number
Returns : Observable<>
Public get
get(id: number)
Parameters :
Name Type Optional Description
id number
Returns : Observable<any>
Private getActionUrl
getActionUrl()
Returns : string
Public getAll
getAll()
Returns : Observable<[]>
Public getAllFromLS
getAllFromLS(maxtime: )
Parameters :
Name Type Optional Description
maxtime
Returns : Array<any>
Public getFromCache
getFromCache(id: )
Parameters :
Name Type Optional Description
id
Returns : any
Private handleError
handleError(error: Response)
Parameters :
Name Type Optional Description
error Response
Returns : any
Public setApiUrl
setApiUrl(url: string)
Parameters :
Name Type Optional Description
url string
Returns : void
Public update
update(id: number, itemToUpdate: any)
Parameters :
Name Type Optional Description
id number
itemToUpdate any
Returns : Observable<number>

Properties

Public headers
headers: Headers
Type : Headers
Public lastGet
lastGet: any
Type : any
Public lastGetAll
lastGetAll: Array<any>
Type : Array<any>
Public modelName
modelName: string
Type : string
Private serverWithApiUrl
serverWithApiUrl: string
Type : string
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class RestService {
    public modelName: string;
    public headers: Headers;
    private serverWithApiUrl: string;

    // cache data
    public lastGetAll: Array<any>;
    public lastGet: any;

    constructor(private http: Http) {
        this.modelName = 'to-configure';

        this.headers = new Headers();
        this.headers.append('Content-Type', 'application/json');
        this.headers.append('Accept', 'application/json');
    }

    public setApiUrl( url: string) {
      this.serverWithApiUrl = url;
    }

    // HELPERS
    public getAllFromLS(maxtime = 0): Array<any> {
      const json = localStorage.getItem( 'rest_all_' + this.modelName );
      if ( json ) {
        const obj = JSON.parse(json);
        if ( obj && (obj.date < (Date.now() - maxtime) ) ) {
          return obj;
        }
      }
    }


    public getFromCache(id): any {
      if (this.lastGetAll) {
        return this.lastGetAll.find((unit) => unit.id === id);
      } else {
        return null;
      }
    }

    private getActionUrl() {
      return this.serverWithApiUrl + this.modelName + '/';
    }


    // REST functions
    public getAll(): Observable<any[]> {
        return this.http.get(this.getActionUrl(), { headers: this.headers })
            .map((response: Response) => {
              // getting an array having the same name as the model
              const data = response.json()[this.modelName];
              // transforming the array from indexed to associative
              const tab = data.records.map((elem) => {
                const unit = {};
                // using the columns order and number to rebuild the object
                data.columns.forEach( (champ, index) => {
                  unit[champ] = elem[index];
                });
                return unit;
              });
              this.lastGetAll = tab;
              const obj = {
                data: tab,
                date: Date.now()
              };
              localStorage.setItem( 'rest_all_' + this.modelName, JSON.stringify(obj) );
              return tab;
            })
            .catch(this.handleError);
    }

    public get(id: number): Observable<any> {
        return this.http.get(this.getActionUrl() + id, { headers: this.headers })
            .map((response: Response) => {
              const data = response.json();
              this.lastGet = data;
              return data;
            })
            .catch(this.handleError);
    }

    public add(item: any): Observable<number> {
        const toAdd = JSON.stringify(item);

        return this.http.post(this.getActionUrl(), toAdd, { headers: this.headers })
            .map((response: Response) => response.json())
            .catch(this.handleError);
    }

    public addAll(tab: Array<any>): Observable<Array<number>> {
      const toAdd = JSON.stringify(tab);

      return this.http.post(this.getActionUrl(), toAdd, { headers: this.headers })
          .map((response: Response) => response.json())
          .catch(this.handleError);
    }

    public update(id: number, itemToUpdate: any): Observable<number> {
        return this.http.put(this.getActionUrl() + id, JSON.stringify(itemToUpdate), { headers: this.headers })
            .map((response: Response) => response.json())
            .catch(this.handleError);
    }

    public delete(id: number): Observable<Response> {
        return this.http.delete(this.getActionUrl() + id, { headers: this.headers })
            .catch(this.handleError);
    }

    private handleError(error: Response) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }
}

results matching ""

    No results matching ""