angular.module('ngBoilerplate.message', ['ui.router', 'ngResource'])
.config(function($stateProvider) {

})
.factory('messageService', function($resource) {
    var service = {};
    service.getAllMessages = function() {
        console.log("getAllMessages");
          var Messages = $resource("/basic-web-app/rest/message");
          return Messages.get().$promise.then(function(data) {
            return data.messages;
          });
    };
    service.sendMessage = function(message, success, failure) {
        console.log("messageService");
        //var Message = $resource("/basic-web-app/rest/messages/"+$rootScope.user);
        //Message.save({}, message, success, failure);
        
        var newMessage = $resource("/basic-web-app/rest/message");
        message.ownerName = localStorage.getItem("name");
        newMessage.save({}, message, success, failure);
        
        /*$http.post("/basic-web-app/rest/message/"+ $rootScope.user,{
            headers:{'Content-Type':'application/json'},
            data: { 
                message: message
            }
        }
        ).then(function(data){
            alert("Mensaje mandado correctamente"); 
        }, function(data){
            alert("Error mandando el mensaje");
        });*/
    };
    
    return service;
});
