Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 1 | import { Injectable } from '@angular/core'; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame^] | 2 | import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 3 | import { Observable } from 'rxjs/Observable'; |
| 4 | import 'rxjs/add/operator/catch'; |
| 5 | import 'rxjs/add/operator/map'; |
| 6 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame^] | 7 | import { Schema } from '../inventory/schema'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 8 | |
| 9 | @Injectable() |
| 10 | export class SchemasService { |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame^] | 11 | public schemas: Schema[]; |
| 12 | public activeSchema: string; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 13 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame^] | 14 | constructor( private http: Http ) { |
| 15 | this.loadData(); |
| 16 | this.activeSchema = localStorage.getItem('activeSchema'); |
| 17 | if (!this.activeSchema) { |
| 18 | this.activeSchema = ""; |
| 19 | this.schemas = []; |
| 20 | } |
| 21 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 22 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame^] | 23 | storeData() { |
| 24 | localStorage.setItem('schemas', JSON.stringify(this.schemas)); |
| 25 | } |
Radek Krejci | c58f347 | 2017-09-08 16:17:01 +0200 | [diff] [blame] | 26 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame^] | 27 | loadData() { |
| 28 | this.schemas = JSON.parse(localStorage.getItem('schemas')); |
| 29 | } |
| 30 | |
| 31 | getActiveSchema(key: string = this.activeSchema): Schema { |
| 32 | if (!key) { |
| 33 | return null; |
| 34 | } |
| 35 | for (let i = this.schemas.length; i > 0; i--) { |
| 36 | if (this.schemas[i - 1].key == key) { |
| 37 | return this.schemas[i - 1]; |
| 38 | } |
| 39 | } |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | changeActiveSchema(key: string): Schema { |
| 44 | let result = this.getActiveSchema(key); |
| 45 | if (result) { |
| 46 | this.activeSchema = key; |
| 47 | localStorage.setItem('activeSession', this.activeSchema); |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | getSchemas() { |
| 53 | return this.http.get( '/netopeer/inventory/schemas' ) |
| 54 | .map(( resp: Response ) => resp.json()).toPromise(); |
| 55 | } |
| 56 | |
| 57 | show(schema: Schema) { |
| 58 | let newSchema = true; |
| 59 | for (let i in this.schemas) { |
| 60 | if (this.schemas[i].key == schema.key) { |
| 61 | schema = this.schemas[i]; |
| 62 | newSchema = false; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (!('data' in schema)) { |
| 68 | let params = new URLSearchParams(); |
| 69 | params.set('key', schema.key); |
| 70 | let options = new RequestOptions({ search: params }); |
| 71 | this.http.get('/netopeer/inventory/schema', options) |
| 72 | .map((resp: Response) => resp.json()).toPromise().then(result => { |
| 73 | console.log(result) |
| 74 | if (result['success']) { |
| 75 | schema['data'] = result['data']; |
| 76 | this.storeData(); |
| 77 | console.log(this.schemas) |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | if (newSchema) { |
| 83 | this.schemas.push(schema); |
| 84 | this.storeData(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | addSchema( schema: File ) { |
| 89 | let headers = new Headers( { 'specific-content-type': '' } ); |
| 90 | let options = new RequestOptions( { headers: headers } ); |
| 91 | let input = new FormData(); |
| 92 | input.append( "schema", schema ); |
| 93 | return this.http.post( '/netopeer/inventory/schemas', input, options ) |
| 94 | .map(( resp: Response ) => resp.json() ) |
| 95 | .catch(( err: Response | any ) => Observable.throw( err ) ); |
| 96 | } |
| 97 | |
| 98 | rmSchema( schema: Schema ) { |
| 99 | let options = new RequestOptions( { body: schema.key } ); |
| 100 | return this.http.delete( '/netopeer/inventory/schemas', options ) |
| 101 | .map(( resp: Response ) => resp.json() ) |
| 102 | .catch(( err: Response | any ) => Observable.throw( err ) ); |
| 103 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 104 | } |