Ejemplo n.º 1
0
            /// <summary>
            /// Swapping the Base to Transaction's currency code and convert exchange rates
            /// </summary>
            /// <param name="currencyCode"></param>
            /// <returns></returns>
            public ExchangeRatesApiResponse SwapBase(string currencyCode)
            {
                //TODO: Лучше бы эту логику вынести в отдельный класс, чтобы отделить
                //      ExchangeRatesApiResponse от класса который содердит в себе значения курсов валют
                var exchangeRatesApiResponse = new ExchangeRatesApiResponse(Success, Timestamp, Base, Date, Rates);

                exchangeRatesApiResponse.ConvertExchangeRates(currencyCode);

                return(exchangeRatesApiResponse);
            }
Ejemplo n.º 2
0
        private static decimal GetRate(string currency, ExchangeRatesApiResponse response)
        {
            var currencyUpper = currency.ToUpperInvariant();

            if (currencyUpper == "EUR")
            {
                return(1.0M);
            }

            if (!response.Rates.ContainsKey(currencyUpper))
            {
                throw new CurrencyNotSupportedException(currencyUpper);
            }

            return(response.Rates[currencyUpper]);
        }
        private CalculatedRatesDateRateResponse CreateCalculatedRatesResponse(ExchangeRatesApiResponse response,
                                                                              List <DateTime> dates, string targetCurrency)
        {
            if (response.Rates.Count > 0)
            {
                var converted = new CalculatedRatesDateRateResponse();
                // dictionary of user entered dates and their corresponding days with exchange rate values
                Dictionary <string, string> datesDictionary = new Dictionary <string, string>();
                dates.ForEach(date =>
                {
                    DateTime dateMoved = DateTimeUtil.ChangeDateForSaturdayOrSunday(date);
                    datesDictionary.Add(DateTimeUtil.DateTimeToDateString(date), DateTimeUtil.DateTimeToDateString(dateMoved));
                });

                // filter rates only for user entered days
                // maps response to <string, decimal> dictionary
                var rates = response.Rates.Where(p => datesDictionary.Any(d =>
                {
                    return(d.Key == p.Key || d.Value == p.Key);
                }))
                            .ToDictionary(p => datesDictionary.FirstOrDefault(d => d.Value == p.Key).Key /*p.Key*/, p => (decimal)p.Value.Fields[targetCurrency])
                            .OrderBy(p => p.Value);

                var minRate = rates.First();
                var maxRate = rates.Last();

                var avg = rates.Select(c => c.Value).Sum() / rates.Count();

                converted.MinRate     = new Rate(minRate.Key, minRate.Value); // $"A min rate of {minRate.Value} on {minRate.Key}.";
                converted.MaxRate     = new Rate(maxRate.Key, maxRate.Value); // $"A max rate of {maxRate.Value} on {maxRate.Key}.";
                converted.AverageRate = avg;                                  //  $"An average rate of {avg}.";

                return(converted);
            }
            else
            {
                throw new EmptyRatesException();
            }
        }