public UrlInfo ShortenUrl(string originalUrl) { using (var ctx = new UrlContext()) { UrlInfo url; url = ctx.UrlInfos.Where(u => u.OriginalUrl == originalUrl).FirstOrDefault(); if (url != null) { return(url); } var abreviation = this.NewAbreviation(); if (string.IsNullOrEmpty(abreviation)) { throw new ArgumentException("Could not find a abreviation"); } url = new UrlInfo() { Added = DateTime.Now, OriginalUrl = originalUrl, Abreviation = abreviation }; ctx.UrlInfos.Add(url); ctx.SaveChanges(); return(url); } }
public string ShortUrl(string Url) { if (Url.Trim() != "") { string shortUrl = GetRandomUrl(); using (UrlContext dbContext = new UrlContext()) { try { while (dbContext.Urls.Any(ur => ur.Surl == shortUrl)) { shortUrl = GetRandomUrl(); } UrlModel data = new UrlModel(); data.Surl = shortUrl; data.Url = Url; dbContext.Urls.Add(data); dbContext.SaveChanges(); } catch { } return(shortUrl); } } return(""); }
public ViewResult Index(String searchingText) { try { Task[] tasks = new Task[preferences.parsers.Count]; int i = 0; preferences.parsers.ForEach(p => { tasks[i] = p.LoadPage(searchingText); i++; }); int index = Task.WaitAny(tasks); List <String> result = preferences.parsers[index].Parse(); foreach (var r in result) { FoundUrl url = new FoundUrl(); url.Url = r; url.Engine = preferences.parsers[index].searchEngineUrl; url.DateFound = DateTime.Now; context.Add(url); } context.SaveChanges(); return(View(result)); } catch (AggregateException ex) { List <String> result = new List <String>(); foreach (var e in ex.InnerExceptions) { result.Add(e.Message); } return(View(result)); } }
public Task <Statistic> Click(string shortUrl, string referer, string ip) { return(Task.Run(() => { using (var context = new UrlContext()) { var url = context.Urls.Include(r => r.Statistics).Where(o => o.ShortUrl.Equals(shortUrl)).FirstOrDefault(); if (url == null) { throw new Exception("Short Not Found"); } url.ClicksCount += 1; Statistic stat = new Statistic { ClickDate = DateTime.Now, Ip = ip, Referer = referer, Url = url }; url.Statistics.Add(stat); context.Urls.Update(url); context.Statistics.Add(stat); context.SaveChanges(); return stat; } })); }
public string Edit(int id, string LongUrl, string ShortUrl, DateTime Date, int Count) { foreach (var url in db.Urls) { if (url.id == id) { url.LongUrl = LongUrl; url.ShortUrl = ShortUrl; url.Date = DateTime.Today; url.Count = 0; break; } } db.SaveChanges(); return("Ссылка , \"" + LongUrl + "\" сокращена до : \"" + ShortUrl + "\""); }
public IActionResult Create([FromBody] UrlDto urlDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var url = new Url() { Description = urlDto.Description, Redirect = urlDto.Redirect }; _context.Urls.Add(url); _context.SaveChanges(); return(Ok(url)); }
public ActionResult ShorterUrl(string longUrl) { if (string.IsNullOrEmpty(longUrl)) { return(Json(new { status = false, message = "Please provide URL" }, JsonRequestBehavior.AllowGet)); } if (!Helpers.UrlHelper.IsUrlExists(longUrl)) { return(Json(new { status = false, message = "Not valid URL provided" }, JsonRequestBehavior.AllowGet)); } if (!Helpers.UrlHelper.HasHttpProtocol(longUrl)) { longUrl = "http://" + longUrl; } using (_context) { var urlInDb = _context.Urls.FirstOrDefault(u => u.LongUrl == longUrl); if (urlInDb == null) { var url = new Url { LongUrl = longUrl, DateGenerated = DateTime.Now, ShortUrl = Helpers.UrlHelper.GenerateRandomShortUrl() }; _context.Urls.Add(url); _context.SaveChanges(); var shortUrl = Helpers.UrlHelper.BuildUrlFromSegments( Request.Url.Scheme, Request.Url.Authority, url.ShortUrl); return(Json(new { status = true, url = shortUrl }, JsonRequestBehavior.AllowGet)); } else { var shortUrl = Helpers.UrlHelper.BuildUrlFromSegments( Request.Url.Scheme, Request.Url.Authority, urlInDb.ShortUrl); return(Json(new { status = true, url = shortUrl }, JsonRequestBehavior.AllowGet)); } } }
public IActionResult Index(UrlSite urlSites, DataUrl dataUrl, string siteUrl) { if (siteUrl == null) { return(View()); } var regex = new Regex(pattern2); var remove = regex.Replace(siteUrl, string.Empty); HttpContext.Session.SetString("Url", remove); urlSites.SiteUrl = remove; urlSites.Date = DateTime.Now; db.UrlSites.Add(urlSites); db.SaveChanges(); string[] urls = BuildSitemap(urlSites); RequestsUrl(urlSites, urls); return(RedirectToAction("Result")); }
public ActionResult RedirectToLong(string shortURL) { if (string.IsNullOrEmpty(shortURL)) { return(RedirectToAction("NotFound", "Home")); } else { URL url = db.Urls.Where(u => u.ShortUrl == shortURL).FirstOrDefault(); if (url == null) { return(RedirectToAction("NotFound", "Home")); } else { #region Statistics collected for this URL UrlStat stats = new UrlStat(Request); stats.UrlId = url.UrlId; try { db.UrlStats.Add(stats); db.SaveChanges(); } catch (Exception exc) { log.Error(exc); } #endregion url.Hits++; // Adiciona visita db.SaveChanges(); Response.StatusCode = 302; return(Redirect(url.LongUrl)); // Redireciona URL } } }
public ActionResult Create(Url urlObject) { if (ModelState.IsValid) { ctx.Url.Add(urlObject); ctx.SaveChanges(); return(Json(new { urlObject, JsonRequestBehavior.AllowGet })); } string errorMessage = "Not Successful"; return(Json(new { Message = errorMessage, JsonRequestBehavior.DenyGet })); }
public UrlServiceTest() { // Giving each DB a different guid name to prevent conflicts var options = new DbContextOptionsBuilder <UrlContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; UrlContextMock = new UrlContext(options); UrlServiceToTest = new UrlService(UrlContextMock); UrlContextMock.Database.EnsureCreated(); UrlContextMock.Urls.AddRange( new Entities.Models.Entity.Url { Id = 1, AccessCount = 0, ActualUrl = "https://google.co.uk", ShortenedUrl = "asTG36", Created = DateTime.Now }, new Entities.Models.Entity.Url { Id = 2, AccessCount = 0, ActualUrl = "https://youtube.co.uk", ShortenedUrl = "fr7Y2J", Created = DateTime.Now }, new Entities.Models.Entity.Url { Id = 3, AccessCount = 0, ActualUrl = "https://github.co.uk", ShortenedUrl = "TT66hh", Created = DateTime.Now } ); UrlContextMock.SaveChanges(); }
public void AddNewUrl(Url longUrl) { urlContext.Urls.Add(longUrl); urlContext.SaveChanges(); }
public Url Add(Url url) { _db.Urls.Add(url); _db.SaveChanges(); return(url); }
public ActionResult EditUrl(UrlModel url) { db.Entry(url).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult UrlTable(ListOfUrls listOfUrls) { urlContext.Entry(listOfUrls).State = EntityState.Modified; urlContext.SaveChanges(); return(View(urlContext.ListOfUrls.ToList())); }