public async Task <Dictionary <string, decimal> > Handle(GetCryptoCurrencyExchangeRatesQuery request,
                                                                 CancellationToken cancellationToken)
        {
            IConfigurationSection currencySection = _configuration.GetSection("AvailableCurrencies");
            var symbols = currencySection.AsEnumerable()
                          .Where(c => !string.IsNullOrEmpty(c.Value))
                          .Select(c => c.Value);

            var usdPrice = await _cryptoCurrencyService.GetLatestPrice(request.Symbol).ConfigureAwait(false);

            var exchangeRate = await _exchangeRateService.GetLatestRate(string.Join(",", symbols))
                               .ConfigureAwait(false);

            var prices = new Dictionary <string, decimal>();

            // calculate price based on currency rate
            foreach (var symbol in symbols)
            {
                if (symbol.Equals("usd", StringComparison.InvariantCultureIgnoreCase))
                {
                    prices.Add("USD", usdPrice);
                }
                else
                {
                    var priceWithRate = usdPrice * exchangeRate.Rates[symbol];
                    prices.Add(symbol, priceWithRate);
                }
            }


            return(prices);
        }
예제 #2
0
        public override Task DoWork(CancellationToken cancellationToken)
        {
            IConfigurationSection currencySection = _configuration.GetSection("AvailableCurrencies");
            var symbols = currencySection.AsEnumerable()
                          .Where(c => !string.IsNullOrEmpty(c.Value))
                          .Select(c => c.Value);

            return(_exchangeRateService.GetLatestRate(string.Join(",", symbols)));
        }
예제 #3
0
 public async Task <ExchangeRate> Handle(GetExchangeRatesQuery request, CancellationToken cancellationToken)
 {
     return(await _exchangeRateService.GetLatestRate(string.Join(",", request.symbols)).ConfigureAwait(false));
 }