Exemplo n.º 1
0
        public IActionResult Index(double FromAmount, string FromCurrency, string ToCurrency)
        {
            if (FromCurrency == ToCurrency)
            {
                ViewBag.Result     = 0;
                ViewBag.Rate       = "";
                ViewBag.FromAmount = FromAmount;
                ViewBag.Error      = "The currency you want to exchange is the same as the currency you want to receive.";
            }
            else
            {
                ViewBag.FromAmount = FromAmount;
                double rate = HttpClientHandler.GetExchangeRateAsync(FromCurrency, ToCurrency);
                ViewBag.Result = FromAmount * rate;
                ViewBag.Rate   = $"•1 {FromCurrency} = {rate} {ToCurrency}";

                ExchangeHistory exchangeHistory = new ExchangeHistory();
                exchangeHistory.Date         = DateTime.UtcNow;
                exchangeHistory.FromAmount   = FromAmount;
                exchangeHistory.FromCurrency = FromCurrency;
                exchangeHistory.ToAmount     = ViewBag.Result;
                exchangeHistory.ToCurrency   = ToCurrency;

                dataManager.ExchangeHistory.SaveExchangeHistory(exchangeHistory);
            }
            return(View());
        }