public async Task <ServiceResponse <CurrencyRateResponse> > CurrencyToRate(CurrencyEnum baseCurrency, CurrencyEnum versusCurrency) { CurrenciesContainer container = new CurrenciesContainer(); try { container = await GetCurrencies(); } catch (HttpRequestException ex) { return(new ServiceResponse <CurrencyRateResponse> { Success = false, Message = @"A problem was found in an external HTTP request to https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml, try again" }); } var data = new CurrencyRateResponse(); data.Base = baseCurrency.ToString(); data.Versus = versusCurrency.ToString(); data.Date = container.Date; var currency1 = container.Currencies.FirstOrDefault(x => x.Name == baseCurrency.ToString()); var currency2 = container.Currencies.FirstOrDefault(x => x.Name == versusCurrency.ToString()); data.Rate = ConvertCurrency(1M, currency1, currency2); return(new ServiceResponse <CurrencyRateResponse> { Success = true, Data = data }); }
/// <summary> /// Get currency rate for a given currency. It tries to read rate from memory for a day. If not found it reads currency from API /// </summary> /// <param name="currencyEnum">Input currency code</param> /// <returns>Currency rate</returns> public async Task <float> GetRate(CurrencyEnum currencyEnum) { var currency = currencyEnum.ToString(); var key = $"{DateTime.Today.ToShortDateString()}"; Dictionary <string, float> rates; _memoryCache.TryGetValue(key, out rates); if (rates == null) { var rateResponse = await _currencyService.GetRates(); _memoryCache.Set(key, rateResponse); rates = rateResponse; } if (rates.ContainsKey(currency)) { return(rates[currency]); } else { _logger.LogError($"Not able to get currency rate. Currency code {currency} not found"); throw new GalvException("Not able to get currency rate. Currency code not found"); } }
public async Task <ServiceResponse <CurrencyRatesResponse> > CurrencyToRates(CurrencyEnum baseCurrency) { CurrenciesContainer container = new CurrenciesContainer(); try { container = await GetCurrencies(); } catch (HttpRequestException ex) { return(new ServiceResponse <CurrencyRatesResponse> { Success = false, Message = @"A problem was found in an external HTTP request to https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml, try again" }); } var data = new CurrencyRatesResponse(); data.Base = baseCurrency.ToString(); data.Date = container.Date; var currency1 = container.Currencies.FirstOrDefault(x => x.Name == baseCurrency.ToString()); foreach (var item in Enum.GetValues(typeof(CurrencyEnum))) { var currency2 = container.Currencies.FirstOrDefault(x => x.Name == item.ToString() && x.Name != baseCurrency.ToString() ); if (currency2 != null) { var convertResult = ConvertCurrency(1M, currency1, currency2); data.Rates.Add(currency2.Name, convertResult); } } return(new ServiceResponse <CurrencyRatesResponse> { Success = true, Data = data }); }
public List <Transaction> GetByCurrency(CurrencyEnum currency) { return(_dbContext.Transactions.Where(t => t.Currency == currency.ToString()).ToList()); }
} //For EF private Currencies(CurrencyEnum @enum) { this.Id = (int)@enum; this.CurrencySign = @enum.ToString(); this.CurrencyFullName = @enum.GetEnumDescription(); }
private Currency(CurrencyEnum @enum) { ID = (int)@enum; Name = @enum.ToString(); Description = @enum.GetEnumDescription(); }