[UI] Support data registration
[validation.git] / ui / src / main / webapp / app / BluvalUI / ModifyLab / ModifyLabController.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 var app = angular.module('ModifyLab');
18 app
19         .controller(
20                 'ModifyLabController',
21                 function($scope, restAPISvc) {
22
23                     initialize();
24
25                     function initialize() {
26                         $scope.loadingLabs = true;
27                         $scope.labInfos = [];
28                         $scope.labs = [];
29                         $scope.selectedLab = '';
30                         $scope.oldName = '';
31                         $scope.oldSilo = '';
32                         $scope.newData = {};
33                         restAPISvc.getRestAPI("/api/v1/lab/", function(data) {
34                             if (data) {
35                                 $scope.labInfos = data;
36                                 angular.forEach(data, function(lab) {
37                                     $scope.labs.push(lab.lab);
38                                 });
39                             } else {
40                                 confirm("No labs found");
41                             }
42                             $scope.loadingLabs = false;
43                         });
44                     }
45
46                     $scope.selectedLabChange = function() {
47                         $scope.oldName = '';
48                         $scope.oldSilo = '';
49                         $scope.newData = {};
50                         var finalLabInfo = '';
51                         angular.forEach($scope.labInfos, function(labInfo) {
52                             if ($scope.selectedLab.trim() === labInfo.lab
53                                     .trim()) {
54                                 finalLabInfo = labInfo;
55                             }
56                         });
57                         if (!finalLabInfo) {
58                             confirm("Error in lab info");
59                             return;
60                         }
61                         $scope.oldName = finalLabInfo.lab;
62                         $scope.oldSilo = finalLabInfo.silo;
63                     }
64
65                     $scope.modify = function() {
66                         if (!$scope.selectedLab || !$scope.newData.name
67                                 || !$scope.newData.silo) {
68                             confirm("You must specify all the values");
69                             return;
70                         }
71                         var finalLabInfo = '';
72                         angular.forEach($scope.labInfos, function(labInfo) {
73                             if ($scope.selectedLab.trim() === labInfo.lab
74                                     .trim()) {
75                                 finalLabInfo = labInfo;
76                             }
77                         });
78                         if (!finalLabInfo) {
79                             confirm("Error in lab info");
80                             return;
81                         }
82                         finalLabInfo.lab = $scope.newData.name;
83                         finalLabInfo.silo = $scope.newData.silo;
84                         restAPISvc
85                                 .postRestAPI(
86                                         "/api/v1/lab/",
87                                         finalLabInfo,
88                                         function(data) {
89                                             if (data) {
90                                                 var confirmText = "The lab has been modified successfully."
91                                                 confirm(confirmText);
92                                             } else {
93                                                 confirm("Error when modifying the lab");
94                                             }
95                                             initialize();
96                                         });
97                     }
98                 });