Example #1
0
        public static FizzBuzzResponse ExecFizzBuzz(string lowerNumber, string higherNumber, string inputData)
        {
            var lowerNumberValue  = lowerNumber.ParseInt();
            var higherNumberValue = higherNumber.ParseInt();

            var response = new FizzBuzzResponse()
            {
                LowerNumber  = lowerNumberValue,
                HigherNumber = higherNumberValue
            };

            var objectCollectionList = inputData.Split(new[] { ' ', ',', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            var resultLines = objectCollectionList.Select(iter =>
            {
                var resultLine = new ResulLine()
                {
                    Value = iter
                };

                if (lowerNumberValue != 0 && higherNumberValue != 0)
                {
                    int value;
                    if (int.TryParse(iter, out value))
                    {
                        resultLine.Fizz = value % lowerNumberValue == 0;
                        resultLine.Buzz = value % higherNumberValue == 0;
                    }
                    else
                    {
                        // input was not an integer
                        resultLine.Error = true;
                    }
                }
                else
                {
                    // lower or higher numbers were invalid
                    resultLine.Error = true;
                }
                return(resultLine);
            }).ToArray();

            response.ResultLines = resultLines;
            return(response);
        }
        public async Task <IActionResult> PostFizzBuzz(FizzBuzzRequest request)
        {
            try
            {
                var results = await _service.FizzBuzz(request.MinValue, request.MaxValue, request.Multiples.ToArray());

                var response = new FizzBuzzResponse {
                    Results = results
                };

                return(Ok(response));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);

                if (e is ArgumentOutOfRangeException || e is ArgumentNullException)
                {
                    return(BadRequest(e.Message));
                }

                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }