コード例 #1
0
        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 async Task <decimal> Handle(GetCryptoCurrencyPriceQuery request, CancellationToken cancellationToken)
 {
     return(await _cryptoCurrencyService.GetLatestPrice(request.Symbol).ConfigureAwait(false));
 }