service-object.service.ts
1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { ServiceObject } from '../models/service-object';
@Injectable()
export class ServiceObjectService {
private url = 'http://localhost:5000/serviceobject';
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
getData(): Promise<ServiceObject[]> {
return this.http.get(this.url)
.toPromise()
.then(response => response.json().serviceObjectEditDsM as ServiceObject[])
.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<ServiceObject> {
return this.http.post(this.url + '/create', data, { headers: this.headers })
.toPromise()
.then(response => response.json() as ServiceObject)
.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);
}
}