123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- 'use strict';
- /* Services that are used to interact with the backend. */
- var eventManServices = angular.module('eventManServices', ['ngResource']);
- /* Modify, in place, an object to convert datetime. */
- function convert_dates(obj) {
- if (obj['begin-date']) {
- obj['begin-date'] = obj['begin_date'] = obj['begin-date'].getTime();
- }
- if (obj['end-date']) {
- obj['end-date'] = obj['end_date'] = obj['end-date'].getTime();
- }
- return obj;
- }
- eventManServices.factory('Event', ['$resource', '$rootScope',
- function($resource, $rootScope) {
- return $resource('events/:id', {id: '@_id', ticket_id: '@_id'}, {
- all: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: true,
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- if (data.error) {
- return data;
- }
- angular.forEach(data.events || [], function(event_, event_idx) {
- convert_dates(event_);
- });
- return data.events;
- }
- },
- get: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- convert_dates(data);
- // strip empty keys.
- angular.forEach(data.persons || [], function(person, person_idx) {
- angular.forEach(person, function(value, key) {
- if (value === "") {
- delete person[key];
- }
- });
- });
- return data;
- }
- },
- update: {
- method: 'PUT',
- interceptor : {responseError: $rootScope.errorHandler}
- }
- });
- }]
- );
- eventManServices.factory('EventTicket', ['$resource', '$rootScope',
- function($resource, $rootScope) {
- return $resource('events/:id/tickets', {event_id: '@event_id', ticket_id: '@_id'}, {
- get: {
- method: 'GET',
- url: 'events/:id/tickets/:ticket_id',
- interceptor : {responseError: $rootScope.errorHandler},
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- return data.person;
- }
- },
- add: {
- method: 'POST',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: false,
- url: 'events/:event_id/tickets',
- params: {uuid: $rootScope.app_uuid},
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- return data.person;
- }
- },
- update: {
- method: 'PUT',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: false,
- url: 'events/:event_id/tickets/:ticket_id',
- params: {uuid: $rootScope.app_uuid},
- transformResponse: function(data, headers) {
- return angular.fromJson(data);
- }
- },
- 'delete': {
- method: 'DELETE',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: false,
- url: 'events/:event_id/tickets/:ticket_id',
- params: {uuid: $rootScope.app_uuid},
- transformResponse: function(data, headers) {
- return angular.fromJson(data);
- }
- }
- });
- }]
- );
- eventManServices.factory('Person', ['$resource', '$rootScope',
- function($resource, $rootScope) {
- return $resource('persons/:id', {id: '@_id'}, {
- all: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: true,
- transformResponse: function(data, headers) {
- // TODO: REMOVE!
- return [];
- data = angular.fromJson(data);
- if (data.error) {
- return data;
- }
- return data.persons;
- }
- },
- update: {
- method: 'PUT',
- interceptor : {responseError: $rootScope.errorHandler}
- },
- getEvents: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- url: 'persons/:_id/events',
- isArray: true,
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- if (data.error) {
- return data;
- }
- angular.forEach(data.events || [], function(event_, event_idx) {
- convert_dates(event_);
- });
- return data.events;
- }
- }
- });
- }]
- );
- eventManServices.factory('Setting', ['$resource', '$rootScope',
- function($resource, $rootScope) {
- return $resource('settings/', {}, {
- query: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: true,
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- if (data.error) {
- return data;
- }
- return data.settings;
- }
- },
- update: {
- method: 'PUT',
- interceptor : {responseError: $rootScope.errorHandler}
- }
- });
- }]
- );
- eventManServices.factory('Info', ['$resource', '$rootScope',
- function($resource, $rootScope) {
- return $resource('info/', {}, {
- get: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- isArray: false,
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- if (data.error) {
- return data;
- }
- return data.info || {};
- }
- }
- });
- }]
- );
- eventManServices.factory('User', ['$resource', '$rootScope',
- function($resource, $rootScope) {
- return $resource('users/:id', {id: '@_id'}, {
- get: {
- method: 'GET',
- interceptor : {responseError: $rootScope.errorHandler},
- transformResponse: function(data, headers) {
- data = angular.fromJson(data);
- if (data.error) {
- return data;
- }
- return data.user || {};
- }
- },
- add: {
- method: 'POST',
- interceptor : {responseError: $rootScope.errorHandler}
- },
- login: {
- method: 'POST',
- url: '/login',
- interceptor : {responseError: $rootScope.errorHandler}
- },
- logout: {
- method: 'GET',
- url: '/logout',
- interceptor : {responseError: $rootScope.errorHandler}
- }
- });
- }]
- );
- /* WebSocket collection used to update the list of persons of an Event. */
- eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
- function($websocket, $location, $log) {
- var dataStream = null;
- var data = {};
- var methods = {
- data: data,
- close: function() {
- $log.debug('close WebSocket connection');
- dataStream.close();
- },
- open: function() {
- $log.debug('open WebSocket connection');
- dataStream && dataStream.close();
- var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
- dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() +
- '/ws/' + $location.path() + '/updates');
- dataStream.onMessage(function(message) {
- $log.debug('EventUpdates message received');
- data.update = angular.fromJson(message.data);
- });
- }
- };
- return methods;
- }]
- );
|