[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / menus / menus.service.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 'use strict';
21
22 (function () {
23   class MenusService {
24     constructor($q, $log, $http, conf, uuid, utilsService) {
25         this.$q = $q;
26         this.$log = $log;
27         this.$http = $http;
28         this.conf = conf;
29         this.uuid = uuid;
30         this.utilsService = utilsService;
31     }
32
33     GetFunctionalMenuForUser() {
34       let deferred = this.$q.defer();
35       // this.$log.info('MenusService::GetFunctionalMenuForUser via REST API');
36       this.$http({
37         method: 'GET',
38         url: this.conf.api.functionalMenuForAuthUser,
39         cache: false,
40         headers: {
41           'X-ECOMP-RequestID':this.uuid.generate()
42         }
43       }).then( res => {
44               // If response comes back as a redirected HTML page which IS NOT a success
45               if (this.utilsService.isValidJSON(res)== false) {
46                   deferred.reject('MenusService::GetFunctionalMenuForUser Failed');
47               } else {
48                   // this.$log.info('MenusService::GetFunctionalMenuForUser success:');
49                   deferred.resolve(res.data);
50               }
51           })
52           .catch( status => {
53             this.$log.error('MenusService::rejection:' + status);
54             deferred.reject(status);
55           });
56
57       return deferred.promise;
58     }
59       
60     getFavoriteItems() {
61       let deferred = this.$q.defer();
62       // this.$log.info('MenusService::getFavoriteItems via REST API');
63         this.$http({
64             method: 'GET',
65             url: this.conf.api.getFavoriteItems +'?date='+new Date().getTime(),
66             cache: false,
67             headers: {
68                 'X-ECOMP-RequestID':this.uuid.generate()
69             }
70         }).then( res => {
71                 // If response comes back as a redirected HTML page which IS NOT a success
72             if (this.utilsService.isValidJSON(res)== false) {
73                     deferred.reject('MenusService::getFavoriteItems has no favorites');
74                 } else {
75                     // this.$log.info('MenusService::getFavoriteItems success:');
76                     deferred.resolve(res.data);
77                 }
78             })
79             .catch( status => {
80                 this.$log.error('MenusService::getFavoriteItems rejection:' + status);
81                 deferred.reject(status);
82             });
83
84         return deferred.promise;
85     }
86     
87     setFavoriteItem(menuId) {
88       let deferred = this.$q.defer();
89       // this.$log.info('menus-service.service::setFavoriteItem  via REST API' + menuId);
90       let url = this.conf.api.setFavoriteItem;
91       this.$http({
92           method: 'POST',
93           url: url,
94           cache: false,
95           headers: {
96               'X-ECOMP-RequestID':this.uuid.generate(),
97               'Content-Type': 'application/json'
98           },
99           data: menuId
100       }).then(res => {
101               // If response comes back as a redirected HTML page which IS NOT a success
102               if (this.utilsService.isValidJSON(res)== false) {
103                   deferred.reject('MenusService::setFavoriteItem Failed');
104               } else {
105                   // this.$log.info('MenusService::setFavoriteItem success:');
106                   deferred.resolve(res.data);
107               }
108           })
109           .catch(errRes => {
110               this.$log.error('MenusService::setFavoriteItem rejection:' + JSON.stringify(errRes));
111               deferred.reject(errRes);
112           });
113       return deferred.promise;
114     }
115     
116       removeFavoriteItem(menuId) {
117           let deferred = this.$q.defer();
118           // this.$log.info('menus-service.service::removeFavoriteItem  via REST API');
119           let url = this.conf.api.removeFavoriteItem.replace(':menuId', menuId);
120           this.$http({
121               method: 'DELETE',
122               url: url,
123               cache: false,
124               headers: {
125                   'X-ECOMP-RequestID':this.uuid.generate()
126               }
127           }).then(res => {
128               // If response comes back as a redirected HTML page which IS NOT a success
129               if (this.utilsService.isValidJSON(res)== false) {
130                   deferred.reject('MenusService::removeFavoriteItem Failed');
131               } else {
132                   // this.$log.info('MenusService::removeFavoriteItem success:');
133                   deferred.resolve(res.data);
134               }
135           })
136               .catch(errRes => {
137                   this.$log.error('MenusService::removeFavoriteItem rejection:' + status);
138                   deferred.reject(errRes);
139               });
140           return deferred.promise;
141       }
142
143     
144   }
145   MenusService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4', 'utilsService'];
146   angular.module('ecompApp').service('menusService', MenusService)
147 })();