%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/news/public/scripts/services/
Upload File :
Create Path :
Current File : /var/www/html/news/public/scripts/services/resource.js

'use strict';

angular.module('services').service('resource', function ($resource, $log, queryBuilder, $location, constants, $http, $q) {

    var vm = this;
    vm.orderParams = {};

    var ServiceForResource = function (endpoint, model) {
        var params = {};
        this.get = function (id, include) {
            if (typeof id !== 'number' && !(typeof id === 'null' || typeof id === 'undefined')) {
                $log.error('First parameter must be a number');
                return;
            }
            if (include) {
                params.include = include;
            }
            if (id) {
                endpoint = endpoint + '/' + id;

                return $resource(endpoint, params).get().$promise.then(function (result) {
                    return result;
                });
            }
            return $resource(endpoint, params).get().$promise.then(function (result) {
                return result;
            });
        };

        this.save = function (data) {
            return $resource(endpoint, params).save(data).$promise;
        };

        this.update = function (id, data) {
            endpoint = endpoint + '/' + id;
            return $resource(endpoint, params, {
                update: {
                    method: 'PUT'
                }
            }).update(id, data).$promise;
        };

        this.delete = function (id) {
            endpoint = endpoint + '/' + id;
            return $resource(endpoint, params, {
                delete: {
                    method: 'DELETE'
                }
            }).delete().$promise;
        };

        this.count = function () {
            endpoint = getApiEndPoint('count' + capitalizeFirstLetter(model))
            return $resource(endpoint, params).get().$promise;
        };

        this.query = function (query, obj) {
            var pagData = queryBuilder.getPaginationData();
            var orderBy = prepareOrderBy();
            if (orderBy && orderBy !== '') {
                obj.orderBy = orderBy;
            }
            if (pagData) {
                obj.perPage = pagData.per_page;
                obj.page = pagData.current_page;
                $location.search('page', pagData.current_page);
                $location.search('perPage', pagData.per_page);
            } else {
                obj.page = $location.search()['page'];
                obj.perPage = $location.search()['perPage'];
            }
            if (query) {
                params.search = query;
            }
            angular.forEach(obj, function (value, key) {
                params[key] = value;
            });
            return $resource(endpoint, params).get().$promise;
        };

        this.export = function (query) {
            var def = $q.defer();
            var request = endpoint;
            if (query) {
                request = request + '?search=' + query;
            }
            $http.get(request, {responseType: 'arraybuffer'})
                .then(function (res) {
                    def.resolve(res.data);
                }, function (err) {
                    def.reject(err);
                });
            return def.promise;
        }
    };

    var factory = function (endpoint) {
        var model = endpoint;
        var url = getApiEndPoint(endpoint);
        return new ServiceForResource(url, model);
    };

    function capitalizeFirstLetter(string) {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }

    function getApiEndPoint(endpoint) {
        return constants.apiEndpoint + endpoint;
    }

    function setOrderBy(order) {
        vm.orderParams[order.property] = order.direction;
    }

    function prepareOrderBy() {
        var ret = [];
        angular.forEach(vm.orderParams, function (value, key) {
            if (value) {
                ret.push(key + ':' + value);
            }
        });
        return ret.join(';');
    }

    return {
        forResource: factory,
        setOrderBy: setOrderBy
    };

});

Zerion Mini Shell 1.0