public async Task <IActionResult> GetRatesForDate(DateTime dated)
        {
            bool isTodaysDate = false;

            if (dated.Date > DateTime.Now.Date)
            {
                dated        = DateTime.Now;
                isTodaysDate = true;
            }
            else if (dated.Date == DateTime.Now.Date)
            {
                isTodaysDate = true;
            }

            ExchangeRatesView ratesView = null;
            int count = ratesService.GetAPIsCallsCount();

            if (count >= 999)
            {
                return(Ok(null));
            }
            ratesView = await ratesService.GetCurrencyRatesForDate(dated);

            if (ratesView.Rates == null)
            {
                string apiKey   = ratesService.GetAPIKeyForOpenExchange();
                string monthStr = dated.Month < 10 ? "0" + dated.Month : dated.Month.ToString();
                string dateStr  = dated.Day < 10 ? "0" + dated.Day : dated.Day.ToString();
                string datedStr = dated.Year + "-" + monthStr + "-" + dateStr;
                ratesView = (isTodaysDate) ? await ratesHttpService.GetRatesAsync(apiKey) : await ratesHttpService.GetRatesForDateAsync(datedStr, apiKey);

                if (ratesView.Rates != null)
                {
                    await ratesService.SaveCurrencyRatesAsync(ratesView.Rates, dated);
                }
                else
                {
                    if (dated >= (dated.AddDays(-14)))
                    {
                        dated = (dated.AddDays(-15));
                    }
                    monthStr        = dated.Month < 10 ? "0" + dated.Month : dated.Month.ToString();
                    dateStr         = dated.Day < 10 ? "0" + dated.Day : dated.Day.ToString();
                    datedStr        = dated.Year + "-" + monthStr + "-" + dateStr;
                    ratesView.Dated = dated.ToShortDateString();
                    ratesView       = await ratesHttpService.GetRatesForDateAsync(datedStr, apiKey);
                }
            }
            return(Ok(ratesView));
        }
        public async Task <ExchangeRatesView> GetRatesAsync(string apiToken)
        {
            ExchangeRatesView ratesView = new ExchangeRatesView();

            try
            {
                var response = await client.GetStringAsync("latest.json?app_id=" + apiToken);

                var    ratesJson = JsonConvert.DeserializeObject <dynamic>(response);
                string ratesStr  = ratesJson != null?JsonConvert.SerializeObject(ratesJson.rates) : "";

                ratesStr        = ratesStr.Replace("\\", "").Replace("\"", "");
                ratesStr        = ratesStr.Replace("{", "");
                ratesStr        = ratesStr.Replace("}", "");
                ratesView.Rates = this.GetRatesList(ratesStr);
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
            return(await Task <List <CurrencyWithRates> > .Run(() => ratesView).ConfigureAwait(false));
        }
Пример #3
0
        public async Task <ExchangeRatesView> GetCurrencyRatesForDate(DateTime dated)
        {
            var unitWork = new UnitOfWork(context);
            ExchangeRatesView        ratesView = new ExchangeRatesView();
            List <CurrencyWithRates> ratesList = new List <CurrencyWithRates>();
            var exchangeRates = await unitWork.ManualRatesRepository.GetManyQueryableAsync(r => r.Year == dated.Year);

            if (exchangeRates.Any())
            {
                foreach (var exRate in exchangeRates)
                {
                    ratesList.Add(new CurrencyWithRates()
                    {
                        Currency     = exRate.Currency,
                        Rate         = exRate.ExchangeRate,
                        CurrencyName = exRate.Currency
                    });
                }
                ratesView.Rates = ratesList;
            }
            return(await Task <ExchangeRatesView> .Run(() => ratesView).ConfigureAwait(false));
        }
        public async Task <IActionResult> GetAverageCurrencyRateForDate([FromBody] ExRateFinderModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ExchangeRatesView ratesView = null;
            int count = ratesService.GetAPIsCallsCount();

            if (count >= 999)
            {
                return(Ok(null));
            }

            if (model.Dated > DateTime.Now)
            {
                ratesView = await ratesService.GetCurrencyRatesForDate(DateTime.Now);
            }
            else
            {
                ratesView = await ratesService.GetCurrencyRatesForDate(model.Dated);
            }

            if (ratesView.Rates == null)
            {
                string apiKey = ratesService.GetAPIKeyForOpenExchange();
                ratesView = await ratesHttpService.GetRatesAsync(apiKey);

                if (ratesView.Rates != null)
                {
                    await ratesService.SaveCurrencyRatesAsync(ratesView.Rates, DateTime.Now);
                }
            }

            var rates = await ratesService.GetAverageCurrencyRatesForDate(model.Dated);

            return(Ok(rates));
        }
        public async Task <IActionResult> GetLatestRates()
        {
            ExchangeRatesView ratesView = null;
            int count = ratesService.GetAPIsCallsCount();

            if (count >= 999)
            {
                return(Ok(null));
            }

            ratesView = await ratesService.GetLatestCurrencyRates();

            if (ratesView.Rates == null)
            {
                string apiKey = ratesService.GetAPIKeyForOpenExchange();
                ratesView = await ratesHttpService.GetRatesAsync(apiKey);

                if (ratesView.Rates != null)
                {
                    await ratesService.SaveCurrencyRatesAsync(ratesView.Rates, DateTime.Now);
                }
            }
            return(Ok(ratesView));
        }
Пример #6
0
        public async Task <ExchangeRatesView> GetLatestCurrencyRates()
        {
            var unitWork = new UnitOfWork(context);

            ExchangeRatesView       ratesView  = new ExchangeRatesView();
            IQueryable <EFCurrency> currencies = null;

            try
            {
                var exRateSettings = unitWork.ExRatesSettingsRepository.GetOne(r => r.Id != 0);

                List <CurrencyWithRates> ratesList = new List <CurrencyWithRates>();
                DateTime dated = DateTime.Now;
                ratesView.Dated = dated.Date.ToString();
                var exchangeRate = await unitWork.ExchangeRatesRepository.GetOneAsync(e => e.Dated.Date == dated.Date);

                ratesView.Rates = (exchangeRate != null) ? JsonConvert.DeserializeObject <List <CurrencyWithRates> >(exchangeRate.ExchangeRatesJson) : null;

                if (ratesView.Rates != null)
                {
                    int count = ratesView.Rates.Count;
                    currencies = unitWork.CurrencyRepository.GetManyQueryable(c => c.Id != 0);
                    if (currencies.Count() < count)
                    {
                        var currencyCodes = (from c in currencies
                                             select c.Currency).ToList <string>();
                        var currencyNames = (from c in currencies
                                             select c.CurrencyName).ToList <string>();

                        var currenciesFromAPI           = ratesView.Rates;
                        List <EFCurrency> newCurrencies = new List <EFCurrency>();
                        foreach (var currency in currenciesFromAPI)
                        {
                            if (!currencyCodes.Contains(currency.Currency))
                            {
                                newCurrencies.Add(new EFCurrency()
                                {
                                    Currency = currency.Currency
                                });
                            }
                        }
                        if (newCurrencies.Count > 0)
                        {
                            unitWork.CurrencyRepository.InsertMultiple(newCurrencies);
                            unitWork.Save();
                        }
                    }
                }

                if (currencies != null && currencies.Count() > 0)
                {
                    foreach (var rate in ratesView.Rates)
                    {
                        rate.CurrencyName = (from c in currencies
                                             where c.Currency == rate.Currency
                                             select c.CurrencyName).FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }

            return(await Task <ExchangeRatesView> .Run(() => ratesView).ConfigureAwait(false));
        }