"use strict"; angular.module("vocabulary") .service("categoryService", ["$q", "categoryApi", "messageHandler", "FeatureCheck", function ($q, categoryApi, messageHandler, FeatureCheck) { var categoryList = []; var entireCategoryTree = []; function getCategoryTree() { var apiCall; if(categoryList.length === 0) { apiCall = categoryApi.getCategoryList().then(function (categoryTree) { if (categoryTree.length === 0) { messageHandler.publishError("There was a problem processing your request"); } categoryList = categoryTree; return categoryList; }).catch(function (reason) { messageHandler.publishError("There was a problem processing your request"); return []; }); }else{ apiCall = $q.resolve(categoryList); } return apiCall.then(function(result){ return result; }); } function getEntireCategoryTree(refresh) { var apiCall; if (refresh) { entireCategoryTree = []; } if(entireCategoryTree.length === 0) { apiCall = categoryApi.getEntireCategoryTree().then(function (categoryTree) { if (categoryTree.length === 0) { messageHandler.publishError("There was a problem processing your request"); } entireCategoryTree = categoryTree; return entireCategoryTree; }).catch(function (reason) { messageHandler.publishError("There was a problem processing your request"); return []; }); }else{ apiCall = $q.resolve(entireCategoryTree); } return apiCall.then(function(result){ return result; }); } function getCategoryLessons(id){ return categoryApi.getLessonsForCategory(id).then( function(categoryLessons){ return categoryLessons; }).catch(function(reason){ messageHandler.publishError("There was a problem processing your request"); return []; }); } function getPremadeCategoryDescription(id){ if(FeatureCheck.isFeatureEnabled('VAZ_TOPIC_DESCRIPTIONS')) { return categoryApi.getPremadeCategoryDescription(id).then(function (data) { return data[0]['category_description']; }).catch(function (reason) { messageHandler.publishError("There was a problem processing your request"); return []; }); }else{ return undefined; } } function getCategoryList() { return categoryList; } function getCategory(id) { return categoryList.filter(function (category) { return category.categoryId === id; }).pop(); } function getCategoryName(id) { var category = getCategory(id); if(!category) { return refreshCategoryList() .then(function () { category = getCategory(id); return category ? category.categoryName : ''; }); } return $q.resolve(category ? category.categoryName : ''); } function deleteCategory(categoryId, parentCategoryId){ return categoryApi.deleteCategory(categoryId, parentCategoryId) .then(function(responseData){ return responseData }); } function updateCategoryName(newCategoryName, categoryId){ return categoryApi.updateCategoryName(categoryId, newCategoryName) .then(function(responseData){ return responseData; }) } function createCategory(newCategoryName, parentCategoryId) { var newCategory = null; return categoryApi.createCategory(new NewCategoryData(newCategoryName, parentCategoryId)) .then(function (newCatData) { newCategory = newCatData; }) .then(refreshCategoryList) .then(function () { return newCategory; }); } function refreshCategoryList() { return categoryApi.getCategoryList() .then(function(categories){ categoryList = categories; }); } function NewCategoryData(name, parentId) { this.categoryName = name; this.parentCategoryId = parentId; } return { getCategoryTree : getCategoryTree, getEntireCategoryTree: getEntireCategoryTree, getCategoryList: getCategoryList, getCategory: getCategory, getCategoryName: getCategoryName, getPremadeCategoryDescription: getPremadeCategoryDescription, createCategory: createCategory, refreshCategoryList: refreshCategoryList, getCategoryLessons: getCategoryLessons, updateCategoryName: updateCategoryName, deleteCategory: deleteCategory }; }]);