busStop.service.ts 1.82 KB
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';

import { busStop } from './busStop';

@Injectable()
export class BusStopService {
  private url = 'http://localhost:5000/busstop';
  private headers = new Headers({'Content-Type': 'application/json'});
  constructor(private http: Http) { }
  getData(): Promise<busStop[]> {
    // let busStops: busStop2[] = [{
    //   roadId: 1,
    //   regionId: 1,
    //   settlementId: 1,
    //   surfaceTypeId: '1',
    //   locationLeft: '1',
    //   locationRight: '1',
    //   stateCommonId: 1,
    //   areaStopAvailability: '',
    //   areaLandAvailability: '',
    //   pocketAvailability: '',
    //   toiletAvailability: '',
    //   yearBuild: '',
    //   yearRepair: '',
    // }];
    // return Promise.resolve(busStops);
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.json().busStopEditDsM as busStop[])
      .catch(this.handleError);
  }
  update(id: number, data: string): Promise<any> {
    return this.http.post(this.url + '/update?id=' + id, data, { headers: this.headers })
      .toPromise()
      .then(response => response.json())
      .catch(this.handleError);
  }
  create(data: string): Promise<busStop> {
    return this.http.post(this.url + '/create', data, { headers: this.headers })
      .toPromise()
      .then(response => response.json() as busStop)
      .catch(this.handleError);
  }
  delete(id: number): Promise<any> {
    return this.http.delete(this.url + '/delete?id=' + id, { headers: this.headers })
      .toPromise()
      .then(response => response.json())
      .catch(this.handleError);
  }
  private handleError(error: any): Promise<any> {
    console.error('An error occured', error);
    return Promise.reject(error.message || error);
  }
}