services.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return data;
  24. }
  25. },
  26. update: {method: 'PUT'},
  27. updatePerson: {
  28. method: 'PUT',
  29. isArray: true,
  30. url: 'events/:id/persons/:person_id',
  31. transformResponse: function(data, headers) {
  32. return angular.fromJson(data).event.persons;
  33. }
  34. },
  35. addPerson: {
  36. method: 'POST',
  37. isArray: true,
  38. url: 'events/:id/persons/:person_id',
  39. transformResponse: function(data, headers) {
  40. return angular.fromJson(data).event.persons;
  41. }
  42. },
  43. deletePerson: {
  44. method: 'DELETE',
  45. isArray: true,
  46. url: 'events/:_id/persons/:person_id',
  47. transformResponse: function(data, headers) {
  48. return angular.fromJson(data).event.persons;
  49. }
  50. }
  51. });
  52. }]
  53. );
  54. eventManServices.factory('Person', ['$resource',
  55. function($resource) {
  56. return $resource('persons/:id', {id: '@_id'}, {
  57. all: {
  58. method: 'GET',
  59. isArray: true,
  60. transformResponse: function(data, headers) {
  61. return angular.fromJson(data).persons;
  62. }
  63. },
  64. update: {method: 'PUT'},
  65. getEvents: {
  66. method: 'GET',
  67. url: 'persons/:_id/events',
  68. isArray: true,
  69. transformResponse: function(data, headers) {
  70. return angular.fromJson(data).events;
  71. }
  72. }
  73. });
  74. }]
  75. );
  76. eventManServices.factory('Person', ['$resource',
  77. function($resource) {
  78. return $resource('persons/:id', {id: '@_id'}, {
  79. all: {
  80. method: 'GET',
  81. isArray: true,
  82. transformResponse: function(data, headers) {
  83. return angular.fromJson(data).persons;
  84. }
  85. },
  86. update: {method: 'PUT'},
  87. getEvents: {
  88. method: 'GET',
  89. url: 'persons/:_id/events',
  90. isArray: true,
  91. transformResponse: function(data, headers) {
  92. return angular.fromJson(data).events;
  93. }
  94. }
  95. });
  96. }]
  97. );
  98. eventManServices.factory('Setting', ['$resource',
  99. function($resource) {
  100. return $resource('settings/', {}, {
  101. query: {
  102. method: 'GET',
  103. isArray: true,
  104. transformResponse: function(data, headers) {
  105. return angular.fromJson(data).settings;
  106. }
  107. },
  108. update: {method: 'PUT'},
  109. });
  110. }]
  111. );
  112. /* WebSocket collection used to update the list of persons of an Event. */
  113. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
  114. function($websocket, $location, $log) {
  115. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  116. var dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
  117. '/ws/' + $location.path() + '/updates');
  118. var data = {};
  119. dataStream.onMessage(function(message) {
  120. $log.debug('EventUpdates message received');
  121. data.persons = angular.fromJson(message.data);
  122. });
  123. var methods = {
  124. data: data,
  125. };
  126. return methods;
  127. }]
  128. );