Exemplo n.º 1
0
        public ActionResult Sort([FromQuery(Name = "array")] string arrayStirng)
        {
            try
            {
                var array = arrayStirng
                            ?.Split(' ')
                            .Select(q =>
                {
                    if (int.TryParse(q, out var result))
                    {
                        return(result);
                    }
                    else
                    {
                        throw new ValidationException($"parameter {nameof(arrayStirng)} does not contain only numbers");
                    }
                })
                            .ToList();

                var sortedList = sortService.BubbleMethod(array);

                using (StreamWriter outputFile = new StreamWriter(fileDirectory))
                {
                    outputFile.WriteLine(string.Join(' ', sortedList));
                }

                return(new JsonResult(new { Array = sortedList }));
            }
            catch (ValidationException e)
            {
                _logger.LogError(e, e.Message);
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }