blob: 590c754af055ce4baea1376d28bfe8c9ba0ee0f9 [file] [log] [blame]
Radek Krejci80e003e2017-09-21 14:20:20 +02001import { Injectable } from '@angular/core';
Jakub Man8f642872018-06-13 15:48:28 +02002import { HttpClient } from '@angular/common/http';
Radek Krejci80e003e2017-09-21 14:20:20 +02003import { Observable } from 'rxjs/Observable';
Jakub Man59c4ea12018-06-13 15:21:32 +02004import { catchError } from 'rxjs/operators';
Radek Krejci80e003e2017-09-21 14:20:20 +02005
6import { Device } from './device';
7
8@Injectable()
9export class DevicesService {
Jakub Man59c4ea12018-06-13 15:21:32 +020010 constructor(private http: HttpClient) {}
Radek Krejci80e003e2017-09-21 14:20:20 +020011
12 getDevices(): Observable<Device[]> {
Jakub Man59c4ea12018-06-13 15:21:32 +020013 return this.http.get<Device[]>('/netopeer/inventory/devices/list')
14 .pipe(
15 catchError(err => Observable.throw(err))
16 );
Radek Krejci80e003e2017-09-21 14:20:20 +020017 }
18
19 addDevice(device: Device) {
Jakub Man59c4ea12018-06-13 15:21:32 +020020 // let options = new HttpOptions({ body: JSON.stringify(device) });
21 return this.http.post<object>('/netopeer/inventory/devices', device)
22 .pipe(
23 catchError(err => Observable.throw(err))
24 );
Radek Krejci80e003e2017-09-21 14:20:20 +020025 }
26
27 rmDevice(device_id: number) {
Jakub Man59c4ea12018-06-13 15:21:32 +020028 // We need to use generic HTTP request, because HttpClient does not support body in DELETE requests.
29 return this.http.request('DELETE', '/netopeer/inventory/devices', { body: JSON.stringify({'id':device_id}) })
30 .pipe(
31 catchError(err => Observable.throw(err))
32 );
Radek Krejci80e003e2017-09-21 14:20:20 +020033 }
34}