Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.