-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRest.js
executable file
·185 lines (170 loc) · 5.75 KB
/
Rest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(function () {
'use strict';
Base.$inject = ['$http', '$q'];
function Base($http, $q) {
function RestPrototype(url) {
this._url = url;
this._query = { params: { start: 0, pageSize: 10 } };
}
RestPrototype.prototype.get = _get;
RestPrototype.prototype.resetAndGet = _resetAndGet;
RestPrototype.prototype.getNew = _getNew;
RestPrototype.prototype.getById = _getById;
RestPrototype.prototype.save = _save;
RestPrototype.prototype.update = _update;
RestPrototype.prototype.delete = _delete;
RestPrototype.prototype.sort = _sort;
RestPrototype.prototype.deleteCollection = _deleteCollection;
RestPrototype.prototype.saveImage = _saveImage;
RestPrototype.prototype.deleteImage = _deleteImage;
RestPrototype.prototype.getSearch = _getSearch;
RestPrototype.prototype.getAdvancedSearch = _getAdvancedSearch;
RestPrototype.prototype.resetDefaultState = _resetQuery;
RestPrototype.prototype.getStateQuery = _getStateQuery;
RestPrototype.prototype.redoSearch = _redoSearch;
RestPrototype.prototype.saveQuery = _saveQuery;
RestPrototype.prototype.getQuery = _getQuery;
RestPrototype.prototype.postTags = _postTags;
RestPrototype.prototype.getAvailableTags = _getAvailableTags;
RestPrototype.prototype.getSelectedTags = _getSelectedTags;
RestPrototype.prototype.extend = _extend;
RestPrototype.prototype.getDocumentationURL = getDocumentationURL
function _get(page) {
if (page) {
this._query.params.start = (page - 1) * this._query.params.pageSize;
if (page < 1) throw 'Invalid page';
}
return $http.get(this._url, this._query);
}
function _getNew() {
return $http.get(this._url + '/new')
}
function _getById(id) {
return $http.get(this._url + '/' + id);
}
function _save(v) {
return $http.post(this._url, v);
}
function _update(v) {
if (v.id) return $http.put(this._url + '/' + v.id, v);
return this.save(v);
}
function _delete(v) {
return $http.delete(this._url + '/' + v.id);
}
function _resetQuery() {
this._query = {
params: {
start: 0,
pageSize: 10
}
};
}
function _resetAndGet() {
this.resetDefaultState();
return $http.get(this._url, this._query);
}
function _sort(f, w) {
this.resetDefaultState();
this._query.params.sortField = f;
this._query.params.sortDir = w;
return $http.get(this._url, this._query);
}
function _deleteCollection(arr) {
var url = this._url;
return $q.all(arr.map(function (v) {
return $http.delete(url + '/' + v.id);
}))
}
function _saveImage(a, m) {
var fd = new FormData();
fd.append(a, m);
return $http.post(this._url + '/' + a + '/', fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
});
}
function _deleteImage(a) {
var fd = new FormData();
fd.append(a, {});
return $http.delete(this._url + '/' + a, fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
});
}
function _getSearch(f, p) {
this.resetDefaultState();
(!p) ? p = '' : angular.noop;
this._query.params.q = p;
this._query.params.searchFields = f;
return this.get();
}
function _getAdvancedSearch(p) {
this.resetDefaultState();
if (typeof p === 'string') {
this._query.params.aq = p;
return $http.get(this._url, this._query);
}
this._query.params.aq = p.hql;
this._query.params.aqo = JSON.stringify(p.source);
return $http.get(this._url, this._query);
}
function _getStateQuery() {
return this._query;
}
function _redoSearch() {
return $http.get(this._url, this._query);
}
function _saveQuery(q) {
var _aux = {
page: location.hash.replace('#', '').replace(/\//gi, '_'),
data: JSON.stringify(q.query),
name: q.name
};
return $http.post(this._url + '/saq', _aux);
}
function _getQuery(page) {
return $http.get(this._url + '/gumgauserdata/aq;' + page.replace('#', '').replace(/\//gi, '_'));
}
function _postTags(objectId, values = []) {
var tags = [];
values.forEach(function (v) {
let newObj = angular.copy(v.definition);
delete newObj.attributes
tags.push({
objectId: objectId, definition: newObj, values: v.definition.attributes.map(function (v) {
let another = angular.copy(v);
delete another.value;
return {
definition: another,
value: v.value
}
})
});
});
return $http.post(this._url + '/tags', { tags });
}
function _getAvailableTags() {
return $http.get(`${this._url}/tags/`);
}
function _getSelectedTags(id) {
return $http.get(`${this._url}/tags/${id}`);
}
function _extend(method = 'GET', urlExtended = ' ', params) {
if (!$http[method.toLowerCase().trim()]) throw 'O método passado como primeiro parâmetro deve ser um método HTTP válido: GET, HEAD, POST, PUT, DELETE, JSONP, PATCH';
return $http[method.toLowerCase().trim()](`${this._url}${urlExtended}`, params)
}
function getDocumentationURL() {
let arrayUrl = this._url.split('')
if (arrayUrl[arrayUrl.length - 1] == '/') {
arrayUrl.pop()
}
const indexOfLastSlash = arrayUrl.join('').lastIndexOf('/')
const urlWithoutSlash = arrayUrl.join('').slice(0, indexOfLastSlash)
return urlWithoutSlash.concat('/proxy/softwarevalues')
}
return RestPrototype;
}
angular.module('gumga.services.rest', [])
.service('GumgaRest', Base);
})();