Ejemplo n.º 1
0
        public IActionResult ShortenUrl(string url)
        {
            var     authDb = new UserRepository(_connectionString);
            var     repo   = new UrlRepository(_connectionString);
            UrlLink ul     = new UrlLink();

            if (User.Identity.IsAuthenticated)
            {
                ul.HashedUrl = repo.GetHashedUrlForUser(url, authDb.GetIdForEmail(User.Identity.Name));
            }
            else
            {
                ul.HashedUrl = repo.GetHashedUrl(url);
            }

            if (ul.HashedUrl == null)
            {
                if (User.Identity.IsAuthenticated)
                {
                    repo.AddUrl(url, authDb.GetIdForEmail(User.Identity.Name));
                }
                else
                {
                    repo.AddUrl(url, null);
                }
                ul.HashedUrl = repo.GetHashedUrl(url);
            }
            return(Json(new HashedObj {
                HashedUrl = GetFullUrl(ul.HashedUrl)
            }));
        }
Ejemplo n.º 2
0
        public IActionResult ShortenURL(Url url)
        {
            var authDb = new Authentication(_connectionString);
            var user   = authDb.GetByEmail(User.Identity.Name);

            var repo = new UrlRepository(_connectionString);

            if (!repo.DoesOriginalUrlExist(url.UrlOriginal))
            {
                url.UserId = user.Id;
                url.Views  = 0;
                var foo = true;
                while (foo)
                {
                    url.UrlShortened = ShortId.Generate(8);
                    if (!repo.DoesShortenedUrlExist(url.UrlShortened))
                    {
                        repo.AddUrl(url);
                        foo = false;
                        break;
                    }
                }
            }

            return(Json(url.UrlShortened));
        }
Ejemplo n.º 3
0
        public string AddUrl(string completeUrl, int userId)
        {
            var    repo    = new UrlRepository(_conn);
            string urlHash = repo.AddUrl(completeUrl, userId);

            //return Json(new { urlHash = completeUrl });
            return(urlHash);
        }
Ejemplo n.º 4
0
        public IActionResult UrlShortener(string url)
        {
            UrlRepository repo = new UrlRepository(_connectionString);

            UserRepository ur        = new UserRepository(_connectionString);
            string         hashedUrl = null;

            if (User.Identity.IsAuthenticated)
            {
                int id = ur.GetByEmail(User.Identity.Name).Id;
                hashedUrl = repo.AddUrl(url, id);
            }
            else
            {
                hashedUrl = repo.AddUrl(url);
            }


            return(Json(hashedUrl));
        }
Ejemplo n.º 5
0
        public ActionResult Shorten(string originalurl)
        {
            var manager = new UrlRepository(Properties.Settings.Default.ConStr);

            var url = manager.Check(originalurl, User.Identity.Name);

            if (url == null)
            {
                url = new Url
                {
                    OriginalUrl  = originalurl,
                    ShortenedUrl = ShortId.Generate(true, false),
                    UserId       = manager.GetByEmail(User.Identity.Name).Id
                };
                manager.AddUrl(url);
            }
            return(Json(Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "") + $"/{url.ShortenedUrl}"));
        }
Ejemplo n.º 6
0
        public ActionResult ShortenUrl(string Realurl)
        {
            var urlrepo  = new UrlRepository(Properties.Settings.Default.ConStr);
            var userrepo = new UserRepository(Properties.Settings.Default.ConStr);
            var url      = urlrepo.GetUrl(User.Identity.Name, Realurl);

            if (url == null)
            {
                var user         = userrepo.GetByEmail(User.Identity.Name);
                var shortenedUrl = ShortId.Generate(true, false);
                url = new URL
                {
                    RealURL      = Realurl,
                    ShortenedURL = shortenedUrl,
                    UserId       = user.Id
                };
                urlrepo.AddUrl(url);
            }
            return(Json($"{ Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "")}/{url.ShortenedURL}"));
        }