+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+import { HttpClient, HttpHeaders } from "@angular/common/http";\r
+import { AppGlobals } from "../../app.global";\r
+import { ParamsService } from "./params.service";\r
+import { Observable, observable, from } from "rxjs";\r
+import { CookieService } from "ngx-cookie-service";\r
+import { FeathersService } from "./feathers.service";\r
+import { Injectable } from "@angular/core";\r
+\r
+Injectable({\r
+ providedIn: 'root'\r
+})\r
+export class ModelService {\r
+\r
+ protected path;\r
+ protected http: HttpClient;\r
+ protected Params: ParamsService;\r
+ protected cookie: CookieService;\r
+ protected feathers: FeathersService;\r
+ private authenticated: Boolean = false;\r
+\r
+ constructor(endpoint: String, http: HttpClient, Params: ParamsService, cookie: CookieService, feathers: FeathersService) {\r
+ this.http = http;\r
+ this.Params = Params;\r
+ this.path = AppGlobals.baseAPIUrl + endpoint;\r
+ this.cookie = cookie;\r
+ this.feathers = feathers;\r
+ }\r
+\r
+ checkAuth(): Observable<Object>{\r
+ return this.feathers.auth;\r
+ }\r
+\r
+ call(method, data?, path?){\r
+ if(!path){\r
+ path = this.path;\r
+ }\r
+ return new Observable(observer => {\r
+ var init = null;\r
+ if(data.params && data.params.events){\r
+ delete data.params.events;\r
+ this.feathers.service(path)\r
+ .on('created', data => {\r
+ if(init){\r
+ if(init.data){\r
+ (init.data as Array<Object>).unshift(data);\r
+ observer.next(init);\r
+ }else{\r
+ (init as Array<Object>).unshift(data);\r
+ observer.next(init);\r
+ }\r
+ }\r
+ })\r
+ .on('removed', data => {\r
+ if(init){\r
+ if(init.data){\r
+ init.data = (init.data as Array<Object>).filter(item => item['_id'] != data._id);\r
+ observer.next(init);\r
+ }else{\r
+ init = (init as Array<Object>).filter(item => item['_id'] != data._id);\r
+ observer.next(init);\r
+ }\r
+ }\r
+ })\r
+ .on('updated', data => {\r
+ if(init){\r
+ if(init.data){\r
+ (init.data as Array<Object>).forEach((elem, val) => {\r
+ if(elem['_id'] == data._id){\r
+ (init.data as Array<Object>).splice(val, 1, data);\r
+ return;\r
+ }\r
+ })\r
+ observer.next(init);\r
+ }else{\r
+ (init as Array<Object>).forEach((elem, val) => {\r
+ if(elem['_id'] == data._id){\r
+ (init as Array<Object>).splice(val, 1, data);\r
+ return;\r
+ }\r
+ })\r
+ observer.next(init);\r
+ }\r
+ }\r
+ });\r
+ \r
+ }\r
+ this.checkAuth().subscribe(res => {\r
+ if(data.data){\r
+ \r
+ //UPDATE & PATCH\r
+ if(method == 'update' || method == 'patch'){\r
+ let id = data.data._id;\r
+ delete data.data._id;\r
+ this.feathers.service(path)[method](id, data.data, {query: data.params}).then(result =>{\r
+ if(!init){\r
+ init = result;\r
+ }\r
+ observer.next(result)\r
+ }).catch(err => {\r
+ observer.error(err)}\r
+ );\r
+ }else{\r
+ this.feathers.service(path)[method](data.data, {query: data.params}).then(result =>{\r
+ if(!init){\r
+ init = result;\r
+ }\r
+ observer.next(result)\r
+ }).catch(err => {\r
+ observer.error(err)\r
+ });\r
+ }\r
+ }else{\r
+ this.feathers.service(path)[method]({query: data.params}).then(result =>{\r
+ if(!init){\r
+ init = result;\r
+ }\r
+ observer.next(result)\r
+ }).catch(err => observer.error(err));\r
+ }\r
+\r
+ }, err => {\r
+ \r
+ this.feathers.authenticate().subscribe(res => {\r
+ observer.next(this.call(method, data, path));\r
+ })\r
+ });\r
+ }) \r
+ }\r
+\r
+ on(event){\r
+ return new Observable(observer => {\r
+ this.feathers.service(this.path).on(event, (data) => {\r
+ observer.next(data);\r
+ });\r
+ })\r
+ }\r
+\r
+ // sfind(params = []): Observable<Object> {\r
+ // return this.http.get(this.path + this.Params.toString(params), this.getHttpOptions());\r
+ // }\r
+\r
+ find(params?): Observable<Object> {\r
+\r
+ return this.call('find', {params: params})\r
+ }\r
+\r
+ // sget(id, params = []): Observable<Object> {\r
+ // return from(this.http.get(this.path + '/' + id + this.Params.toString(params), this.getHttpOptions()));\r
+ // }\r
+\r
+ get(id, params?): Observable<Object> {\r
+ return this.call('get', {data: id, params: params})\r
+ }\r
+\r
+ // create(data, params = []): Observable<Object> {\r
+ // return this.http.post(this.path + this.Params.toString(params), data, this.getHttpOptions());\r
+ // }\r
+\r
+ create(data, params?): Observable<Object> {\r
+ return this.call('create', {data: data, params: params})\r
+ }\r
+\r
+ // update(data, params = []): Observable<Object> {\r
+ // return this.http.put(this.path + '/' + data._id + this.Params.toString(params), data, this.getHttpOptions());\r
+ // }\r
+\r
+ update(data, params?): Observable<Object> {\r
+ return this.call('update', {data: data, params: params})\r
+ }\r
+\r
+ // patch(data, params = []): Observable<Object> {\r
+ // return this.http.patch(this.path + '/' + data._id + this.Params.toString(params), data, this.getHttpOptions());\r
+ // }\r
+\r
+ patch(data, params?): Observable<Object> {\r
+ return this.call('patch', {data: data, params: params})\r
+ }\r
+\r
+ // delete(id, params = []): Observable<Object> {\r
+ // return this.http.delete(this.path + '/' + id + this.Params.toString(params), this.getHttpOptions());\r
+ // }\r
+\r
+ delete(id, params?): Observable<Object> {\r
+ return this.call('remove', {data: id, params: params})\r
+ }\r
+\r
+ protected getHttpOptions() {\r
+ return {\r
+ headers: new HttpHeaders({\r
+ 'Authorization': 'Bearer ' + JSON.parse(this.cookie.get('access_token'))\r
+ })\r
+ };\r
+ }\r
+\r
+}
\ No newline at end of file