Open dlangerenken opened 8 years ago
This is the complete file, however, this needs dependencies (ng-file-upload)
/**
* Generic data service to interact with Sails.js backend. This will just
* wrap $sailsSocket methods to a single service, that is used from application.
*
* This is needed because we need to make some common url handling for sails
* endpoint.
*/
(function () {
'use strict';
angular.module('frontend.core.services')
.factory('DataService', [
'$sailsSocket',
'_',
'BackendConfig',
'Upload',
'$localStorage',
function factory($sailsSocket,
_,
BackendConfig, Upload, $localStorage) {
/**
* Helper function to get "proper" end point url for sails backend API.
*
* @param {string} endPoint Name of the end point
* @param {number} [identifier] Identifier of endpoint object
*
* @returns {string}
* @private
*/
function _parseEndPointUrl(endPoint, identifier) {
if (!_.isUndefined(identifier)) {
endPoint = endPoint + '/' + identifier;
}
return BackendConfig.url + '/' + endPoint;
}
/**
* Helper function to parse used parameters in 'get' and 'count' methods.
*
* @param {{}} parameters Used query parameters
*
* @returns {{params: {}}}
* @private
*/
function _parseParameters(parameters) {
parameters = parameters || {};
return {params: parameters};
}
return {
/**
* Service method to get count of certain end point objects.
*
* @param {string} endPoint Name of the end point
* @param {{}} parameters Used query parameters
*
* @returns {Promise|*}
*/
count: function count(endPoint, parameters) {
return $sailsSocket
.get(_parseEndPointUrl(endPoint) + '/count/', _parseParameters(parameters));
},
/**
* Service method to get data from certain end point. This will always return a collection
* of data.
*
* @param {string} endPoint Name of the end point
* @param {{}} parameters Used query parameters
*
* @returns {Promise|*}
*/
collection: function collection(endPoint, parameters) {
return $sailsSocket
.get(_parseEndPointUrl(endPoint), _parseParameters(parameters));
},
/**
* Service method to get data from certain end point. This will return just a one
* record as an object.
*
* @param {string} endPoint Name of the end point
* @param {number} identifier Identifier of endpoint object
* @param {{}} parameters Used query parameters
*
* @returns {Promise|*}
*/
fetch: function fetch(endPoint, identifier, parameters) {
return $sailsSocket
.get(_parseEndPointUrl(endPoint, identifier), _parseParameters(parameters));
},
/**
* Service method to create new object to specified end point.
*
* @param {string} endPoint Name of the end point
* @param {{}} data Data to update
*
* @returns {Promise|*}
*/
create: function create(endPoint, data) {
return $sailsSocket
.post(_parseEndPointUrl(endPoint), data);
},
/**
* Service method to update specified end point object.
*
* @param {string} endPoint Name of the end point
* @param {number} identifier Identifier of endpoint object
* @param {{}} data Data to update
*
* @returns {Promise|*}
*/
update: function update(endPoint, identifier, data) {
return $sailsSocket
.put(_parseEndPointUrl(endPoint, identifier), data);
},
/**
* Service method to upload files which has to be done with 'Upload' and http-requests rather
* than using sockets (which are not supported yet!
* @param endPoint
* @param data
* @returns {*}
*/
upload: function upload(endPoint, data) {
return Upload.upload({
url: _parseEndPointUrl(endPoint),
data: data,
header: {'Authorization': 'Bearer ' + $localStorage.credentials.token}
});
},
/**
* Service method to delete specified object.
*
* @param {string} endPoint Name of the end point
* @param {number} identifier Identifier of endpoint object
*
* @returns {Promise|*}
*/
delete: function remove(endPoint, identifier) {
return $sailsSocket
.delete(_parseEndPointUrl(endPoint, identifier));
}
};
}
])
;
}());
I could not find a way to upload files via your socket-connection. http://sailsjs.org/documentation/reference/request-req tells me this is not supported for websockets.
I added an upload function to your DataService.js:
where Upload is from ng-file-upload (https://github.com/danialfarid/ng-file-upload).
I don't have time to make a proper pull-request and I am not sure whether or not this is the way to do it. It solved my problem though.