コード例 #1
0
        public async Task <CommandResult> AddRangeAsync(List <State> listOfStates, bool validateRepository = true)
        {
            CommandResult commandResult;
            Stopwatch     watch = Stopwatch.StartNew();

            try
            {
                int recordsToSave = Count(listOfStates);
                if (recordsToSave == 0)
                {
                    commandResult = CommandResult.BadRequest("Nenhum registro salvo, a lista está vazia.");
                }
                else
                {
                    if (!await CanAddAsync(listOfStates, validateRepository))
                    {
                        commandResult = CommandResult.BadRequest("Nenhum registro salvo, existem erros.");
                    }
                    else
                    {
                        await StateRepository.AddRangeAsync(listOfStates);

                        commandResult = await CommitAsync(_commandName, ActionType.Register);

                        if (commandResult.Success)
                        {
                            commandResult.Message = $"Ação concluída com sucesso. Salvos { recordsToSave } registros de um total de { recordsToSave }";
                            commandResult.Data    = listOfStates;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                commandResult = CommandResult.InternalServerError($"Ocorreu um erro ao salvar.");
            }

            watch.Stop();
            commandResult.ElapsedTime = watch.ElapsedMilliseconds;

            return(commandResult);
        }