예제 #1
0
        public ActionResult ShorterURL(string longUrl)
        {
            db.Configuration.ProxyCreationEnabled = false;

            if (string.IsNullOrEmpty(longUrl))
                return Json(new { status = false, message = "Please provide URL" }, JsonRequestBehavior.AllowGet);
            else
            {
                if (!new URL().HasHTTPProtocol(longUrl))
                    longUrl = "http://" + longUrl;

                // Check if long URL already exists in the database
                URL existingURL = db.Urls.Where(u => u.LongUrl.ToLower() == longUrl.ToLower()).FirstOrDefault();

                // if the long url doesnt yet exist
                if (existingURL == null)
                {
                    URL shortUrl = new URL()
                    {
                        LongUrl = longUrl,
                        GeneratedDate = DateTime.UtcNow,
                        Hits = 0
                    };
                    string userId = User.Identity.GetUserId();

                    if (shortUrl.CheckLongUrlExists())  // goes to the site to check its valid
                    {
                        // Main work happens here
                        var prevItem = db.Urls.OrderByDescending(s => s.UrlId).FirstOrDefault();
                        int nextItemId = prevItem != null ? prevItem.UrlId + 1 : 1;
                        string result = URL.Encode(nextItemId);
                        shortUrl.AssignShortUrl(result);

                        //assigning userId
                        if (!string.IsNullOrEmpty(userId))
                            shortUrl.UserId = userId;

                        try
                        {
                            //adding it to the database
                            db.Urls.Add(shortUrl);
                            db.SaveChanges();

                            shortUrl.ShortUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "/" + shortUrl.ShortUrl;

                            return Json(new { status = true, url = shortUrl }, JsonRequestBehavior.AllowGet);
                        }
                        catch (Exception exc)
                        {
                            log.Error(exc);
                            return Json(new { status = false, message = exc.Message }, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                        return Json(new { status = false, message = "Not valid URL provided" }, JsonRequestBehavior.AllowGet);
                } // if the long url already exists
                else
                {
                    existingURL.ShortUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "/" + existingURL.ShortUrl;
                    return Json(new { status = true, url = existingURL }, JsonRequestBehavior.AllowGet);
                }
            }
        }
예제 #2
0
        public ActionResult ShorterURL(string longUrl)
        {
            if (string.IsNullOrEmpty(longUrl))
                return Json(new { status = false, message = "Please provide URL" }, JsonRequestBehavior.AllowGet);
            else
            {
                if (!new URL().HasHTTPProtocol(longUrl))
                    longUrl = "http://" + longUrl;

                // Check if long URL already exists in the database
                URL existingURL = db.Urls.Where(u => u.LongUrl.ToLower() == longUrl.ToLower()).FirstOrDefault();

                if (existingURL == null)
                {
                    URL shortUrl = new URL()
                    {
                        LongUrl = longUrl,
                        GeneratedDate = DateTime.UtcNow,
                        Hits = 0
                    };
                    string userId = User.Identity.GetUserId();

                    if (shortUrl.CheckLongUrlExists())
                    {
                        shortUrl.GenerateRandomShortUrl();
                        if (!string.IsNullOrEmpty(userId)) // only if user is authorized
                            shortUrl.UserId = userId;

                        db.Urls.Add(shortUrl);
                        try
                        {
                            db.SaveChanges();
                            shortUrl.ShortUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "/" + shortUrl.ShortUrl;

                            return Json(new { status = true, url = shortUrl }, JsonRequestBehavior.AllowGet);
                        }
                        catch (Exception exc)
                        {
                            log.Error(exc);
                            return Json(new { status = false, message = exc.Message }, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                        return Json(new { status = false, message = "Not valid URL provided" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    existingURL.ShortUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "/" + existingURL.ShortUrl;
                    return Json(new { status = true, url = existingURL }, JsonRequestBehavior.AllowGet);
                }
            }
        }