initial contracts work
[groupbasedpolicy.git] / groupbasedpolicy-ui / module / src / main / resources / gbp / contract / contract-list.service.js
1 define([], function () {
2     'use strict';
3
4     angular.module('app.gbp').service('ContractListService', ContractListService);
5
6     ContractListService.$inject = ['Restangular', 'ContractService'];
7
8     function ContractListService(Restangular, ContractService) {
9         /* methods */
10         this.createList = createList;
11
12         function ContractList() {
13             /* properties */
14             this.data = [];
15             /* methods */
16             this.setData = setData;
17             this.get = get;
18
19             /* Implementation */
20             /**
21              * fills ContractList object with data
22              * @param data
23              */
24             function setData(data) {
25                 var self = this;
26                 data.forEach(function (dataElement) {
27                     self.data.push(ContractService.createObject(dataElement));
28                 });
29             }
30
31             function get(dataStore) {
32                 /* jshint validthis:true */
33                 var self = this;
34
35                 var restObj = Restangular.one('restconf').one(dataStore).one('policy:tenants')
36                 .one('tenant').one('tenant1').one('policy');
37
38                 return restObj.get().then(function (data) {
39                     if (data.policy.contract) {
40                         self.setData(data.policy.contract);
41                     }
42                 });
43             }
44         }
45
46         function createList() {
47             var obj = new ContractList();
48
49             return obj;
50         }
51     }
52
53     return ContractListService;
54 });