public IActionResult MyAccount() { var urlRepo = new ShortenedUrlsRepository(_connectionString); var urls = urlRepo.GetUrlsForUser(User.Identity.Name); return(View(urls)); }
public IActionResult ViewShortUrl(string hash) { var urlRepo = new ShortenedUrlsRepository(_connectionString); var url = urlRepo.GetByHash(hash); if (url == null) { return(Redirect("/")); } urlRepo.AddView(url.Id); return(Redirect(url.OriginalUrl)); }
public IActionResult ShortenUrl(string originalUrl) { ShortenedUrl url = null; var urlRepo = new ShortenedUrlsRepository(_connectionString); if (User.Identity.IsAuthenticated) { url = urlRepo.GetByOriginalUrlForUser(User.Identity.Name, originalUrl); } else { url = urlRepo.GetByOriginalUrl(originalUrl); } if (url == null) { var hash = ShortId.Generate(7); while (urlRepo.HashExists(hash)) { hash = ShortId.Generate(7); } url = new ShortenedUrl { OriginalUrl = originalUrl, UrlHash = hash }; if (User.Identity.IsAuthenticated) { var userRepo = new UserRepository(_connectionString); var user = userRepo.GetByEmail(User.Identity.Name); url.UserId = user.Id; } urlRepo.Add(url); } return(Json(new { shortUrl = GetFullUrl(url.UrlHash) })); }