services.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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', person_id: '@person_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. updatePerson: {
  54. method: 'PUT',
  55. interceptor : {responseError: $rootScope.errorHandler},
  56. isArray: false,
  57. url: 'events/:id/persons/:person_id',
  58. params: {uuid: $rootScope.app_uuid},
  59. transformResponse: function(data, headers) {
  60. return angular.fromJson(data);
  61. }
  62. },
  63. addPerson: {
  64. method: 'POST',
  65. interceptor : {responseError: $rootScope.errorHandler},
  66. isArray: false,
  67. url: 'events/:id/persons/:person_id',
  68. params: {uuid: $rootScope.app_uuid},
  69. transformResponse: function(data, headers) {
  70. return angular.fromJson(data);
  71. }
  72. },
  73. deletePerson: {
  74. method: 'DELETE',
  75. interceptor : {responseError: $rootScope.errorHandler},
  76. isArray: false,
  77. url: 'events/:_id/persons/:person_id',
  78. params: {uuid: $rootScope.app_uuid},
  79. transformResponse: function(data, headers) {
  80. return angular.fromJson(data);
  81. }
  82. }
  83. });
  84. }]
  85. );
  86. eventManServices.factory('EventTicket', ['$resource', '$rootScope',
  87. function($resource, $rootScope) {
  88. return $resource('events/:id/tickets', {id: '@_id', person_id: '@person_id'}, {
  89. updateTicket: {
  90. method: 'PUT',
  91. interceptor : {responseError: $rootScope.errorHandler},
  92. isArray: false,
  93. url: 'events/:id/ticketss/:person_id',
  94. params: {uuid: $rootScope.app_uuid},
  95. transformResponse: function(data, headers) {
  96. return angular.fromJson(data);
  97. }
  98. },
  99. addTicket: {
  100. method: 'POST',
  101. interceptor : {responseError: $rootScope.errorHandler},
  102. isArray: false,
  103. url: 'events/:id/tickets/:person_id',
  104. params: {uuid: $rootScope.app_uuid},
  105. transformResponse: function(data, headers) {
  106. return angular.fromJson(data);
  107. }
  108. },
  109. deleteTicket: {
  110. method: 'DELETE',
  111. interceptor : {responseError: $rootScope.errorHandler},
  112. isArray: false,
  113. url: 'events/:_id/tickets/:person_id',
  114. params: {uuid: $rootScope.app_uuid},
  115. transformResponse: function(data, headers) {
  116. return angular.fromJson(data);
  117. }
  118. }
  119. });
  120. }]
  121. );
  122. eventManServices.factory('Person', ['$resource', '$rootScope',
  123. function($resource, $rootScope) {
  124. return $resource('persons/:id', {id: '@_id'}, {
  125. all: {
  126. method: 'GET',
  127. interceptor : {responseError: $rootScope.errorHandler},
  128. isArray: true,
  129. transformResponse: function(data, headers) {
  130. data = angular.fromJson(data);
  131. if (data.error) {
  132. return data;
  133. }
  134. return data.persons;
  135. }
  136. },
  137. update: {
  138. method: 'PUT',
  139. interceptor : {responseError: $rootScope.errorHandler}
  140. },
  141. getEvents: {
  142. method: 'GET',
  143. interceptor : {responseError: $rootScope.errorHandler},
  144. url: 'persons/:_id/events',
  145. isArray: true,
  146. transformResponse: function(data, headers) {
  147. data = angular.fromJson(data);
  148. if (data.error) {
  149. return data;
  150. }
  151. angular.forEach(data.events || [], function(event_, event_idx) {
  152. convert_dates(event_);
  153. });
  154. return data.events;
  155. }
  156. }
  157. });
  158. }]
  159. );
  160. eventManServices.factory('Setting', ['$resource', '$rootScope',
  161. function($resource, $rootScope) {
  162. return $resource('settings/', {}, {
  163. query: {
  164. method: 'GET',
  165. interceptor : {responseError: $rootScope.errorHandler},
  166. isArray: true,
  167. transformResponse: function(data, headers) {
  168. data = angular.fromJson(data);
  169. if (data.error) {
  170. return data;
  171. }
  172. return data.settings;
  173. }
  174. },
  175. update: {
  176. method: 'PUT',
  177. interceptor : {responseError: $rootScope.errorHandler}
  178. }
  179. });
  180. }]
  181. );
  182. eventManServices.factory('Info', ['$resource', '$rootScope',
  183. function($resource, $rootScope) {
  184. return $resource('info/', {}, {
  185. get: {
  186. method: 'GET',
  187. interceptor : {responseError: $rootScope.errorHandler},
  188. isArray: false,
  189. transformResponse: function(data, headers) {
  190. data = angular.fromJson(data);
  191. if (data.error) {
  192. return data;
  193. }
  194. return data.info || {};
  195. }
  196. }
  197. });
  198. }]
  199. );
  200. /* WebSocket collection used to update the list of persons of an Event. */
  201. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
  202. function($websocket, $location, $log) {
  203. var dataStream = null;
  204. var data = {};
  205. var methods = {
  206. data: data,
  207. close: function() {
  208. $log.debug('close WebSocket connection');
  209. dataStream.close();
  210. },
  211. open: function() {
  212. $log.debug('open WebSocket connection');
  213. dataStream && dataStream.close();
  214. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  215. dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
  216. '/ws/' + $location.path() + '/updates');
  217. dataStream.onMessage(function(message) {
  218. $log.debug('EventUpdates message received');
  219. data.update = angular.fromJson(message.data);
  220. });
  221. }
  222. };
  223. return methods;
  224. }]
  225. );