Ejemplo n.º 1
0
        public ShortLinkDto GetShortLink(WebUrlDto webUrlDto)
        {
            ShortLinkEntity entity = _repository.GetByWebUrl(webUrlDto.webUrl);

            if (entity == null)
            {
                string shortlink = ShortLinkHelper.GenerateShortLink();

                DeeplinkDto deeplinkDto = _helper.ConvertWebLinkToDeeplink(webUrlDto);

                entity = _repository.Insert(deeplinkDto.deeplink, webUrlDto.webUrl, shortlink);
            }

            return(new ShortLinkDto()
            {
                shortlink = entity.ShortLink
            });
        }
Ejemplo n.º 2
0
 public ActionResult RedirectTo(String shortLink)
 {
     if (shortLink.Length > 8)
     {
         TempData["Error"] = "Превышен размер короткой ссылки";
         return(RedirectToAction("Index"));
     }
     if (shortLink != null)
     {
         var link = store.LinkService.GetByShortLink(ShortLinkHelper.ShortLinkToGuidConverter(shortLink));
         if (link != null)
         {
             ++link.Visitors;
             store.LinkService.Save();
             return(Redirect(link.Original));
         }
     }
     TempData["Error"] = "Ссылка не существует";
     return(RedirectToAction("Index"));
 }
Ejemplo n.º 3
0
        public JsonResult Get()
        {
            // Получить Identity пользователя из Cookies.
            Guid        appUserIdentity;
            AppUser     appUser = null;
            List <Link> links   = new List <Link>();

            if (Guid.TryParse(Request.Cookies.Get("userIdentity")?.Value ?? "", out appUserIdentity))
            {
                appUser = store.AppUserService.GetByIdentity(appUserIdentity);
            }

            if (appUser != null)
            {
                // Получить все ссылки пользователя.
                links = store.LinkService.GetAllByAppUser(appUser).ToList();
            }

            var res = new List <Object>(links.Count);

            foreach (var link in links)
            {
                res.Add(new
                {
                    Short = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/" + ShortLinkHelper.GuidToShortLinkConverter(link.Short),
                    link.Original,
                    CreatedDate = link.CreatedDate.ToString(),
                    link.Visitors
                });
            }

            return(Json(new
            {
                Links = res
            }, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 4
0
        public JsonResult Shorten(ShortenInputModel inputModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new
                {
                    Success = false
                }));
            }

            var shortLink = String.Empty;

            Guid    appUserIdentity;
            AppUser appUser = null;

            if (Guid.TryParse(Request.Cookies.Get("userIdentity")?.Value ?? "", out appUserIdentity))
            {
                appUser = store.AppUserService.GetByIdentity(appUserIdentity);
            }
            else
            {
                appUserIdentity = Guid.NewGuid();
            }

            if (appUser == null)
            {
                appUser = new AppUser
                {
                    Identity = appUserIdentity
                };
                store.AppUserService.Create(appUser);
                store.AppUserService.Save();
            }

            var link = store.LinkService.GetByOriginalLink(inputModel.Url.Trim());

            // Если ссылка не существует в БД.
            if (link == null)
            {
                link = new Link
                {
                    CreatedDate = DateTime.Now,
                    Original    = inputModel.Url.Trim(),
                    Short       = ShortLinkHelper.NewShortLink(8),
                    Visitors    = 0,
                    AppUsers    = new List <AppUser>()
                    {
                        appUser
                    }
                };
                store.LinkService.Create(link);
                store.LinkService.Save();
            }

            Response.SetCookie(new HttpCookie("userIdentity", appUserIdentity.ToString("N")));
            return(Json(new
            {
                Success = true,
                ShortLink = ShortLinkHelper.GuidToShortLinkConverter(link.Short),
                textlink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/"
            }));
        }