Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
 public async Task <CacheUser> GetUserAsync(string connectionId, CancellationToken cancellationToken)
 {
     return(await _cacheDatabase.GetAsync <CacheUser>(USER_KEY + connectionId, cancellationToken));
 }