public IActionResult shortner([FromBody] SingleUrl longUrl) { if (!ValidateUrl(longUrl.url)) { return(BadRequest()); } return(Ok(new { shortUrl = shortener.setShortUrl(longUrl) })); }
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); }