private IEnumerable <CurrencyResponseModel> CreateModel(
            DateTime startDate, DateTime endDate, IReadOnlyCollection <Currency> currencies)
        {
            var result = new List <CurrencyResponseModel>();

            while (startDate < endDate)
            {
                var date             = new DateTime(startDate.Year, startDate.Month, 1);
                var currenciesByDate = currencies.Where(e =>
                                                        date <= e.Date &&
                                                        e.Date <= date.AddDays(DateTime.DaysInMonth(date.Year, date.Month))
                                                        ).ToArray();

                var itemModel = new CurrencyResponseModel
                {
                    Year        = startDate.Year,
                    Month       = startDate.Month,
                    WeekPeriods = CreateWeekPeriods(startDate.Year, startDate.Month)
                };

                foreach (var currencyWeekPeriodModel in itemModel.WeekPeriods)
                {
                    var startWeekDate = new DateTime(
                        startDate.Year, startDate.Month, currencyWeekPeriodModel.StartDay);

                    var endWeekDate = new DateTime(
                        startDate.Year, startDate.Month, currencyWeekPeriodModel.EndDay);

                    var currenciesInPeriod = currenciesByDate
                                             .Where(e => startWeekDate <= e.Date && e.Date <= endWeekDate)
                                             .GroupBy(e => e.Code)
                                             .ToDictionary(e => e.Key, v => v.ToArray());

                    foreach (var code in _parameters.CurrenciesCodes)
                    {
                        var currenciesByCode = currenciesInPeriod.ContainsKey(code)
                            ? currenciesInPeriod[code]
                            : new Currency[0];

                        var collection = new CurrencyCollection();
                        collection.AddRange(currenciesByCode);

                        var model = new CurrencyResponseValueModel
                        {
                            Max    = collection.Max(),
                            Min    = collection.Min(),
                            Median = collection.Median(),
                            Code   = code
                        };
                        currencyWeekPeriodModel.Currencies.Add(model);
                    }
                }

                result.Add(itemModel);
                startDate = startDate.AddMonths(1);
            }

            return(result.OrderBy(e => e.Year).ThenBy(e => e.Month));
        }
Exemple #2
0
        public void MedianCurrencyTest()
        {
            var currencyCollection = new CurrencyCollection();

            var currency1 = _currencyFactory.Create("USD", 1, 100, DateTime.Today);
            var currency2 = _currencyFactory.Create("USD", 1, 200, DateTime.Today);
            var currency3 = _currencyFactory.Create("USD", 1, 300, DateTime.Today);

            currencyCollection.Add(currency1);
            currencyCollection.Add(currency2);
            currencyCollection.Add(currency3);

            var median = currencyCollection.Median();

            Assert.True(currencyCollection.Count == 3, "CurrencyCollection len <> 3");
            Assert.True(median == 200.0m, $"{median} <> {200.0m}");
        }