/// <summary> /// <para>Method that parse csv string to IEnumerable of euro exchanges</para> /// If header (first line where are column definitions) is not exactly as CSV_HEADERS method returns an empty array /// </summary> /// <param name="source">source string(csv)</param> /// <returns>IEnumerable of euro exchanges</returns> private IEnumerable <EuroExchange> parseCsvData(string source) { var lines = source.Replace("\r", "").Split('\n'); // check if first line is correct if (lines.FirstOrDefault() == CSV_HEADERS) { // ignore first line (headers) foreach (var line in lines.Skip(1)) { var fields = line.Split(','); var currency = fields.ElementAtOrDefault(CurrencyIndex); var timePeriod = fields.ElementAtOrDefault(TimePeriodIndex);; var obsValue = fields.ElementAtOrDefault(ObsValuevIndex)?.Replace('.', ','); if (string.IsNullOrEmpty(currency) == true || string.IsNullOrEmpty(timePeriod) == true || string.IsNullOrEmpty(obsValue) == true) { continue; } var currencyExchange = new EuroExchange() { Date = DateTime.Parse(timePeriod), Currency = currency, ExchangeRate = Convert.ToDouble(obsValue) }; yield return(currencyExchange); } } }
/// <summary> /// Method that converts currencies by using their euro rates /// </summary> /// <param name="from">EuroExchange with base currency</param> /// <param name="to">EuroExchange with target currency</param> /// <param name="date">exchange date</param> public static CurrencyExchangeDTO Create( EuroExchange from, EuroExchange to, DateTime date) { return(new CurrencyExchangeDTO() { CurrencyFrom = from.Currency, CurrencyTo = to.Currency, Date = date.ToString("yyyy-MM-dd"), ExchangeRate = Math.Round(1 / from.ExchangeRate * to.ExchangeRate, 4) }); }
/// <summary> /// Method that generates currency exchanges from currency codes list and euro rates /// </summary> /// <param name="currencyCodes">requested codes to be exchanged</param> /// <param name="euroExchangesPerDay">euro exchanges to all currencies</param> /// <param name="startDate">first day</param> /// <param name="endDate">last day</param> /// <returns>generated IEnumerable of CurrencyExchanges</returns> private IEnumerable <CurrencyExchangeDTO> generateCurrencyExchanges( List <KeyValuePair <string, string> > currencyCodes, Dictionary <DateTime, List <EuroExchange> > euroExchangesPerDay, DateTime startDate, DateTime endDate) { // set euro var euro = new EuroExchange() { Currency = "EUR", ExchangeRate = 1 }; // get date of first usefull element in given euroexchanges var firstCurrencyDate = euroExchangesPerDay .Where(e => e.Key <= startDate) .Select(e => e.Key) .LastOrDefault(); // get enumerators var currentEnumerator = euroExchangesPerDay.GetEnumerator(); var nextEnumerator = euroExchangesPerDay.Skip(1).GetEnumerator(); var canMoveToNext = true; var bankingHolidays = new List <BankingHoliday>(); // generate currency-currency exchange rates var currentDay = substractWorkingDaysFromDate(startDate, 3); while (currentDay <= endDate) { // check if next enumerator cover next day if (canMoveToNext && currentDay >= nextEnumerator.Current.Key) { currentEnumerator.MoveNext(); canMoveToNext = nextEnumerator.MoveNext(); } if (currentDay != currentEnumerator.Current.Key && currentDay.DayOfWeek != DayOfWeek.Saturday && currentDay.DayOfWeek != DayOfWeek.Sunday && currentDay.Date < DateTime.Now) { bankingHolidays.Add(new BankingHoliday(currentDay)); } if (currentDay >= startDate) { foreach (var codePair in currencyCodes) { var fromEuroRate = codePair.Key == "EUR" ? euro : currentEnumerator.Current.Value?.FirstOrDefault(e => e.Currency == codePair.Key); var toEuroRate = codePair.Value == "EUR" ? euro : currentEnumerator.Current.Value?.FirstOrDefault(e => e.Currency == codePair.Value); if (fromEuroRate != null && toEuroRate != null) { yield return(CurrencyExchangeDTO.Create(fromEuroRate, toEuroRate, currentDay)); } } } currentDay = currentDay.AddDays(1); } _dataCachingService.StoreBankingHolidays(bankingHolidays); }