public async Task <IActionResult> Get([FromBody] CurrencyCollectionRequest collectionModel)
        {
            return(await OnActionAsync(async() =>
            {
                _tokenService.ValidateCurrentToken(collectionModel.ApiKey);

                _logger.LogInformation($"Executing CurrencyController.Get with values {collectionModel}");
                return Ok(await _currencyService.GetCurrencyResultsAsync(collectionModel));
            }));
        }
        public async Task WhenGetLargeAmountOfData_ThenCheckDifferenceNoCacheAndCachedData()
        {
            var resultStringBuilder = new StringBuilder();

            var currencyCollectionModel = new CurrencyCollectionRequest
            {
                CurrencyCodes = new Dictionary <string, string>
                {
                    { "PLN", "EUR" },
                },
                StartDate = new DateTime(2009, 1, 1),
                EndDate   = new DateTime(2019, 1, 1)
            };

            try
            {
                _currencyAutomatedTestHelper.CurrencyCollectionModel = currencyCollectionModel;

                Stopwatch stopwatch       = Stopwatch.StartNew();
                var       currencyResults = await _currencyAutomatedTestHelper.GetCurrencyResultsAsync();

                stopwatch.Stop();

                resultStringBuilder.AppendLine($"Elapsed time => {stopwatch.ElapsedMilliseconds} with no cached data with {currencyResults.ToList().Count()}");

                var expectedCount = currencyResults.Count();

                stopwatch       = Stopwatch.StartNew();
                currencyResults = await _currencyAutomatedTestHelper.GetCurrencyResultsAsync();

                stopwatch.Stop();

                resultStringBuilder.AppendLine($"Elapsed time => {stopwatch.ElapsedMilliseconds} with cached data with {currencyResults.ToList().Count()}");

                currencyResults.Count().Should().Be(expectedCount);

                Console.WriteLine(resultStringBuilder.ToString());
            }
            finally
            {
                await _currencyAutomatedTestHelper.ResetDatabase();
            }
        }
Пример #3
0
        /// <summary>
        /// Get currency information
        /// </summary>
        /// <param name="currencyCollectionModel"><see cref="CurrencyCollectionRequest"/></param>
        /// <returns>returns collection of <see cref="CurrencyResultResponse"/> items</returns>
        public async Task <IEnumerable <CurrencyResultResponse> > GetCurrencyResultsAsync(CurrencyCollectionRequest currencyCollectionModel)
        {
            var currencyResult = new List <CurrencyResultResponse>();
            var currencyModel  = new CurrencyModel();

            _dateChecker.ValidateDate(currencyCollectionModel.StartDate, currencyCollectionModel.EndDate);
            (DateTime StartDate, DateTime EndDate)dateItems = _dateChecker.SetCorrectDate(currencyCollectionModel.StartDate, currencyCollectionModel.EndDate);

            currencyModel.StartDate = dateItems.StartDate;
            currencyModel.EndDate   = dateItems.EndDate;

            foreach (KeyValuePair <string, string> currencyCode in currencyCollectionModel.CurrencyCodes)
            {
                currencyModel.CurrencyBeingMeasured = currencyCode.Key;
                currencyModel.CurrencyMatched       = currencyCode.Value;

                List <CurrencyResultResponse> currencyResults = _cacheDatabase.GetAsync(currencyModel).Select(c => new CurrencyResultResponse
                {
                    CurrencyBeingMeasured = c.Currency.CurrencyBeingMeasured,
                    CurrencyMatched       = c.Currency.CurrencyMatched,
                    CurrencyValue         = c.Value,
                    DailyDataOfCurrency   = c.DailyDataOfCurrency
                }).ToList();

                if (currencyResults.Count == 0)
                {
                    var xmlBody = await _currencyGetterService.FetchDataAsync(currencyModel);

                    currencyResults = _xmlReader.GetCurrencyResults(currencyModel, xmlBody).ToList();

                    foreach (CurrencyResultResponse c in currencyResults)
                    {
                        await _cacheDatabase.SaveAsync(c);
                    }
                }

                currencyResult.AddRange(currencyResults);
            }

            return(currencyResult);
        }