services.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. /* Services that are used to interact with the backend. */
  3. var eventManServices = angular.module('eventManServices', ['ngResource']);
  4. eventManServices.factory('Event', ['$resource',
  5. function($resource) {
  6. return $resource('events/:id', {id: '@_id', person_id: '@person_id'}, {
  7. all: {
  8. method: 'GET',
  9. isArray: true,
  10. transformResponse: function(data, headers) {
  11. return angular.fromJson(data).events;
  12. }
  13. },
  14. get: {method: 'GET',
  15. transformResponse: function(data, headers) {
  16. data = angular.fromJson(data);
  17. if (data && data['begin-datetime']) {
  18. data['begin-date'] = data['begin-date'].getTime();
  19. }
  20. if (data && data['end-datetime']) {
  21. data['end-date'] = data['end-date'].getTime();
  22. }
  23. // strip empty keys.
  24. angular.forEach(data.persons || [], function(person, person_idx) {
  25. angular.forEach(person, function(value, key) {
  26. if (value === "") {
  27. delete person[key];
  28. }
  29. });
  30. });
  31. return data;
  32. }
  33. },
  34. update: {method: 'PUT'},
  35. updatePerson: {
  36. method: 'PUT',
  37. isArray: false,
  38. url: 'events/:id/persons/:person_id',
  39. transformResponse: function(data, headers) {
  40. return angular.fromJson(data);
  41. }
  42. },
  43. addPerson: {
  44. method: 'POST',
  45. isArray: false,
  46. url: 'events/:id/persons/:person_id',
  47. transformResponse: function(data, headers) {
  48. return angular.fromJson(data);
  49. }
  50. },
  51. deletePerson: {
  52. method: 'DELETE',
  53. isArray: false,
  54. url: 'events/:_id/persons/:person_id',
  55. transformResponse: function(data, headers) {
  56. return angular.fromJson(data);
  57. }
  58. }
  59. });
  60. }]
  61. );
  62. eventManServices.factory('Person', ['$resource',
  63. function($resource) {
  64. return $resource('persons/:id', {id: '@_id'}, {
  65. all: {
  66. method: 'GET',
  67. isArray: true,
  68. transformResponse: function(data, headers) {
  69. return angular.fromJson(data).persons;
  70. }
  71. },
  72. update: {method: 'PUT'},
  73. getEvents: {
  74. method: 'GET',
  75. url: 'persons/:_id/events',
  76. isArray: true,
  77. transformResponse: function(data, headers) {
  78. return angular.fromJson(data).events;
  79. }
  80. }
  81. });
  82. }]
  83. );
  84. eventManServices.factory('Person', ['$resource',
  85. function($resource) {
  86. return $resource('persons/:id', {id: '@_id'}, {
  87. all: {
  88. method: 'GET',
  89. isArray: true,
  90. transformResponse: function(data, headers) {
  91. return angular.fromJson(data).persons;
  92. }
  93. },
  94. update: {method: 'PUT'},
  95. getEvents: {
  96. method: 'GET',
  97. url: 'persons/:_id/events',
  98. isArray: true,
  99. transformResponse: function(data, headers) {
  100. return angular.fromJson(data).events;
  101. }
  102. }
  103. });
  104. }]
  105. );
  106. eventManServices.factory('Setting', ['$resource',
  107. function($resource) {
  108. return $resource('settings/', {}, {
  109. query: {
  110. method: 'GET',
  111. isArray: true,
  112. transformResponse: function(data, headers) {
  113. return angular.fromJson(data).settings;
  114. }
  115. },
  116. update: {method: 'PUT'},
  117. });
  118. }]
  119. );
  120. /* WebSocket collection used to update the list of persons of an Event. */
  121. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
  122. function($websocket, $location, $log) {
  123. var dataStream = null;
  124. var data = {};
  125. var methods = {
  126. data: data,
  127. close: function() {
  128. $log.debug('close WebSocket connection');
  129. dataStream.close();
  130. },
  131. open: function() {
  132. $log.debug('open WebSocket connection');
  133. dataStream && dataStream.close();
  134. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  135. dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
  136. '/ws/' + $location.path() + '/updates');
  137. dataStream.onMessage(function(message) {
  138. $log.debug('EventUpdates message received');
  139. data.update = angular.fromJson(message.data);
  140. });
  141. }
  142. };
  143. return methods;
  144. }]
  145. );