public async Task<CommandResult> AddAsync(string countryIsoCode, int year) { CommandResult commandResult; Stopwatch watch = Stopwatch.StartNew(); try { var rulesHolidays = (await RuleHolidayRepository.FindAsync(r => r.CountryIsoCode == countryIsoCode)).ToList(); if (rulesHolidays == null || rulesHolidays.Count() == 0) { commandResult = CommandResult.BadRequest($"Não existe feriados cadastrados para {countryIsoCode}."); } else { var holidays = await Task.Run(() => rulesHolidays.ToHolidayList(GetNotificationHandler(), year, GetUserLogged())); bool validateRepository = await MustBeValidateAsync(countryIsoCode, year); commandResult = await AddRangeAsync(holidays, validateRepository); } } catch (Exception ex) { commandResult = CommandResult.InternalServerError($"Ocorreu um erro ao salvar."); } watch.Stop(); commandResult.ElapsedTime = watch.ElapsedMilliseconds; return commandResult; }
public async Task <CommandResult> AddAsync(RuleHoliday ruleHoliday) { CommandResult commandResult; Stopwatch watch = Stopwatch.StartNew(); try { if (!await CanAddAsync(ruleHoliday, true)) { commandResult = CommandResult.BadRequest("Registro não pode ser salvo, existem erros."); } else { ruleHoliday = await RuleHolidayRepository.AddAsync(ruleHoliday); commandResult = await CommitAsync(_commandName, ruleHoliday.Action); if (commandResult.Success) { commandResult.Data = ruleHoliday; } } } catch (Exception ex) { commandResult = CommandResult.InternalServerError($"Ocorreu um erro ao salvar."); } watch.Stop(); commandResult.ElapsedTime = watch.ElapsedMilliseconds; return(commandResult); }
public async Task <CommandResult> AddRangeAsync(List <RuleHoliday> listOfRuleHolidays, bool validateRepository = true) { CommandResult commandResult; Stopwatch watch = Stopwatch.StartNew(); try { int recordsToSave = Count(listOfRuleHolidays); if (recordsToSave == 0) { commandResult = CommandResult.BadRequest("Nenhum registro salvo, a lista está vazia."); } else { if (!await CanAddAsync(listOfRuleHolidays, validateRepository)) { commandResult = CommandResult.BadRequest("Nenhum registro salvo, existem erros."); } else { await RuleHolidayRepository.AddRangeAsync(listOfRuleHolidays); 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 = listOfRuleHolidays; } } } } catch (Exception ex) { commandResult = CommandResult.InternalServerError($"Ocorreu um erro ao salvar."); } watch.Stop(); commandResult.ElapsedTime = watch.ElapsedMilliseconds; return(commandResult); }
private async Task <bool> MustBeValidateAsync(RuleHoliday ruleHoliday) { return((await RuleHolidayRepository.GetCountAsync(c => c.CountryIsoCode == ruleHoliday.CountryIsoCode)) > 0); }