Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame^] | 1 | import { Injectable } from '@angular/core'; |
| 2 | import { Http, Response, RequestOptions } from '@angular/http'; |
| 3 | import { Observable } from 'rxjs/Observable'; |
| 4 | import 'rxjs/add/operator/catch'; |
| 5 | import 'rxjs/add/operator/map'; |
| 6 | |
| 7 | import { Device } from './device'; |
| 8 | |
| 9 | @Injectable() |
| 10 | export class DevicesService { |
| 11 | constructor(private http: Http) {} |
| 12 | |
| 13 | getDevices(): Observable<Device[]> { |
| 14 | return this.http.get('/netopeer/inventory/devices/list') |
| 15 | .map((resp: Response) => resp.json()) |
| 16 | .catch((err: Response | any) => Observable.throw(err)); |
| 17 | } |
| 18 | |
| 19 | addDevice(device: Device) { |
| 20 | let options = new RequestOptions({ body: JSON.stringify(device) }); |
| 21 | return this.http.post('/netopeer/inventory/devices', null, options) |
| 22 | .map((resp: Response) => resp.json()) |
| 23 | .catch((err: Response | any) => Observable.throw(err)); |
| 24 | } |
| 25 | |
| 26 | rmDevice(device_id: number) { |
| 27 | let options = new RequestOptions({ body: JSON.stringify({'id':device_id}) }); |
| 28 | return this.http.delete('/netopeer/inventory/devices', options) |
| 29 | .map((resp: Response) => resp.json()) |
| 30 | .catch((err: Response | any) => Observable.throw(err)); |
| 31 | } |
| 32 | } |