services.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. /* Services that are used to interact with the backend. */
  3. var eventManServices = angular.module('eventManServices', ['ngResource']);
  4. /* Modify, in place, an object to convert datetime. */
  5. function convert_dates(obj) {
  6. if (obj['begin-date']) {
  7. obj['begin-date'] = obj['begin_date'] = obj['begin-date'].getTime();
  8. }
  9. if (obj['end-date']) {
  10. obj['end-date'] = obj['end_date'] = obj['end-date'].getTime();
  11. }
  12. return obj;
  13. }
  14. eventManServices.factory('Event', ['$resource', '$rootScope',
  15. function($resource, $rootScope) {
  16. return $resource('events/:id', {id: '@_id', ticket_id: '@_id'}, {
  17. all: {
  18. method: 'GET',
  19. interceptor : {responseError: $rootScope.errorHandler},
  20. isArray: true,
  21. transformResponse: function(data, headers) {
  22. data = angular.fromJson(data);
  23. if (data.error) {
  24. return data;
  25. }
  26. angular.forEach(data.events || [], function(event_, event_idx) {
  27. convert_dates(event_);
  28. });
  29. return data.events;
  30. }
  31. },
  32. get: {
  33. method: 'GET',
  34. interceptor : {responseError: $rootScope.errorHandler},
  35. transformResponse: function(data, headers) {
  36. data = angular.fromJson(data);
  37. convert_dates(data);
  38. // strip empty keys.
  39. angular.forEach(data.persons || [], function(person, person_idx) {
  40. angular.forEach(person, function(value, key) {
  41. if (value === "") {
  42. delete person[key];
  43. }
  44. });
  45. });
  46. return data;
  47. }
  48. },
  49. update: {
  50. method: 'PUT',
  51. interceptor : {responseError: $rootScope.errorHandler}
  52. }
  53. });
  54. }]
  55. );
  56. eventManServices.factory('EventTicket', ['$resource', '$rootScope',
  57. function($resource, $rootScope) {
  58. return $resource('events/:id/tickets', {event_id: '@event_id', ticket_id: '@_id'}, {
  59. get: {
  60. method: 'GET',
  61. url: 'events/:id/tickets/:ticket_id',
  62. interceptor : {responseError: $rootScope.errorHandler},
  63. transformResponse: function(data, headers) {
  64. data = angular.fromJson(data);
  65. return data.person;
  66. }
  67. },
  68. add: {
  69. method: 'POST',
  70. interceptor : {responseError: $rootScope.errorHandler},
  71. isArray: false,
  72. url: 'events/:event_id/tickets',
  73. params: {uuid: $rootScope.app_uuid},
  74. transformResponse: function(data, headers) {
  75. data = angular.fromJson(data);
  76. return data.person;
  77. }
  78. },
  79. update: {
  80. method: 'PUT',
  81. interceptor : {responseError: $rootScope.errorHandler},
  82. isArray: false,
  83. url: 'events/:event_id/tickets/:ticket_id',
  84. params: {uuid: $rootScope.app_uuid},
  85. transformResponse: function(data, headers) {
  86. return angular.fromJson(data);
  87. }
  88. },
  89. 'delete': {
  90. method: 'DELETE',
  91. interceptor : {responseError: $rootScope.errorHandler},
  92. isArray: false,
  93. url: 'events/:event_id/tickets/:ticket_id',
  94. params: {uuid: $rootScope.app_uuid},
  95. transformResponse: function(data, headers) {
  96. return angular.fromJson(data);
  97. }
  98. }
  99. });
  100. }]
  101. );
  102. eventManServices.factory('Person', ['$resource', '$rootScope',
  103. function($resource, $rootScope) {
  104. return $resource('persons/:id', {id: '@_id'}, {
  105. all: {
  106. method: 'GET',
  107. interceptor : {responseError: $rootScope.errorHandler},
  108. isArray: true,
  109. transformResponse: function(data, headers) {
  110. // TODO: REMOVE!
  111. return [];
  112. data = angular.fromJson(data);
  113. if (data.error) {
  114. return data;
  115. }
  116. return data.persons;
  117. }
  118. },
  119. update: {
  120. method: 'PUT',
  121. interceptor : {responseError: $rootScope.errorHandler}
  122. },
  123. getEvents: {
  124. method: 'GET',
  125. interceptor : {responseError: $rootScope.errorHandler},
  126. url: 'persons/:_id/events',
  127. isArray: true,
  128. transformResponse: function(data, headers) {
  129. data = angular.fromJson(data);
  130. if (data.error) {
  131. return data;
  132. }
  133. angular.forEach(data.events || [], function(event_, event_idx) {
  134. convert_dates(event_);
  135. });
  136. return data.events;
  137. }
  138. }
  139. });
  140. }]
  141. );
  142. eventManServices.factory('Setting', ['$resource', '$rootScope',
  143. function($resource, $rootScope) {
  144. return $resource('settings/', {}, {
  145. query: {
  146. method: 'GET',
  147. interceptor : {responseError: $rootScope.errorHandler},
  148. isArray: true,
  149. transformResponse: function(data, headers) {
  150. data = angular.fromJson(data);
  151. if (data.error) {
  152. return data;
  153. }
  154. return data.settings;
  155. }
  156. },
  157. update: {
  158. method: 'PUT',
  159. interceptor : {responseError: $rootScope.errorHandler}
  160. }
  161. });
  162. }]
  163. );
  164. eventManServices.factory('Info', ['$resource', '$rootScope',
  165. function($resource, $rootScope) {
  166. return $resource('info/', {}, {
  167. get: {
  168. method: 'GET',
  169. interceptor : {responseError: $rootScope.errorHandler},
  170. isArray: false,
  171. transformResponse: function(data, headers) {
  172. data = angular.fromJson(data);
  173. if (data.error) {
  174. return data;
  175. }
  176. return data.info || {};
  177. }
  178. }
  179. });
  180. }]
  181. );
  182. eventManServices.factory('User', ['$resource', '$rootScope',
  183. function($resource, $rootScope) {
  184. return $resource('users/:id', {id: '@_id'}, {
  185. get: {
  186. method: 'GET',
  187. interceptor : {responseError: $rootScope.errorHandler},
  188. transformResponse: function(data, headers) {
  189. data = angular.fromJson(data);
  190. if (data.error) {
  191. return data;
  192. }
  193. return data.user || {};
  194. }
  195. },
  196. add: {
  197. method: 'POST',
  198. interceptor : {responseError: $rootScope.errorHandler}
  199. },
  200. login: {
  201. method: 'POST',
  202. url: '/login',
  203. interceptor : {responseError: $rootScope.errorHandler}
  204. },
  205. logout: {
  206. method: 'GET',
  207. url: '/logout',
  208. interceptor : {responseError: $rootScope.errorHandler}
  209. }
  210. });
  211. }]
  212. );
  213. /* WebSocket collection used to update the list of persons of an Event. */
  214. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
  215. function($websocket, $location, $log) {
  216. var dataStream = null;
  217. var data = {};
  218. var methods = {
  219. data: data,
  220. close: function() {
  221. $log.debug('close WebSocket connection');
  222. dataStream.close();
  223. },
  224. open: function() {
  225. $log.debug('open WebSocket connection');
  226. dataStream && dataStream.close();
  227. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  228. dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
  229. '/ws/' + $location.path() + '/updates');
  230. dataStream.onMessage(function(message) {
  231. $log.debug('EventUpdates message received');
  232. data.update = angular.fromJson(message.data);
  233. });
  234. }
  235. };
  236. return methods;
  237. }]
  238. );