Exemplo n.º 1
0
        public async Task Validar_comando_correto()
        {
            var command = new CreateServerCommand("teste", "127.0.0.1", 80);

            var result = await _validator.ValidateAsync(command);

            result.IsValid.Should().BeTrue();
        }
Exemplo n.º 2
0
        public async Task <CreateServerCommandResponse> Handle(CreateServerCommand request, CancellationToken cancellationToken)
        {
            var validationResult = await _validator.ValidateAsync(request);

            if (!validationResult.IsValid)
            {
                var errors = validationResult.Errors
                             .Select(x => x.ErrorMessage);

                return(new CreateServerCommandResponse(null, errors));
            }

            _logger.LogInformation("Adicionando novo servidor {0}", request.Name);

            var newServer = new Server(request.Name, request.IP, request.Port);

            var createdServer = await _serverRepository.AddAsync(newServer);

            await _serverRepository.SaveChangesAsync();

            _logger.LogInformation("Servidor {0} criado.", createdServer.Name);

            return(new CreateServerCommandResponse(createdServer, null));
        }