Created
February 20, 2015 13:56
-
-
Save vitaliy-bobrov/3daf6f4af8fecc372a92 to your computer and use it in GitHub Desktop.
Tutorial App part 6 - Comments factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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