示例#1
0
        public ActionResult Calculate(CalculatorViewModel viewModel)
        {
            // Validate parameters
            if (!viewModel.IsValid())
            {
                ViewBag.FormMessage = FormMessages.POST_REQUEST_INVALID_PARAMS;

                return(View("Index"));
            }

            // Get the relevant factory for the controller (casting is safe at this point)
            ICalculationFactory calculationFactory = new BinaryCalculationFactory((double)viewModel.Operator1, (double)viewModel.Operator2);

            // Convert the requested operation type
            CalculationType calculationType = EnumUtils.ConvertToEnum <CalculationType>(viewModel.SelectedCalculation);

            try
            {
                // Get the corresponding calculation type
                ICalculation calculation = calculationFactory.Calculation(calculationType);

                // Set the result to the view model and
                viewModel.Result = calculation.Calculate();

                // Log to file asyncronously
                LogToFileAsync(viewModel, calculationType);

                // Pass the view model back to the view
                return(View("Index", viewModel));
            }
            catch (Exception ex)
            {
                return(new HttpStatusCodeResult(500, $"Server Error: {ex.Message}"));
            }
        }
示例#2
0
        public void Invalid_String_Should_Be_Converted_To_Default_Enum(string enumName, CalculationType calculationType)
        {
            //Arrange
            CalculationType expected = calculationType;

            //Act
            CalculationType actual = EnumUtils.ConvertToEnum <CalculationType>(enumName);

            //Assert
            Assert.AreEqual(expected, actual);
        }