(function () { 'use strict'; var serviceId = 'coursesDataContext'; angular.module('courses').factory(serviceId, ['$q', '$http', 'coursesConfig', '$modal', datacontext]); function datacontext($q, $http, config, $modal) { var service = { getCourses: getCourses, getCoursesIncludingParents: getCoursesIncludingParents, getCourse: getCourse, deleteCourse: deleteCourse, updateCourse: updateCourse, createCourse: createCourse }; 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 getCourses(organisationId) { var request = $http({ method: "get", url: config.apigatewayUrl + 'coursetraining/api/courses/organisation/' + organisationId }); return (request.then(handleSuccess, handleError)); } function getCoursesIncludingParents(organisationId) { var request = $http({ method: "get", url: config.apigatewayUrl + '/coursetraining/api/courses/organisation/' + organisationId + '?includeParents=true' }); return (request.then(handleSuccess, handleError)); } function getCourse(courseId) { var request = $http({ method: "get", url: config.apigatewayUrl + 'coursetraining/api/courses/' + courseId }); return (request.then(handleSuccess, handleError)); } function deleteCourse(courseId) { var request = $http({ method: "delete", url: config.apigatewayUrl + 'coursetraining/api/courses/' + courseId }); return (request.then(handleSuccess, handleError)); } function updateCourse(courseId, course) { var request = $http({ method: "put", url: config.apigatewayUrl + 'coursetraining/api/courses/' + courseId, data:course }); return (request.then(handleSuccess, handleError)); } function createCourse(course) { var request = $http({ method: "post", url: config.apigatewayUrl + 'coursetraining/api/courses/', data: course }); return (request.then(handleSuccess, handleError)); } } })();