public CommandResult Atualizar(AtualizarRotaCommand command)
        {
            string entityName  = "Rota";
            string commandName = $"Atualizando {entityName}";

            try
            {
                command.Validate();
                if (command.Invalid)
                {
                    return(CommandResult.Invalid(command.Notifications.ToNotificationsString()));
                }

                Rota rota = dataContext.Rota.FirstOrDefault(x => x.Cod_Rota == command.Cod_Rota);

                if (rota is null)
                {
                    return(CommandResult.Invalid(Logs.EntidadeNaoEncontrada(entityName, command.Cod_Rota)));
                }

                if (dataContext.Rota.Any(x => x.Nome == command.Nome.ToUpper() && x.Cod_Rota != command.Cod_Rota))
                {
                    string message = "Já existe uma Rota com este nome.";
                    return(CommandResult.Invalid(message));
                }

                rota.Atualizar(DataString.FromString(command.Nome),
                               DataString.FromNullableString(command.Composicao_Rota),
                               DataString.FromNullableString(command.Observacao),
                               command.Flag_Ativo);


                dataContext.SaveChanges();
                return(CommandResult.Valid());
            }
            catch (Exception ex)
            {
                return(CommandResult.Invalid(ex.Message));
            }
        }
Beispiel #2
0
 public IActionResult Atualizar(
     [FromServices] IRotaService service,
     [FromBody] AtualizarRotaCommand command)
 {
     return(Result(service.Atualizar(command)));
 }