public ICommandResult Handle(UsuarioReadCommand command, IServiceProvider service)
        {
            //Regras e Fluxo
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                var message = "Não foi possível consultar o registro. \n";
                foreach (var notification in command.Notifications)
                {
                    message += $"{notification.Property} - {notification.Message}" + "\n";
                }

                return(new CommandResult <bool>(false, message));
            }

            var usuarioRepository = (IUsuarioRepository)service.GetService(typeof(IUsuarioRepository));
            var usuario           = usuarioRepository.Read(command.Nome);

            if (usuario == null)
            {
                return(new CommandResult <UsuarioReadOut>(false, "Registro não encontrado."));
            }

            var result = new CommandResult <UsuarioReadOut>(true, string.Empty);

            result.Data = new UsuarioReadOut()
            {
                Id = usuario.Id, Nome = usuario.Nome, Email = usuario.Email, Senha = usuario.Senha
            };
            return(result);
        }
예제 #2
0
 public ActionResult Read(string nome)
 {
     try
     {
         var handler = new UsuarioHandler();
         var command = new UsuarioReadCommand()
         {
             Nome = nome
         };
         var result = (CommandResult <UsuarioReadOut>)handler.Handle(command, Service);
         return(Ok(result));
     }
     catch (ArgumentNullException e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
     catch (Exception e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
 }
예제 #3
0
        public void ReadSucesso()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioReadCommand()
            {
                Nome = "UsuarioExistente"
            };
            var result = (CommandResult <UsuarioReadOut>)handler.Handle(command, Service);

            Assert.AreEqual(true, result.Success);
        }
예제 #4
0
        public void ReadRegistroInexistenteErro()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioReadCommand()
            {
                Nome = "UsuarioInexistente"
            };
            var result = (CommandResult <UsuarioReadOut>)handler.Handle(command, Service);

            Assert.AreEqual(false, result.Success);
        }