public async Task <IActionResult> AllCurrencies(int?page, [FromQuery] int?count) { try { GetCurrencyOpts opts = new GetCurrencyOpts(page ?? 1, count ?? 5); var currencies = await _currencyService.GetCurrenciesAsync(opts); var viewModels = currencies.Select(x => new CurrencyViewModel(x)); return(Ok(viewModels)); } catch (Exception e) { return(ProcessError(e)); } }
public async Task <IEnumerable <CurrencyData> > GetCurrenciesAsync(GetCurrencyOpts opts = null) { if (!_memCache.TryGetValue(MEMCACHE_CURRENCIES_KEY, out IEnumerable <CurrencyData> currencies)) { try { using var client = _httpFactory.CreateClient(); var data = await client.GetAsync(BANK_GET_CURRENCIES_URL); var resultStr = await data.Content.ReadAsStringAsync(); currencies = JsonConvert.DeserializeObject <BankDailyResult>(resultStr).Valute.Values; } catch (JsonException) { throw new Exception("Error while deserialize data, bank maybe change json format"); } catch (HttpRequestException) { throw new BankServiceException("Error while make request to bank, maybe he go offline"); } DateTime dateForRefresh = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 11, 30, 00 ); if (DateTime.Now.Hour > 11 || (DateTime.Now.Hour == 11 && DateTime.Now.Minute > 30)) { dateForRefresh = dateForRefresh.AddDays(1); } // ЦБ РФ определяет курс в 11:30, так что можем смело кешировать, до этого времени _memCache.Set(MEMCACHE_CURRENCIES_KEY, currencies, dateForRefresh); } if (opts != null) { currencies = currencies.Skip((opts.Page - 1) * opts.Count).Take(opts.Count); } return(currencies); }