all files / app/core/services/ api-resolver.service.js

16.67% Statements 5/30
0% Branches 0/12
16.67% Functions 1/6
16.67% Lines 5/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                        
(function ()
{
    'use strict';
 
    angular
        .module('app.core')
        .factory('apiResolver', apiResolverService);
 
    /** @ngInject */
    function apiResolverService($q, $log, api)
    {
        var service = {
            resolve: resolve
        };
 
        return service;
 
        //////////
        /**
         * Resolve api
         * @param action
         * @param parameters
         */
        function resolve(action, parameters)
        {
            var actionParts = action.split('@'),
                resource = actionParts[0],
                method = actionParts[1],
                params = parameters || {};
 
            if ( !resource || !method )
            {
                $log.error('apiResolver.resolve requires correct action parameter (ResourceName@methodName)');
                return false;
            }
 
            // Create a new deferred object
            var deferred = $q.defer();
 
            // Get the correct api object from api service
            var apiObject = getApiObject(resource);
 
            if ( !apiObject )
            {
                $log.error('Resource "' + resource + '" is not defined in the api service!');
                deferred.reject('Resource "' + resource + '" is not defined in the api service!');
            }
            else
            {
                apiObject[method](params,
 
                    // Success
                    function (response)
                    {
                        deferred.resolve(response);
                    },
 
                    // Error
                    function (response)
                    {
                        deferred.reject(response);
                    }
                );
            }
 
            // Return the promise
            return deferred.promise;
        }
 
        /**
         * Get correct api object
         *
         * @param resource
         * @returns {*}
         */
        function getApiObject(resource)
        {
            // Split the resource in case if we have a dot notated object
            var resourceParts = resource.split('.'),
                apiObject = api;
 
            // Loop through the resource parts and go all the way through
            // the api object and return the correct one
            for ( var l = 0; l < resourceParts.length; l++ )
            {
                if ( angular.isUndefined(apiObject[resourceParts[l]]) )
                {
                    $log.error('Resource part "' + resourceParts[l] + '" is not defined!');
                    apiObject = false;
                    break;
                }
 
                apiObject = apiObject[resourceParts[l]];
            }
 
            if ( !apiObject )
            {
                return false;
            }
 
            return apiObject;
        }
    }
 
})();