public async Task <IResult> Handler(AlterarProjetoCommand command) { command.Validate(); if (!command.Valid) { return(command.ValidationResult); } var projeto = await _repository.Projeto.GetProjetoByIdAsync(command.Id); if (projeto == null) { return(new CommandResult(false, "Projeto não encontrado", command.Notifications)); } projeto = await _repository.Projeto.GetProjetoByNameAndDiferentIdAsync(command.Nome, command.Id); if (projeto != null) { return(new CommandResult(false, "Já existe um projeto com mesmo nome cadastrado", command.Notifications)); } var projetoEntity = _mapper.Map <Projeto>(command); _repository.Projeto.Update(projetoEntity); await _repository.SaveAsync(); return(new CommandResult("Dados alterados com sucesso!")); }
public async Task ShouldInvalidateAlterarProjetoCommandWhenAlreadyProjectWithSameName() { var projeto = new Projeto { Id = 1, Nome = "Teste", Status = StatusDeCadastro.Normal }; var projetoJaCadastrado = new Projeto { Id = 2, Nome = "Teste", Status = StatusDeCadastro.Normal }; _repositoryMock.Setup(r => r.Projeto.GetProjetoByIdAsync(1)).Returns(Task.FromResult(projeto)); _repositoryMock.Setup(r => r.Projeto.GetProjetoByNameAndDiferentIdAsync("Teste", 1)).Returns(Task.FromResult(projetoJaCadastrado)); var command = new AlterarProjetoCommand(1, "Teste", StatusDeCadastro.Normal); var result = await _projetoHandler.Handler(command); Assert.AreEqual("Já existe um projeto com mesmo nome cadastrado", ((CommandResult)result).Message); }
public void ShouldInvalidateAlterarProjetoCommandWhenNameIsLessThan3() { var _alterarProjetoCommand = new AlterarProjetoCommand(1, "as", StatusDeCadastro.Normal); _alterarProjetoCommand.Validate(); var result = _alterarProjetoCommand.Notifications.ToList(); Assert.AreEqual(result[0].Message, "Nome deve ter no mínimo 3 caracteres."); }
public void ShouldInvalidateAlterarProjetoCommandWhenNameIsGreaterThan60() { var _alterarProjetoCommand = new AlterarProjetoCommand(1, "dhasghghghaghdgashdghgdhjghasgdjhagddhasghghghaghdgashdghgdhjghasgdjhagd", StatusDeCadastro.Normal); _alterarProjetoCommand.Validate(); var result = _alterarProjetoCommand.Notifications.ToList(); Assert.AreEqual(result[0].Message, "Nome não pode ser maior que 60 caracteres."); }
public void ShouldInvalidateAlterarProjetoCommandWhenNameIsEmpty() { var _alterarProjetoCommand = new AlterarProjetoCommand(1, "", StatusDeCadastro.Normal); _alterarProjetoCommand.Validate(); var result = _alterarProjetoCommand.Notifications.ToList(); Assert.AreEqual(result[0].Message, "Nome é obrigatório"); }
public void ShouldInvalidateAlterarProjetoCommandWhenIdIsZero() { var _alterarProjetoCommand = new AlterarProjetoCommand(0, "teste", StatusDeCadastro.Normal); _alterarProjetoCommand.Validate(); var result = _alterarProjetoCommand.Notifications.ToList(); Assert.AreEqual(result[0].Message, "Id do projeto inválido"); }
public async Task ShouldInvalidateAlterarProjetoCommandWhenNotFoundProjectById() { _repositoryMock.Setup(r => r.Projeto.GetProjetoByIdAsync(1)); var command = new AlterarProjetoCommand(1, "Teste", StatusDeCadastro.Normal); var result = await _projetoHandler.Handler(command); Assert.AreEqual("Projeto não encontrado", ((CommandResult)result).Message); }
public async Task ShouldUpdateProjectWithAlterarProjetoCommandValid() { var projeto = new Projeto { Id = 1, Nome = "Teste", Status = StatusDeCadastro.Normal }; _repositoryMock.Setup(r => r.Projeto.GetProjetoByIdAsync(1)).Returns(Task.FromResult(projeto)); _repositoryMock.Setup(r => r.Projeto.Update(projeto)); _repositoryMock.Setup(r => r.SaveAsync()); var command = new AlterarProjetoCommand(1, "Teste", StatusDeCadastro.Normal); var result = await _projetoHandler.Handler(command); Assert.AreEqual("Dados alterados com sucesso!", ((CommandResult)result).Message); }