public async Task <IActionResult> ShortenUrl()
        {
            //Gets Query string and removes the leading '?' and trims leading and trailing whitespace
            string url = Request.QueryString.Value
                         .Replace('?', ' ').Trim();

            if (url != null)
            {
                if (regex.IsMatch(url))
                {
                    Url newEntry = new Url {
                        Name = url
                    };

                    repo.AddUrl(newEntry);

                    if (await repo.SaveChangesAsync())
                    {
                        return(Json(new { OldUrl = url, NewUrl = $"https://infinite-caverns-82170.herokuapp.com/{newEntry.Id}" }));
                    }
                    else
                    {
                        return(Json(new { Error = "An error occured. Item was not saved to Database." }));
                    }
                }
            }

            return(Json(
                       new {
                Error = "Url Format is incorrect, please use a real site.",
                ProperFormat = "?http(s)://website.com"
            }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateUrl(ShortUrlModel shortUrlModel)
        {
            try
            {
                await _repository.AddUrl(shortUrlModel.Key, shortUrlModel.Url);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }

            return(Created(shortUrlModel.Key, shortUrlModel));
        }