(function () { 'use strict'; var serviceId = 'commentsDataContext'; angular.module('comments').factory(serviceId, ['$q', '$http', 'commentsConfig', datacontext]); function datacontext($q, $http, config, $modal) { var service = { getCommentByExternalId: getCommentByExternalId, createComment: createComment, getCommentById: getCommentById, deleteComment: deleteComment }; return service; function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if ( !angular.isObject(response.data) || !response.data.message ) { return ($q.reject("An unknown error occurred.")); } // Otherwise, use expected error message. return ($q.reject(response.data.message)); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return (response.data); } function getCommentByExternalId(externalId, userId) { var request = $http({ method: "get", url: config.commentsUrl + 'comments/external/' + externalId + '/user/' + userId, }); return (request.then(handleSuccess, handleError)); } function getCommentById(commentId) { var request = $http({ method: "get", url: config.commentsUrl + 'comments/' + commentId, }); return (request.then(handleSuccess, handleError)); } function createComment(comment) { var request = $http({ method: "post", url: config.commentsUrl + 'comments', data: comment }); return (request.then(handleSuccess, handleError)); } function deleteComment(comment) { var request = $http({ method: "delete", url: config.commentsUrl + 'comments/' + comment.id, }); return (request.then(handleSuccess, handleError)); } } })();