// Summary: // Manually converting and mapping the exchange rates from default base currency vaalue to the target currency. // // Parameters: // createConversionDto: // An instance to CreateConversionDto. // Return: // showConversionDto // An instance to ShowConversionDto private float FindExchangeRate(LatestExchangeRates latestExchangeRates, string toCurrency) { if (latestExchangeRates.Rates.TryGetValue(toCurrency, out float exchangeRate)) { return(exchangeRate); } return(0); }
// Summary: // Get the current exchange rate from the API repository and converts as ShowConversionDto. // // Parameters: // createConversionDto: // An instance to CreateConversionDto. // Return: // showConversionDto // An instance to ShowConversionDto private async Task <ShowConversionDto> CalculateValue(CreateConversionDto createConversionDto) { LatestExchangeRates latestExchangeRates = await _currencyRepository.GetLatestConversionRatesByCurrency("EUR"); ShowConversionDto showConversionDto = new ShowConversionDto(createConversionDto.FromCurrency, createConversionDto.ToCurrency, createConversionDto.Value); showConversionDto.ExchangeRate = CalculateExchangeRateValue(showConversionDto, latestExchangeRates); return(showConversionDto); }
// Summary: // Gets latest currency exchange rates for base currency {currencyCode} // // Parameters: // string currencyCode: // Currency codes like EUR, GBP, INR, etc,. // Return: // LatestExchangeRates // Typed response from the Http as LatestExchangeRates public async Task <LatestExchangeRates> GetLatestConversionRatesByCurrency(string currencyCode) { string apiUrl = $"/latest?base=EUR"; var responseMessage = await this._currencyApi.Call(apiUrl); LatestExchangeRates latestExchangeRates = JsonConvert.DeserializeObject <LatestExchangeRates>(responseMessage); return(latestExchangeRates); }
// Summary: // It internally converts the base currency to the targer currency since API only supports EUR // // Parameters: // latestExchangeRates: // An instance to LatestExchangeRates // string: // Currency code. // Return: // showExchangeRateDto // An instance to ShowExchangeRateDto private ShowExchangeRateDto ConvertAsBaseCurrency(LatestExchangeRates latestExchangeRates, string currencyCode) { ShowExchangeRateDto convertedRates = new ShowExchangeRateDto() { Base = currencyCode, Timestamp = latestExchangeRates.Timestamp, Date = latestExchangeRates.Date, Rates = new Dictionary <string, float>() }; if (latestExchangeRates.Base == currencyCode) { convertedRates.Rates = latestExchangeRates.Rates; return(convertedRates); } foreach (KeyValuePair <string, float> entry in latestExchangeRates.Rates.ToList()) { ShowConversionDto showConversionDto = new ShowConversionDto(currencyCode, entry.Key, 0); convertedRates.Rates.Add(entry.Key, CalculateExchangeRateValue(showConversionDto, latestExchangeRates)); } return(convertedRates); }
// Summary: // Manually converting and mapping the exchange rates from default base currency vaalue to the target currency. // // Parameters: // LatestExchangeRates: // An instance to LatestExchangeRates. // ShowConversionDto: // An instance to ShowConversionDto. // Return: // float // Converted final exchange rate private float CalculateExchangeRateValue(ShowConversionDto showConversionDto, LatestExchangeRates latestExchangeRates) { float fromVsBaseExchangeRate = showConversionDto.FromCurrency == latestExchangeRates.Base ? 1 : FindExchangeRate(latestExchangeRates, showConversionDto.FromCurrency); float baseVsToExchangeRate = showConversionDto.ToCurrency == latestExchangeRates.Base ? 1 : FindExchangeRate(latestExchangeRates, showConversionDto.ToCurrency); if (fromVsBaseExchangeRate > 0 && baseVsToExchangeRate > 0) { float finalExchangeRate = (1 / fromVsBaseExchangeRate) * baseVsToExchangeRate; return(finalExchangeRate); } throw new NotSupportedException($"Conversion from {showConversionDto.FromCurrency} to {showConversionDto.ToCurrency} is not supported at the moment"); }
// Summary: // Get latest exchange rates for a specific currency. By default it's EUR. // // Parameters: // string: // Currency code. // Return: // showExchangeRateDto // An instance to ShowExchangeRateDto public async Task <ShowExchangeRateDto> GetAllConversionRatesByCurrency(string currencyCode) { LatestExchangeRates latestExchangeRates = await _currencyRepository.GetLatestConversionRatesByCurrency("EUR"); return(ConvertAsBaseCurrency(latestExchangeRates, currencyCode));; }