Skip to content

Instantly share code, notes, and snippets.

@vitaliy-bobrov
Created February 20, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaliy-bobrov/3daf6f4af8fecc372a92 to your computer and use it in GitHub Desktop.
Save vitaliy-bobrov/3daf6f4af8fecc372a92 to your computer and use it in GitHub Desktop.
Tutorial App part 6 - Comments factory
.factory('Comments', ['$http', '$q', 'config', function($http, $q, config) {
//Endpoints Variables.
var commentsCheckEndpoint = 'api/comment/countAll',
commentsEndpoint = 'api/comments/',
postCommentEndpoint = 'api/comment';
return {
/*
* Check Comments number by Article ID.
*/
check: function(articleId, token) {
var defer = $q.defer();
$http({
method : 'POST',
url : config.serviceBaseUrl + commentsCheckEndpoint,
dataType : 'json',
data : {
nid: articleId,
},
headers: {
'X-CSRF-Token': token,
},
})
.success(function(data, status, headers, config) {
defer.resolve(data[0]);
})
.error(function(data, status, headers, config) {
defer.reject(data[0]);
});
return defer.promise;
},
/*
* Return Comments by Article ID.
*/
get: function(articleId) {
var defer = $q.defer();
$http({
method : 'GET',
url : config.serviceBaseUrl + commentsEndpoint + articleId,
dataType : 'json',
})
.success(function(data, status, headers, config) {
defer.resolve(data);
})
.error(function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
},
/*
* Post Comment to the article.
*/
post: function(commentData, token) {
var defer = $q.defer();
$http({
method : 'POST',
url : config.serviceBaseUrl + postCommentEndpoint,
dataType : 'json',
data : commentData,
headers: {
'X-CSRF-Token': token,
},
})
.success(function(data, status, headers, config) {
defer.resolve(data);
})
.error(function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment