Пример #1
0
 public IActionResult shortner([FromBody] SingleUrl longUrl)
 {
     if (!ValidateUrl(longUrl.url))
     {
         return(BadRequest());
     }
     return(Ok(new { shortUrl = shortener.setShortUrl(longUrl) }));
 }
Пример #2
0
        public string setShortUrl(SingleUrl longUrl)
        {
            string      shortUrl;
            Task <bool> search;

            do
            {
                shortUrl = RandomString();
                search   = dbContext.Urls.AnyAsync(p => p.shortUrl == shortUrl);
                search.Wait();
            }while(search.Result);


            Url newUrl = new Url(longUrl.url, shortUrl);

            dbContext.Urls.Add(newUrl);
            dbContext.SaveChanges();
            return(shortUrl);
        }