1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 import { HttpClient, HttpHeaders } from "@angular/common/http";
\r
18 import { AppGlobals } from "../../app.global";
\r
19 import { ParamsService } from "./params.service";
\r
20 import { Observable, observable, from } from "rxjs";
\r
21 import { CookieService } from "ngx-cookie-service";
\r
22 import { FeathersService } from "./feathers.service";
\r
23 import { Injectable } from "@angular/core";
\r
28 export class ModelService {
\r
31 protected http: HttpClient;
\r
32 protected Params: ParamsService;
\r
33 protected cookie: CookieService;
\r
34 protected feathers: FeathersService;
\r
35 private authenticated: Boolean = false;
\r
37 constructor(endpoint: String, http: HttpClient, Params: ParamsService, cookie: CookieService, feathers: FeathersService) {
\r
39 this.Params = Params;
\r
40 this.path = AppGlobals.baseAPIUrl + endpoint;
\r
41 this.cookie = cookie;
\r
42 this.feathers = feathers;
\r
45 checkAuth(): Observable<Object>{
\r
46 return this.feathers.auth;
\r
49 call(method, data?, path?){
\r
53 return new Observable(observer => {
\r
55 if(data.params && data.params.events){
\r
56 delete data.params.events;
\r
57 this.feathers.service(path)
\r
58 .on('created', data => {
\r
61 (init.data as Array<Object>).unshift(data);
\r
62 observer.next(init);
\r
64 (init as Array<Object>).unshift(data);
\r
65 observer.next(init);
\r
69 .on('removed', data => {
\r
72 init.data = (init.data as Array<Object>).filter(item => item['_id'] != data._id);
\r
73 observer.next(init);
\r
75 init = (init as Array<Object>).filter(item => item['_id'] != data._id);
\r
76 observer.next(init);
\r
80 .on('updated', data => {
\r
83 (init.data as Array<Object>).forEach((elem, val) => {
\r
84 if(elem['_id'] == data._id){
\r
85 (init.data as Array<Object>).splice(val, 1, data);
\r
89 observer.next(init);
\r
91 (init as Array<Object>).forEach((elem, val) => {
\r
92 if(elem['_id'] == data._id){
\r
93 (init as Array<Object>).splice(val, 1, data);
\r
97 observer.next(init);
\r
103 this.checkAuth().subscribe(res => {
\r
107 if(method == 'update' || method == 'patch'){
\r
108 let id = data.data._id;
\r
109 delete data.data._id;
\r
110 this.feathers.service(path)[method](id, data.data, {query: data.params}).then(result =>{
\r
114 observer.next(result)
\r
116 observer.error(err)}
\r
119 this.feathers.service(path)[method](data.data, {query: data.params}).then(result =>{
\r
123 observer.next(result)
\r
125 observer.error(err)
\r
129 this.feathers.service(path)[method]({query: data.params}).then(result =>{
\r
133 observer.next(result)
\r
134 }).catch(err => observer.error(err));
\r
139 this.feathers.authenticate().subscribe(res => {
\r
140 observer.next(this.call(method, data, path));
\r
147 return new Observable(observer => {
\r
148 this.feathers.service(this.path).on(event, (data) => {
\r
149 observer.next(data);
\r
154 // sfind(params = []): Observable<Object> {
\r
155 // return this.http.get(this.path + this.Params.toString(params), this.getHttpOptions());
\r
158 find(params?): Observable<Object> {
\r
160 return this.call('find', {params: params})
\r
163 // sget(id, params = []): Observable<Object> {
\r
164 // return from(this.http.get(this.path + '/' + id + this.Params.toString(params), this.getHttpOptions()));
\r
167 get(id, params?): Observable<Object> {
\r
168 return this.call('get', {data: id, params: params})
\r
171 // create(data, params = []): Observable<Object> {
\r
172 // return this.http.post(this.path + this.Params.toString(params), data, this.getHttpOptions());
\r
175 create(data, params?): Observable<Object> {
\r
176 return this.call('create', {data: data, params: params})
\r
179 // update(data, params = []): Observable<Object> {
\r
180 // return this.http.put(this.path + '/' + data._id + this.Params.toString(params), data, this.getHttpOptions());
\r
183 update(data, params?): Observable<Object> {
\r
184 return this.call('update', {data: data, params: params})
\r
187 // patch(data, params = []): Observable<Object> {
\r
188 // return this.http.patch(this.path + '/' + data._id + this.Params.toString(params), data, this.getHttpOptions());
\r
191 patch(data, params?): Observable<Object> {
\r
192 return this.call('patch', {data: data, params: params})
\r
195 // delete(id, params = []): Observable<Object> {
\r
196 // return this.http.delete(this.path + '/' + id + this.Params.toString(params), this.getHttpOptions());
\r
199 delete(id, params?): Observable<Object> {
\r
200 return this.call('remove', {data: id, params: params})
\r
203 protected getHttpOptions() {
\r
205 headers: new HttpHeaders({
\r
206 'Authorization': 'Bearer ' + JSON.parse(this.cookie.get('access_token'))
\r