public void Can_Get_Rates_From_Nbrb() { var client = new NBRBServiceClient(); var rates = client.GetLatestRates(); client.Dispose(); Assert.IsNotNull(rates); Assert.IsTrue(rates.Count > 0); }
public void UpdateRates() { var nationalBankClient = new NBRBServiceClient(); try { var now = DateTime.UtcNow; var currencies = _deps.Currencies.FindAll(); var nationalCurrency = currencies.First(x => x.ISOName == _settings.NationalCurrency); var foreignCurrencies = currencies.Except(new[] {nationalCurrency}).ToList(); var nationalBankRates = nationalBankClient.GetLatestRates() .Where(x => foreignCurrencies.Any(f => f.ISOName == x.Conversion.To)) .ToList(); var commercialRates = nationalBankRates .Select(x => _deps.ExchangeRateCalculator.CalculateFromNationalBankRate(x.Conversion.To, x.Rate, now)) .ToDictionary(x => x.Foreign.ISOName, x => x); var foreignCurrenciesIds = foreignCurrencies.Select(x => x.ISOName).ToList(); var pairs = from id1 in foreignCurrenciesIds from id2 in foreignCurrenciesIds select new {id1, id2}; var crossConversions = pairs.Where(x => x.id1 != x.id2) .Select(x => new CurrencyConversion(x.id1, x.id2)) .OrderBy(x => _settings.GetPriority(x.From)) .DistinctBy(x => x.ExchangeRateKey) .ToList(); var crossRates = crossConversions .Select(x => _deps.ExchangeRateCalculator.CalculateCrossRate(commercialRates[x.From],commercialRates[x.To], now)) .ToList(); var allRates = commercialRates.Values.Concat(crossRates).ToList(); allRates.ForEach(_deps.ExchangeRates.Save); Commit(); } catch (Exception ex) { throw new ServiceException("Can't update currency rates.", ex); } finally { nationalBankClient.Dispose(); } }