Created
July 8, 2017 00:39
-
-
Save rafaelvicio/04fcbe21c30849d6604c0ed9c9e2a194 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Olá, | |
Queria ajuda para refatorar meu serviço AngularJS: | |
angular.module('usuarioServico', ['ngResource']) | |
.factory('recursoUsuario', function($resource) { | |
return $resource('/api/usuarios/:usuarioId', null, { | |
'update' : { | |
method: 'PUT' | |
} | |
}); | |
}) | |
.factory("cadastroDeUsuarios", function(recursoUsuario, $q) { | |
var service = {}; | |
service.cadastrar = function(usuario) { | |
return $q(function(resolve, reject) { | |
if(usuario._id) { | |
recursoUsuario.update({usuarioId: usuario._id}, usuario, function() { | |
resolve({ | |
mensagem: 'Usuario ' + usuario.login + ' atualizada com sucesso', | |
inclusao: false | |
}); | |
}, function(erro) { | |
console.log(erro); | |
reject({ | |
mensagem: 'Não foi possível atualizar o usuario ' + usuario.login | |
}); | |
}); | |
} else { | |
recursoUsuario.save(usuario, function() { | |
console.log('não tem id'); | |
resolve({ | |
mensagem: 'Usuario ' + usuario.login + ' incluída com sucesso', | |
inclusao: true | |
}); | |
}, function(erro) { | |
console.log(erro); | |
reject({ | |
mensagem: 'Não foi possível incluir a usuario ' + usuario.login | |
}); | |
}); | |
} | |
}); | |
}; | |
return service; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment