Esempio n. 1
0
        public IActionResult AddNumbers(string number1, string number2)
        {
            //Add values to a list string
            List <string> numberList = new string[] { number1, number2 }.ToList();

            //Validating string list
            var errorMsg = numberValidator.ValidateStrings(numberList);

            //Return bad request if error is detected
            if (!string.IsNullOrEmpty(errorMsg))
            {
                ModelState.AddModelError("Validation Error", errorMsg);
                logActivityService.LogActivity($"Timestamp= {DateTime.Now.ToString()}, number1={number1} , number2={number2} , ERROR_MSG = {errorMsg}");
                return(BadRequest(ModelState));
            }

            ICalculateNumbers calculateNumbers = new CalculateNumbers();

            //Calculate total sum of numbers
            calculateNumbers.AddNumbers(number1, number2);

            //Create DTO for serialization
            var numbers = new NumbersSumDto()
            {
                Value1   = number1,
                Value2   = number2,
                TotalSum = calculateNumbers.Result
            };

            //Creating error log
            logActivityService.LogActivity($"Timestamp= {DateTime.Now.ToString()}, number1={number1} , number2={number2} , result={numbers.TotalSum}");
            return(Ok(numbers));
        }