matheusmurta
5/11/2018 - 5:38 PM

Service

Service

angular.module('meuApp').controller('userCtrl', function ({blabla..}, userService) {  

  {...}
 
    //Exemplo
    userService.List().then(function (result) {
        vm.list = result;
    });
angular.module('meuApp').factory("userService", function (API_URL, $http) {

  {...}

    userService.List = function () {
        var promise = $http({
            method: 'GET',
            url: API_URL.url + '/api/v1/usuario'
        })
            .then(function (response) {
                return response.data.content;
            },
            function (response) {
                //error action
            });

        return promise;
    };

    userService.getUser = function (id) {
        var promise = $http({
            method: 'GET',
            url: API_URL.url + '/api/v1/usuario/' + id
        })
            .then(function (response) {
                return response.data.content;
            },  
            function (response) {
                //error action
            });
        return promise;
    };

    userService.New = function (userData) {
       
        var promise = $http({
            method: 'POST',
            url: API_URL.url + '/api/v1/usuario',
            data: userData

        })
            .then(function (response) {
                return response.data;
            },
            function (response) {
                 //error action
            });

        return promise;
    };  

    userService.Edit = function (userData, id) {
        var promise = $http({
            method: 'PUT',
            url: API_URL.url + '/api/v1/usuario/' + id,
            data: userData
        })
            .then(function (response) {
                return response.data;
            },
            function (response) {
                //error action
            });
        return promise;
    };

    userService.Delete = function (id) {
        var promise = $http({
            method: 'DELETE',
            url: API_URL.url + '/api/v1/usuario/' + id
        })
            .then(function (response) {
                return response.data;
            },
            function (response) {
                //error action
            });
        return promise;
    };

    return userService;
});