import { Injectable } from '@angular/core'; import { Headers, Http } from '@angular/http'; import 'rxjs/add/operator/toPromise'; import { BusStop } from '../models/bus-stop'; @Injectable() export class BusStopService { private url = 'http://localhost:5000/busstop'; private headers = new Headers({'Content-Type': 'application/json'}); constructor(private http: Http) { } getData(from: number = 0, to: number = 100, sort: string = null): Promise { let url = this.url; url += '?from=' + from + '&to=' + to; if (sort) { url += '&sort=' + sort; } return this.http.get(url) .toPromise() .then(response => response.json().busStopEditDsM as BusStop[]) .catch(this.handleError); } update(id: number, data: string): Promise { 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 { 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 { 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 { console.error('An error occured', error); return Promise.reject(error.message || error); } }