Пример #1
0
        public IActionResult Personalize(Link link)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(link.Short))
                {
                    link.NumberOfClicks = 0;
                    var linkIsInDatabase = Context.Links.SingleOrDefault(x => x.Long == link.Long);

                    if (linkIsInDatabase == null)
                    {
                        link.NumberOfClicks = 0;
                        link.Short          = Shortener.Hash(link.Long);
                        Context.Users.Include(x => x.Links).Single(x => x.UserName == User.Identity.Name).Links.Add(link);
                        Context.SaveChanges();
                        ViewBag.Message = "http://localhost:59290/" + link.Short;
                        return(View());
                    }
                    else
                    {
                        ViewBag.Message = "http://localhost:59290/" + linkIsInDatabase.Short;
                        return(View());
                    }
                }
                else
                {
                    var linkIsInDatabase = Context.Links.SingleOrDefault(x => x.Short == link.Short);
                    if (linkIsInDatabase == null)
                    {
                        link.NumberOfClicks = 0;
                        Context.Users.Include(x => x.Links).Single(x => x.UserName == User.Identity.Name).Links.Add(link);
                        Context.SaveChanges();
                        ViewBag.Message = "http://localhost:59290/" + link.Short;
                        return(View());
                    }
                    else
                    {
                        ViewBag.Message = "This personalized option is already in use!";
                        return(View());
                    }
                }
            }

            return(View("Index", link));
        }
Пример #2
0
        public IActionResult Edit(int id, string @short)
        {
            if (string.IsNullOrEmpty(@short))
            {
                Link link = Context.Links.Single(x => x.Id == id);

                if (Context.Links.Count(x => x.Long == link.Long) == 1)
                {
                    link.Short = Shortener.Hash(link.Long);
                    Context.Links.Update(link);
                    Context.SaveChanges();
                    return(RedirectToAction("ShowLinks"));
                }
                else
                {
                    ViewBag.Message = "This shorten form is already in database!";
                    return(View(link));
                }
            }
            else
            {
                var linkIsInDatabase = Context.Links.SingleOrDefault(x => x.Short == @short);

                if (linkIsInDatabase == null)
                {
                    var linkToChange = Context.Links.Single(x => x.Id == id);
                    linkToChange.Short = @short;
                    Context.Links.Update(linkToChange);
                    Context.SaveChanges();
                    return(RedirectToAction("ShowLinks"));
                }
                else
                {
                    ViewBag.Message = "This shorten form is already in database!";
                    return(View());
                }
            }
        }
Пример #3
0
        public IActionResult Result(Link link)
        {
            if (ModelState.IsValid)
            {
                var linkIsInDatabase = Context.Links.SingleOrDefault(x => x.Long == link.Long);

                if (linkIsInDatabase == null)
                {
                    link.NumberOfClicks = 0;
                    link.Short          = Shortener.Hash(link.Long);
                    Context.Links.Add(link);
                    Context.SaveChanges();
                    ViewBag.Message = "http://localhost:59290/" + link.Short;
                    return(View());
                }
                else
                {
                    ViewBag.Message = "http://localhost:59290/" + linkIsInDatabase.Short;
                    return(View());
                }
            }

            return(View("Index", link));
        }