public void InValidSearchModelWrongDates() { Dictionary <string, string> codes = new Dictionary <string, string>(); codes.Add("PLN", "JPY"); codes.Add("USD", "EUR"); var searchModel = new ExchangeRateSearcher() { currencyCodes = codes, startDate = DateTime.Now.AddDays(5), endDate = DateTime.Now, }; var searchModel2 = new ExchangeRateSearcher() { currencyCodes = codes, startDate = DateTime.Now, endDate = DateTime.Now.AddDays(-5), }; ValidationResult result = searchModelValidator.Validate(searchModel); ValidationResult result2 = searchModelValidator.Validate(searchModel2); Assert.False(result.IsValid); Assert.False(result2.IsValid); }
private string SearchModelToCacheKey(ExchangeRateSearcher s) { string cacheKey = s.startDate.ToString() + s.endDate.ToString(); foreach (var item in s.currencyCodes) { cacheKey = cacheKey + item.Key + item.Value; } return(cacheKey); }
public async Task <IActionResult> GetExchangeRatesAsync(DateTime startDate, DateTime endDate, [FromQuery] Dictionary <string, string> currencyCodes) { _logger.LogInformation("HttpGet: GetExchangeRatesAsync started"); var searchModel = new ExchangeRateSearcher() { currencyCodes = currencyCodes, endDate = endDate, startDate = startDate }; var result = await _service.GetExchangeRatesVMAsync(searchModel); _logger.LogInformation("HttpGet: returned view model"); return(Ok(result)); }
private void SearchModelValidator(ExchangeRateSearcher searchModel) { var result = _validator.Validate(searchModel); if (!result.IsValid) { var errors = new List <string>(); foreach (var err in result.Errors) { errors.Add(err.ErrorMessage); } throw new ExchangeRateException(System.Net.HttpStatusCode.BadRequest, errors, "Search model is not valid"); } if (searchModel.startDate > DateTime.Now || searchModel.endDate < new DateTime(1999, 1, 4) || searchModel.startDate < new DateTime(1999, 1, 4)) { throw new ExchangeRateException(System.Net.HttpStatusCode.NotFound, "Not Found :( use any date range between 1999.1.4 and current time"); } }
public async Task <List <ExchangeRatesVM> > GetExchangeRatesVMAsync(ExchangeRateSearcher searchModel) { SearchModelValidator(searchModel); var checkList = new List <ExchangeRatesVM>(); string cacheKey = SearchModelToCacheKey(searchModel); if (_cache.TryGetValue(cacheKey, out checkList)) { checkList = _cache.Get <List <ExchangeRatesVM> >(cacheKey); _logger.LogInformation("response from cache"); return(checkList); } var list = new List <ExchangeRatesVM>(); var codes = RequiredCurrencyCodesWithNoEuro(searchModel.currencyCodes); var exrDayByDay = await _ecbService.GetExchangeRatesDayByDayAsync(searchModel.startDate, searchModel.endDate); var dailyRates = FilteredExchangeRatesByDay(codes, exrDayByDay, searchModel.startDate, searchModel.endDate); var earlierRates = await CheckFirstRequestedDay(dailyRates, searchModel.currencyCodes, searchModel.startDate); if (earlierRates.Count() > 0) { dailyRates = AddAdditionalExchangeRatesByDay(earlierRates, dailyRates).OrderBy(x => x.DateTime).ToList(); } foreach (var item in dailyRates) { var exrVM = new ExchangeRatesVM() { Date = item.DateTime, Rates = new List <RatesVM>() }; foreach (var pair in searchModel.currencyCodes) { var k = item.CurrencyRates.FirstOrDefault(x => x.CurrencyCode == pair.Key); var v = item.CurrencyRates.FirstOrDefault(x => x.CurrencyCode == pair.Value); if ((k != null && v != null) || (pair.Key == "EUR" && v != null) || (k != null && pair.Value == "EUR")) { var rateVM = new RatesVM() { SourceCurrency = pair.Key, TargetCurrency = pair.Value }; if (pair.Key == "EUR") { rateVM.Rate = v.Rate / 1; } else if (pair.Value == "EUR") { rateVM.Rate = 1 / k.Rate; } else { rateVM.Rate = v.Rate / k.Rate; } exrVM.Rates.Add(rateVM); } } if (exrVM.Rates.Count() > 0) { list.Add(exrVM); } } var cacheEntryOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(10)); _cache.Set(cacheKey, list, cacheEntryOptions); return(list); }