public ActionResult Index()
 {
     Dictionary<string, string> currencies = CurrencyService.GetCurrencies();
     var vm = new IndexModel();
     vm.AvailableCurrencies = new SelectList(currencies, "Key", "Value");
     vm.SourceCurrency = "USD";      // Initial default
     vm.DestinationCurrency = "JPY"; // Initial default
     return View(vm);
 }
        public void Verify_Destination_Currency_Properly_Calculated()
        {
            // Create mock currency service
            var stubbedCurrencyService = MockRepository.GenerateStub<ICurrencyService>();
            var rate = (decimal)0.5d;
            stubbedCurrencyService.Stub(s => s.GetExchangeRate("ABC", "MTG")).Return(rate);

            // Call controller action
            var controller = new CurrencyController(stubbedCurrencyService);
            var indexModel = new IndexModel { SourceCurrency = "ABC", DestinationCurrency = "MTG", SourceAmount = 15 };
            var result = (ContentResult)controller.Convert(indexModel);

            // Validate
            Assert.That(result.Content, Is.EqualTo((indexModel.SourceAmount * rate).ToString()));
        }
        public ActionResult Convert(IndexModel vm)
        {
            if (!ModelState.IsValid)
            {
                string errors = string.Join("<br />", ModelState.Values
                                        .SelectMany(x => x.Errors)
                                        .Select(x => x.ErrorMessage));
                return new ContentResult{ Content = errors };
            }

            decimal exchangeRate;
            try
            {
                exchangeRate = CurrencyService.GetExchangeRate(vm.SourceCurrency, vm.DestinationCurrency);
            }
            catch (CurrencyServiceException ex)
            {
                return new ContentResult {Content = ex.Message};
            }

            decimal newValue = vm.SourceAmount * exchangeRate;
            return new ContentResult { Content = newValue.ToString() };
        }