[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / basic-auth-account / basic-auth-account.service.js
1 'use strict';
2
3 (function () {
4     class BasicAuthAccountService {
5         constructor($q, $log, $http, conf,uuid, utilsService) {
6             this.$q = $q;
7             this.$log = $log;
8             this.$http = $http;
9             this.conf = conf;
10             this.uuid = uuid;
11             this.utilsService = utilsService;   
12         }
13         
14         createAccount(newAccount) {
15                 let deferred = this.$q.defer();
16                 this.$http({
17                 method: "POST",
18                 url: this.conf.api.basicAuthAccount,
19                 data: newAccount,
20                 cache: false,
21                 headers:{
22                         'X-ECOMP-RequestID':this.uuid.generate()
23                 }
24             }).then(res => {
25                 if (res == null || res.data == null) {
26                                 this.$log.error('BasicAuthAccountService::createAccount Failed: Result or result.data is null');
27                      deferred.reject("BasicAuthAccountService::createAccount Failed: Result or result.data is null");
28                 } else if (!this.utilsService.isValidJSON(res.data)) {
29                         this.$log.error('BasicAuthAccountService::createAccount Failed: Invalid JSON format');
30                     deferred.reject("BasicAuthAccountService::createAccount Failed: Invalid JSON format");
31                 } else {
32                     deferred.resolve(res.data);
33                 }
34             })
35             .catch(errRes => {
36                 deferred.reject(errRes);
37             });
38             return deferred.promise;
39         }
40         
41         updateAccount(accountId, newAccount) {
42                 let deferred = this.$q.defer();
43                 this.$http({
44                 method: "PUT",
45                 url: this.conf.api.basicAuthAccount + "/" + accountId,
46                 data: newAccount,
47                 cache: false,
48                 headers:{
49                         'X-ECOMP-RequestID':this.uuid.generate()
50                 }
51             }).then(res => {
52                 if (res == null || res.data == null) {
53                                 this.$log.error('BasicAuthAccountService::updateAccount Failed: Result or result.data is null');
54                      deferred.reject("BasicAuthAccountService::updateAccount Failed: Result or result.data is null");
55                 } else if (!this.utilsService.isValidJSON(res.data)) {
56                         this.$log.error('BasicAuthAccountService::updateAccount Failed: Invalid JSON format');
57                     deferred.reject("BasicAuthAccountService::updateAccount Failed: Invalid JSON format");
58                 } else {
59                     deferred.resolve(res.data);
60                 }
61             })
62             .catch(errRes => {
63                 deferred.reject(errRes);
64             });
65             return deferred.promise;
66         }
67         
68         getAccountList() {
69                 let deferred = this.$q.defer();
70                 this.$http({
71                 method: "GET",
72                 url: this.conf.api.basicAuthAccount,
73                 cache: false,
74                 headers:{
75                         'X-ECOMP-RequestID':this.uuid.generate()
76                 }
77             }).then(res => {
78                 if (res == null || res.data == null) {
79                                 this.$log.error('BasicAuthAccountService::getAccountList Failed: Result or result.data is null');
80                      deferred.reject("BasicAuthAccountService::getAccountList Failed: Result or result.data is null");
81                 } else if (!this.utilsService.isValidJSON(res.data)) {
82                         this.$log.error('BasicAuthAccountService::getAccountList Failed: Invalid JSON format');
83                     deferred.reject("BasicAuthAccountService::getAccountList Failed: Invalid JSON format");
84                 } else {
85                     deferred.resolve(res.data.response);
86                 }
87             })
88             .catch(errRes => {
89                 deferred.reject(errRes);
90             });
91             return deferred.promise;
92         }
93         
94         deleteAccount(accountId) {
95                 let deferred = this.$q.defer();
96                 this.$http({
97                 method: "DELETE",
98                 url: this.conf.api.basicAuthAccount + "/" + accountId,
99                 cache: false,
100                 headers:{
101                         'X-ECOMP-RequestID':this.uuid.generate()
102                 }
103             }).then(res => {
104                 if (res == null || res.data == null) {
105                                 this.$log.error('BasicAuthAccountService::deleteAccount Failed: Result or result.data is null');
106                      deferred.reject("BasicAuthAccountService::deleteAccount Failed: Result or result.data is null");
107                 } else if (!this.utilsService.isValidJSON(res.data)) {
108                         this.$log.error('BasicAuthAccountService::deleteAccount Failed: Invalid JSON format');
109                     deferred.reject("BasicAuthAccountService::deleteAccount Failed: Invalid JSON format");
110                 } else {
111                     deferred.resolve(res.data);
112                 }
113             })
114             .catch(errRes => {
115                 deferred.reject(errRes);
116             });
117             return deferred.promise;
118         }
119        
120     }
121     
122     BasicAuthAccountService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
123     angular.module('ecompApp').service('basicAuthAccountService', BasicAuthAccountService)
124 })();