public async Task <IEnumerable <Rate> > GetRateByFilterAsync(RateFilterModel filterModel) { if (filterModel.DateTo < filterModel.DateFrom || filterModel.DateFrom.AddMonths(2) < filterModel.DateTo) { throw new ArgumentException("Invalid filter model. Date range is invalid."); } var baseIsAvailable = await _ratesRepository.CheckAvailableBaseCurrencyAsync(filterModel.BaseCurrency.Id); var resultsIsAvailable = await _ratesRepository.CheckAvailableResultCurrenciesAsync( filterModel.ResultCurrencyList.Select(x => x.Id)); if (!baseIsAvailable || !resultsIsAvailable) { throw new ArgumentException("Invalid filter model. Check your currency accessing."); } var cachingRates = await _ratesRepository.GetRatesInfoAsync(filterModel); if (cachingRates.Count() == filterModel.ResultCurrencyList.Count() * filterModel.DateFrom.GetBusinessDaysCount(filterModel.DateTo)) { return(cachingRates); } else { var newRates = await GetMissingRates(filterModel, cachingRates); await _ratesRepository.AddRangeRatesInfoAsync(newRates); var result = cachingRates.ToList(); result.AddRange(newRates); return(result.GroupBy(x => new { x.ResultCurrencyId, x.RateDate }).Select(x => x.First())); } }