(function () { 'use strict'; // The base Data Context service provides functionality to interact with remote RESTful endpoints / local cache var serviceId = 'appDataContext'; angular.module('app').factory(serviceId, ['$http', '$q', 'DSCacheFactory', appDataContext]); function appDataContext($http, $q, DSCacheFactory) { var service = {}; /** * Process a GET request, returning the response if successful * * Uses a cache to retrieve and store the data if the * cacheName and cacheItemName are set * * When refresh is true, the cached data is ignored */ service.get = function (url, cache, cacheItem, refresh) { return (cache && cacheItem && !refresh) ? getFromCache(url, cache, cacheItem) : request(url, 'get'); }; /** * Process a POST request, returning the response if successful */ service.post = function (url, data) { return request(url, 'post', data); } /** * Process a PUT request, returning the response if successful */ service.put = function (url, data) { return request(url, 'put', data); } /** * Process a POST request, returning the response if successful */ service.delete = function (url) { return request(url, 'delete'); } /** * Update a Cache with new data */ service.updateCache = function (cache, cacheItem, data) { if (cache = DSCacheFactory.get(cache)) { cache.put(cacheItem, data) } } /** * Clear any provided caches */ service.clearCaches = function (caches) { if (!Array.isArray(caches)) { caches = [caches]; } for (var i = 0; i < caches.length; i++) { var cache = DSCacheFactory.get(caches[i]); if (cache) { cache.removeAll() }; } } /** * Remove an item from the provided cache */ service.removeCacheItem = function (cache, cacheItem) { var cache = DSCacheFactory.get(cache); if (cache) { cache.removeItem(cacheItem); } } /** * Make a request with a given type and data * * Returns the http promise object */ function request(url, type, data) { if (!url) { } return $http({ method: type, url: url, data: data }).then(handleSuccess, handleError); } /** * Get an item from a given cache */ function getFromCache(url, cacheName, cacheItem) { var cachedValue; var cache = DSCacheFactory.get(cacheName) || createCache(cacheName); return ((cachedValue = cache.get(cacheItem)) ? Promise.resolve(cachedValue) : request(url, 'get').then(function (data) { cache.put(cacheItem, data); return data; }) ); } /** * Create and return a new cache */ function createCache(name) { var cache = DSCacheFactory(name, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 300000, deleteOnExpire: 'aggressive' }); return cache; } /** * Return a success response */ function handleSuccess(response) { return response.data; } /** * Returns the error response message from the API, or * a default error if the API did not provide a response */ function handleError(response) { if (response.status === 404) { return $q.reject("Not found.", response.status); } if (!angular.isObject(response.data) || !response.data.message) { return $q.reject("An unknown error occurred.", response.status); } return $q.reject(response.data.message); } return service; } })();