eventman/angular_app/js/services.js

277 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) {
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'}, {
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.
angular.forEach(data.persons || [], function(person, person_idx) {
angular.forEach(person, function(value, key) {
if (value === "") {
delete person[key];
}
});
});
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}
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'}, {
2016-06-07 23:21:08 +02:00
2016-06-08 23:05:16 +02:00
get: {
method: 'GET',
url: 'events/:id/tickets/:ticket_id',
interceptor : {responseError: $rootScope.errorHandler},
transformResponse: function(data, headers) {
data = angular.fromJson(data);
2016-06-09 23:13:56 +02:00
return data.person;
2016-06-08 23:05:16 +02:00
}
},
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);
return data.person;
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) {
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('Person', ['$resource', '$rootScope',
function($resource, $rootScope) {
2015-03-21 20:31:36 +01:00
return $resource('persons/: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 20:31:36 +01:00
isArray: true,
2015-03-21 15:45:40 +01:00
transformResponse: function(data, headers) {
// TODO: REMOVE!
return [];
2016-05-31 22:26:38 +02:00
data = angular.fromJson(data);
if (data.error) {
return data;
}
return data.persons;
2015-03-21 15:45:40 +01:00
}
2015-03-21 16:48:00 +01:00
},
2016-05-29 11:34:39 +02:00
update: {
method: 'PUT',
interceptor : {responseError: $rootScope.errorHandler}
},
2015-04-04 14:15:52 +02:00
getEvents: {
method: 'GET',
2016-05-29 11:34:39 +02:00
interceptor : {responseError: $rootScope.errorHandler},
url: 'persons/:_id/events',
2015-04-04 14:15:52 +02:00
isArray: true,
transformResponse: function(data, headers) {
2016-04-25 13:40:03 +02:00
data = angular.fromJson(data);
2016-05-29 11:34:39 +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_);
});
return data.events;
2015-04-18 17:33:42 +02:00
}
}
});
}]
);
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'}, {
get: {
method: 'GET',
interceptor : {responseError: $rootScope.errorHandler},
transformResponse: function(data, headers) {
data = angular.fromJson(data);
if (data.error) {
return data;
}
return data.user || {};
}
},
2016-06-12 23:44:48 +02:00
add: {
method: 'POST',
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}
}
});
}]
);
/* WebSocket collection used to update the list of persons of an Event. */
eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log',
function($websocket, $location, $log) {
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() {
$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);
});
2015-04-26 12:42:47 +02:00
}
};
return methods;
}]
);