public async Task <ActionResult> Index(CurrenciesViewModel model) { List <string> currenciesList = await _currenciesListHelper.GetCurrenciesListAsync(); model.SelectedBase = currenciesList.FirstOrDefault (x => x.Contains(ConfigurationManager.AppSettings["DefaultBase"])); model.SelectedQuote = currenciesList.FirstOrDefault (x => x.Contains(ConfigurationManager.AppSettings["DefaultQuote"])); //Or defaults if (model.SelectedBase == null) { model.SelectedBase = currenciesList[0]; } if (model.SelectedQuote == null) { model.SelectedQuote = currenciesList[1]; } DatesRangeUIModel datesRangeModel = await _datesRangeHelper.GetDatesRangeAsync(); model.StartDate = DateTime.Parse(datesRangeModel.StartDate.ToString("dd-MM-yyyy")); model.EndDate = DateTime.Parse(datesRangeModel.EndDate.ToString("dd-MM-yyyy")); model.Date = model.EndDate; Session["CurrenciesList"] = currenciesList; model.Rate = await _rateHelper.GetRateAsync(model.SelectedBase, model.SelectedQuote, model.Date); return(View(model)); }
public async Task <DatesRangeUIModel> GetDatesRangeAsync() { if (_apiHelper.UseLocalApi) { return(await _apiHelper.GetDatesRangeAsync()); } else { DatesRangeUIModel model = new DatesRangeUIModel { StartDate = DateTime.Parse(ConfigurationManager.AppSettings["RemoteApiStartDate"]), EndDate = DateTime.Now, }; return(model); } }
//To avoid calling async method on constructor protected async override void OnViewLoaded(object view) { base.OnViewLoaded(view); //Changes in controls automatically calls the API for updating values, setting this property to false avoids it CanCallGetRateAsync = false; //Gets the list of avilables currencies try { CurrenciesList = await _currenciesListHelper.GetCurrenciesListAsync(); //Sets initial values for comboboxes SetSelectedCurrencies(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error, could not retrive currencies list. Sorry, shutting down... "); Application.Current.Shutdown(); } //Gets the range of avilables dates in the database try { DatesRangeUIModel model = await _datesRangeHelper.GetDatesRange(); StartDate = model.StartDate; EndDate = model.EndDate; //Initialize DatePiker in end date Date = model.EndDate; } catch (Exception ex) { MessageBox.Show(ex.Message, $"Error, could not retrieve avilable dates range, " + $"the application may not work correctly"); } CanCallGetRateAsync = true; //Finally the call to the API is made await GetRateAsync(); }