예제 #1
0
        public async Task <IActionResult> FizzBuzz(FizzBuzzFormModel fizzBuzzFormModel)
        {
            if (!ValidateUserInput(fizzBuzzFormModel))
            {
                return(BadRequest("Server validation failed."));
            }

            //Add record to database.
            await AddGameDataToDB(fizzBuzzFormModel);

            //Return the results of the FizzBuzz game to the appropriate view.
            return(PartialView("~/Views/Home/PartialFizzBuzz.cshtml", PlayFizzBuzz(fizzBuzzFormModel)));
        }
예제 #2
0
        /// <summary>
        /// Returns result of FizzBuzz game using multiples of 3 and 5 by default.
        /// </summary>
        /// <param name="fizzBuzzFormModel">The start and end values of the FizzBuzz game. 1 to 100 by default.</param>
        /// <returns>Returns result of FizzBuzz game using multiples of 3 and 5 by default.</returns>
        public List <FizzBuzzResultViewModel> PlayFizzBuzz(FizzBuzzFormModel fizzBuzzFormModel)
        {
            //Create list of results to send to view.
            var results = new List <FizzBuzzResultViewModel>();

            //Loop through start value to end value.
            for (int i = fizzBuzzFormModel.StartValue; i <= fizzBuzzFormModel.EndValue; i++)
            {
                /* Use modulo operator to determine if value is multiple of 3 or 5.
                 * Returns the remainder after dividing two numbers.*/
                var isMultipleOf3 = i % 3 == 0;
                var isMultipleOf5 = i % 5 == 0;

                //Create new result for this loop iteration.
                FizzBuzzResultViewModel result = new FizzBuzzResultViewModel();

                //Determine the FizzBuzz result. (Potentially not efficient as the result may be set multiple times.)
                //Check if multiple of 3.
                if (isMultipleOf3)
                {
                    result.fizzBuzzResult = "Three";
                }

                //Check if multiple of 5.
                if (isMultipleOf5)
                {
                    result.fizzBuzzResult = "Five";
                }

                //Check if multiple of 3 and 5.
                if (isMultipleOf3 && isMultipleOf5)
                {
                    result.fizzBuzzResult = "Eurofins";
                }

                //Otherwise, just use current iteration number.
                if (result.fizzBuzzResult == null)
                {
                    result.fizzBuzzResult = i.ToString();
                }

                result.initialValue = i.ToString();

                //Add the current result to the list of results.
                results.Add(result);
            }

            //Return all results for this game.
            return(results);
        }
예제 #3
0
        /// <summary>
        /// Adds data about the current game of FizzBuzz to the database.
        /// </summary>
        /// <param name="fizzBuzzFormModel">Start and end value.</param>
        public async Task AddGameDataToDB(FizzBuzzFormModel fizzBuzzFormModel)
        {
            //Make sure that the database is created.
            await _context.Database.EnsureCreatedAsync();

            //Add new entry to context. Not in database yet.
            _context.FizzBuzzGameData.Add(new Data.FizzBuzzDataModel
            {
                startValue = fizzBuzzFormModel.StartValue,
                endValue   = fizzBuzzFormModel.EndValue,
                dateTime   = DateTime.Now
            });

            //Save context changes. Make changes to the database.
            await _context.SaveChangesAsync();
        }
예제 #4
0
        /// <summary>
        /// Validation for the user input, start and end values.
        /// </summary>
        /// <param name="fizzBuzzFormModel">The start and end values of the FizzBuzz game. 1 to 100 by default</param>
        /// <returns>True of the input is valid.</returns>
        public bool ValidateUserInput(FizzBuzzFormModel fizzBuzzFormModel)
        {
            var startValue = fizzBuzzFormModel.StartValue;
            var endValue   = fizzBuzzFormModel.EndValue;

            //Start value must be greater than end value.
            if (startValue > endValue)
            {
                return(false);
            }

            //No more than 1000 iterations.
            if ((endValue - startValue) > 200)
            {
                return(false);
            }

            //Otherwise validation has passed.
            return(true);
        }