/// <summary> /// This Function Redirects oncoming connections to the Orignal Urls as mapped in the DB. also stores some information along the way /// </summary> /// <param name="customUrl">Short Url in question</param> /// <returns>Stastics Object</returns> public Task <Statistics> Click(string customUrl) { return(Task.Run(() => { using (DBContext ctx = new DBContext()) { #region Checks if Mapping present in DB URLDetails url = ctx.URLDetails.Where(u => u.CustomUrl == customUrl).FirstOrDefault(); if (url == null) { throw new ShortUrlNotFoundException(); } #endregion #region increments Clicks counter url.ClickCounter = url.ClickCounter + 1; #endregion Statistics stat = new Statistics() { ClickDate = DateTime.Now, ShortUrl = url }; ctx.Statistics.Add(stat); ctx.SaveChanges(); return stat; } })); }
public List <URLDetails> GetBreadCrumb(Item CurrentItem) { List <URLDetails> BreadCrumb = new List <URLDetails>(); IBreadCrumb BreadCrumbItem = ScContext.Cast <IBreadCrumb>(Sitecore.Context.Database.GetItem(Constants.RenderingItemIDs.BreadcrumbId)); var BreadCrumbRestriction = BreadCrumbItem.SitecoreID.Split(','); if (CurrentItem != null) { URLDetails UrlObj = new URLDetails(); UrlObj.LinkName = ScContext.Cast <IBreadCrumbInfo>(CurrentItem).BreadCrumbTitle; UrlObj.LinkURL = Sitecore.Links.LinkManager.GetItemUrl(CurrentItem); BreadCrumb.Add(UrlObj); Item Parent = CurrentItem.Parent; while (!BreadCrumbRestriction.Contains(Parent.TemplateID.ToString())) { var TempItem = Parent; UrlObj = new URLDetails(); UrlObj.LinkName = ScContext.Cast <IBreadCrumbInfo>(TempItem).BreadCrumbTitle; UrlObj.LinkURL = Sitecore.Links.LinkManager.GetItemUrl(TempItem); BreadCrumb.Add(UrlObj); Parent = TempItem.Parent; } BreadCrumb.Reverse(); } return(BreadCrumb); }
public async Task <ActionResult> ShowStats(string segment) { URLDetails details = null; if (ModelState.IsValid) { details = await _manager.FetchStatistics(segment); } return(View(details)); }
public async Task <ActionResult> Index(URLModel url) { if (ModelState.IsValid) { URLDetails shortUrl = await this._manager.ShortenUrl(url.OriginalURL, Request.UserHostAddress); url.ShortURL = string.Format("{0}://{1}{2}{3}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"), shortUrl.CustomUrl); } return(View(url)); }
/// <summary> /// Fetches the Statictics for a link created before or just now /// </summary> /// <param name="customUrl">Link Segment in question</param> /// <returns>URLDetail Object containing information</returns> public Task <URLDetails> FetchStatistics(string customUrl) { return(Task.Run(() => { using (DBContext ctx = new DBContext()) { URLDetails details = ctx.URLDetails.Where(u => u.CustomUrl == customUrl).FirstOrDefault(); if (details == null) { throw new ShortUrlNotFoundException(); } return details; } })); }
/// <summary> /// Driving Function of the Process, Validates Orignal URL and Custom URL(If present) and assigns a suitable short Url /// </summary> /// <param name="orignalUrl">Long URL in question</param> /// <param name="ip">Ip of the request</param> /// <param name="customUrl">Custom URL if present</param> /// <returns>URLDetails Object</returns> public Task <URLDetails> ShortenUrl(string orignalUrl, string ip, string customUrl = "") { return(Task.Run(() => { using (var ctx = new DBContext()) { URLDetails url; #region Check If Orignal Url Present in DB if Present give the last short url url = ctx.URLDetails.Where(u => u.OrignalUrl == orignalUrl).FirstOrDefault(); if (url != null) { return url; } #endregion #region Validation for Orignal URL if (!orignalUrl.StartsWith("http://") && !orignalUrl.StartsWith("https://")) { throw new ArgumentException("Invalid URL format"); } Uri urlCheck = new Uri(orignalUrl); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlCheck); request.Timeout = 10000; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); } catch (Exception) { throw new ShortUrlNotFoundException(); } #endregion #region Custom URL Process if (!string.IsNullOrEmpty(customUrl)) { if (ctx.URLDetails.Where(u => u.CustomUrl == customUrl).Any()) { throw new ShorturlConflictException(); } if (customUrl.Length > 20 || !Regex.IsMatch(customUrl, @"^[A-Za-z\d_-]+$")) { throw new ArgumentException("Malformed or too long segment"); } } else { customUrl = this.NewUrl(); } #endregion if (string.IsNullOrEmpty(customUrl)) { throw new ArgumentException("Segment is empty"); } url = new URLDetails() { CreatedOn = DateTime.Now, OrignalUrl = orignalUrl, Ip = ip, ClickCounter = 0, CustomUrl = customUrl }; ctx.URLDetails.Add(url); ctx.SaveChanges(); return url; } })); }