Exemplo n.º 1
0
 public IActionResult DoUntil(string what, [FromBody] DoUntil doUntil)
 {
     if (doUntil.Until == null)
     {
         return(new JsonResult(new
         {
             error = "Please provide a number!"
         }));
     }
     else
     {
         if (what == "sum")
         {
             return(new JsonResult(new
             {
                 result = doUntil.Sum()
             }));
         }
         else if (what == "factor")
         {
             return(new JsonResult(new
             {
                 result = doUntil.Factorial()
             }));
         }
     }
     return(new JsonResult(new
     {
         error = "Please provide a number!"
     }));
 }
        public IActionResult Index(string what, [FromBody] DoUntil until)
        {
            if (until == null || until.Until == null)
            {
                return(Json(new { error = "Please provide a number!" }));
            }
            if (string.IsNullOrEmpty(what))
            {
                return(NotFound());
            }
            else if (what == "sum")
            {
                int sum = 0;

                for (int i = 0; i <= until.Until; i++)
                {
                    sum += i;
                }
                return(Json(new { result = sum }));
            }
            else if (what == "factor")
            {
                int fact = 1;
                fact = (int)until.Until;
                for (int i = (int)until.Until - 1; i >= 1; i--)
                {
                    fact = fact * i;
                }
                return(Json(new { result = fact }));
            }
            else
            {
                return(Json(new { result = 4 }));
            }
        }
Exemplo n.º 3
0
        public async Task ReturnResultWithUntil5()
        {
            var usedUntil = new DoUntil
            {
                Until = 5,
            };
            var convertedUsedUntil = JsonConvert.SerializeObject(usedUntil);
            var data     = new StringContent(convertedUsedUntil.ToString(), encoding: Encoding.UTF8, mediaType: "application/json");
            var response = await Client.PostAsync("dountil/sum", data);

            string responseJson = await response.Content.ReadAsStringAsync();

            Assert.Equal("{\"result\":15}", responseJson);
        }
Exemplo n.º 4
0
        public async Task ReturnErrorMessageWithOutWhat()
        {
            var usedUntil = new DoUntil
            {
                Until = 5,
            };
            var convertedUsedUntil = JsonConvert.SerializeObject(usedUntil);
            var data     = new StringContent(convertedUsedUntil.ToString(), encoding: Encoding.UTF8, mediaType: "application/json");
            var response = await Client.PostAsync("dountil", data);

            var responseString = await response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
Exemplo n.º 5
0
 public IActionResult Dountil(string what, [FromBody] DoUntil number)
 {
     if (what == "sum" && number == null)
     {
         return(Json(new { error = "Please provide a number!" }));
     }
     else if (what == "sum" && !String.IsNullOrEmpty(number.Number.ToString()))
     {
         return(Json(new { result = number.Sum() }));
     }
     else if (what == "factor" && number == null)
     {
         return(Json(new { error = "Please provide a number!" }));
     }
     else if (what == "factor" && !String.IsNullOrEmpty(number.Number.ToString()))
     {
         return(Json(new { result = number.Factor() }));
     }
     return(Json(new { error = "Please provide a number!" }));
 }