(function() { "use strict"; angular.module("raz") .service("deliverableService", ["$http", "$q", function ($http, $q) { var resourceCollection = {whatsNewInfo: [], popularBookInfo: []}; var currentWhatsNewBookCount = 0; var currentWhatsNewNonbookCount = 0; var currentPopularBookCount = 0; return { getWhatsNewBookCount: getWhatsNewBookCount, getWhatsNewNonbookCount: getWhatsNewNonbookCount, getPopularBookCount: getPopularBookCount, setInitPopularBook: setInitPopularBook, setInitWhatsNew: setInitWhatsNew, getWhatsNewInfo: getWhatsNewInfo, getPopularBookInfo: getPopularBookInfo }; function getWhatsNewBookCount() { return currentWhatsNewBookCount; } function getWhatsNewNonbookCount() { return currentWhatsNewNonbookCount; } function getPopularBookCount() { return currentPopularBookCount; } function setWhatsNewCount(whatsNewBookCount, whatsNewNonbookCount) { currentWhatsNewBookCount = whatsNewBookCount; currentWhatsNewNonbookCount = whatsNewNonbookCount; } function setPopularBookCount(popularBookCount) { currentWhatsNewBookCount = popularBookCount; } function setInitWhatsNew(languageId, whatsNewBook, whatsNewCloseRead) { var whatsNew = []; whatsNew['book'] = whatsNewBook; whatsNew['nonbook'] = whatsNewCloseRead; resourceCollection.whatsNewInfo[languageId] = whatsNew; setWhatsNewCount(whatsNew['book'].count, whatsNew['nonbook'].count); } function setInitPopularBook(gradeId, popularBook) { resourceCollection.popularBookInfo[gradeId] = popularBook; setPopularBookCount(popularBook.count); } function getWhatsNewInfo(languageId) { var whatsNew = null; if (resourceCollection.whatsNewInfo[languageId]) { var deferred = $q.defer(); whatsNew = resourceCollection.whatsNewInfo[languageId]; deferred.resolve(whatsNew); setWhatsNewCount(whatsNew['book'].count, whatsNew['nonbook'].count); return deferred.promise; } else { var url = '/api/deliverable/whatsNewInfo/language/' + languageId; return $http.get(url) .then(function (response) { whatsNew = response.data; resourceCollection.whatsNewInfo[languageId] = whatsNew; setWhatsNewCount(whatsNew['book'].count, whatsNew['nonbook'].count); return response.data; }); } } function getPopularBookInfo(gradeId) { if (resourceCollection.popularBookInfo[gradeId]) { var deferred = $q.defer(); deferred.resolve(resourceCollection.popularBookInfo[gradeId]); setPopularBookCount(resourceCollection.popularBookInfo[gradeId].count); return deferred.promise; } else { if (gradeId == 'K') { gradeId = 0; } var url = '/api/deliverable/popularBookInfo/grade/' + gradeId; return $http.get(url) .then(function (response) { resourceCollection.popularBookInfo[gradeId] = response.data; setPopularBookCount(resourceCollection.popularBookInfo[gradeId].count); return response.data; }); } } }]); })();