eventman/angular_app/js/services.js

273 lines
8.8 KiB
JavaScript
Raw Normal View History

2015-03-22 08:57:57 +01:00
'use strict';
2015-03-22 09:36:32 +01:00
/* Services that are used to interact with the backend. */
2015-03-21 13:21:15 +01:00
var eventManServices = angular.module('eventManServices', ['ngResource']);
2015-03-22 09:19:30 +01:00
2016-04-25 13:40:03 +02:00
/* Modify, in place, an object to convert datetime. */
function convert_dates(obj) {
angular.forEach(['begin_date', 'end_date', 'ticket_sales_begin_date', 'ticket_sales_end_date'], function(key, key_idx) {
if (!obj[key]) {
return;
}
obj[key] = obj[key].getTime();
});
2016-04-25 13:40:03 +02:00
return obj;
}
eventManServices.factory('Event', ['$resource', '$rootScope',
function($resource, $rootScope) {
return $resource('events/:id', {id: '@_id'}, {
2015-03-21 15:45:40 +01:00
all: {
method: 'GET',
2016-05-29 11:34:39 +02:00
interceptor : {responseError: $rootScope.errorHandler},
2015-03-21 15:45:40 +01:00
isArray: true,
transformResponse: function(data, headers) {
2016-04-25 13:40:03 +02:00
data = angular.fromJson(data);
2016-05-31 22:26:38 +02:00
if (data.error) {
return data;
}
2016-04-25 13:40:03 +02:00
angular.forEach(data.events || [], function(event_, event_idx) {
convert_dates(event_);
});
2016-06-06 21:44:04 +02:00
2016-04-25 13:40:03 +02:00
return data.events;
2015-03-21 15:45:40 +01:00
}
},
2016-05-29 11:34:39 +02:00
get: {
method: 'GET',
interceptor : {responseError: $rootScope.errorHandler},
2015-03-23 23:06:44 +01:00
transformResponse: function(data, headers) {
data = angular.fromJson(data);
2016-04-25 13:40:03 +02:00
convert_dates(data);
2016-04-17 19:14:52 +02:00
// strip empty keys.
2016-07-09 14:20:48 +02:00
angular.forEach(data.tickets || [], function(ticket, ticket_idx) {
angular.forEach(ticket, function(value, key) {
2016-04-17 19:14:52 +02:00
if (value === "") {
2016-07-09 14:20:48 +02:00
delete ticket[key];
2016-04-17 19:14:52 +02:00
}
});
});
2015-03-23 23:06:44 +01:00
return data;
}
},
2016-05-29 11:34:39 +02:00
update: {
method: 'PUT',
interceptor : {responseError: $rootScope.errorHandler}
},
group_persons: {
method: 'GET',
url: 'events/:id/group_persons',
isArray: true,
transformResponse: function(data, headers) {
data = angular.fromJson(data);
return data.persons || [];
}
2015-04-05 00:55:59 +02:00
}
2015-03-21 15:45:40 +01:00
});
}]
2015-03-21 13:21:15 +01:00
);
2016-06-07 23:21:08 +02:00
eventManServices.factory('EventTicket', ['$resource', '$rootScope',
function($resource, $rootScope) {
return $resource('events/:id/tickets', {event_id: '@event_id', ticket_id: '@_id'}, {
all: {
method: 'GET',
url: '/tickets',
interceptor : {responseError: $rootScope.errorHandler},
isArray: true,
transformResponse: function(data, headers) {
data = angular.fromJson(data);
2016-07-09 14:20:48 +02:00
return data.tickets;
}
},
2016-07-09 20:24:45 +02:00
get: {
method: 'GET',
url: 'events/:id/tickets/:ticket_id',
interceptor : {responseError: $rootScope.errorHandler},
transformResponse: function(data, headers) {
data = angular.fromJson(data);
2016-07-10 09:35:55 +02:00
if (data.error) {
return data;
}
2016-07-09 20:24:45 +02:00
return data.ticket;
}
},
2016-06-09 23:13:56 +02:00
add: {
2016-06-08 23:05:16 +02:00
method: 'POST',
2016-06-07 23:21:08 +02:00
interceptor : {responseError: $rootScope.errorHandler},
isArray: false,
url: 'events/:event_id/tickets',
2016-06-07 23:21:08 +02:00
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
2016-06-09 23:13:56 +02:00
data = angular.fromJson(data);
2016-07-10 09:35:55 +02:00
if (data.error) {
return data;
}
2016-07-09 14:20:48 +02:00
return data.ticket;
2016-06-07 23:21:08 +02:00
}
},
2016-07-02 14:49:39 +02:00
update: {
2016-06-08 23:05:16 +02:00
method: 'PUT',
2016-06-07 23:21:08 +02:00
interceptor : {responseError: $rootScope.errorHandler},
isArray: false,
url: 'events/:event_id/tickets/:ticket_id',
2016-06-07 23:21:08 +02:00
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
2016-07-10 09:35:55 +02:00
if (data.error) {
return data;
}
2016-06-07 23:21:08 +02:00
return angular.fromJson(data);
}
},
2016-07-02 14:49:39 +02:00
'delete': {
2016-06-07 23:21:08 +02:00
method: 'DELETE',
interceptor : {responseError: $rootScope.errorHandler},
isArray: false,
url: 'events/:event_id/tickets/:ticket_id',
2016-06-07 23:21:08 +02:00
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
return angular.fromJson(data);
}
}
});
}]
);
2016-05-29 11:34:39 +02:00
eventManServices.factory('Setting', ['$resource', '$rootScope',
function($resource, $rootScope) {
2015-04-18 17:33:42 +02:00
return $resource('settings/', {}, {
query: {
method: 'GET',
2016-05-29 11:34:39 +02:00
interceptor : {responseError: $rootScope.errorHandler},
2015-04-18 17:33:42 +02:00
isArray: true,
transformResponse: function(data, headers) {
2016-05-31 22:26:38 +02:00
data = angular.fromJson(data);
if (data.error) {
return data;
}
return data.settings;
2015-04-18 17:33:42 +02:00
}
},
2016-05-29 11:34:39 +02:00
update: {
method: 'PUT',
interceptor : {responseError: $rootScope.errorHandler}
}
2015-04-18 17:33:42 +02:00
});
}]
);
2016-05-29 11:34:39 +02:00
eventManServices.factory('Info', ['$resource', '$rootScope',
function($resource, $rootScope) {
return $resource('info/', {}, {
2016-05-29 11:34:39 +02:00
get: {
method: 'GET',
interceptor : {responseError: $rootScope.errorHandler},
isArray: false,
transformResponse: function(data, headers) {
2016-05-31 22:26:38 +02:00
data = angular.fromJson(data);
if (data.error) {
return data;
}
return data.info || {};
}
}
});
}]
);
2016-06-12 16:04:46 +02:00
eventManServices.factory('User', ['$resource', '$rootScope',
function($resource, $rootScope) {
return $resource('users/:id', {id: '@_id'}, {
2016-07-08 22:10:42 +02:00
all: {
method: 'GET',
interceptor : {responseError: $rootScope.errorHandler},
isArray: true,
transformResponse: function(data, headers) {
data = angular.fromJson(data);
if (data.error) {
return data;
}
return data.users;
}
},
2016-06-12 16:04:46 +02:00
get: {
method: 'GET',
interceptor : {responseError: $rootScope.errorHandler},
transformResponse: function(data, headers) {
2016-07-09 13:34:36 +02:00
return angular.fromJson(data);
2016-06-12 16:04:46 +02:00
}
},
2016-06-12 23:44:48 +02:00
add: {
method: 'POST',
interceptor : {responseError: $rootScope.errorHandler}
},
2016-07-08 22:10:42 +02:00
update: {
method: 'PUT',
interceptor : {responseError: $rootScope.errorHandler}
},
2016-06-12 16:04:46 +02:00
login: {
method: 'POST',
url: '/login',
interceptor : {responseError: $rootScope.errorHandler}
},
logout: {
method: 'GET',
url: '/logout',
interceptor : {responseError: $rootScope.errorHandler}
}
});
}]
);
2016-07-09 14:20:48 +02:00
/* WebSocket collection used to update the list of tickets of an Event. */
2016-07-10 13:10:00 +02:00
eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log', '$rootScope',
function($websocket, $location, $log, $rootScope) {
2015-04-26 12:42:47 +02:00
var dataStream = null;
var data = {};
var methods = {
data: data,
2015-04-26 12:42:47 +02:00
close: function() {
$log.debug('close WebSocket connection');
dataStream.close();
},
open: function() {
var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
2016-07-10 13:10:00 +02:00
var url = proto + '://' + $location.host() + ':' + $location.port() +
'/ws/' + $location.path() + '/updates?uuid=' + $rootScope.app_uuid;
$log.debug('open WebSocket connection to ' + url);
//dataStream && dataStream.close();
dataStream = $websocket(url);
2015-04-26 12:42:47 +02:00
dataStream.onMessage(function(message) {
$log.debug('EventUpdates message received');
data.update = angular.fromJson(message.data);
});
2015-04-26 12:42:47 +02:00
}
};
return methods;
}]
);