public ShortUrlContainer RetrieveUrlFromDatabase(string internal_url) { ShortUrlContainer oShortUrl = new ShortUrlContainer(); oShortUrl.ShortenedUrl = internal_url; using (var _db = new UrlsDBContext()) { try { var clean_url = this.Clean(internal_url); var url = (from c in _db.Urls where c.ShortenedUrl == clean_url select c).FirstOrDefault(); if (url != null) { oShortUrl.CreateDate = url.CreateDate; oShortUrl.CreatedBy = url.CreatedBy; oShortUrl.RealUrl = url.RealUrl; oShortUrl.Title = url.Title; oShortUrl.Description = url.Description; oShortUrl.Image = url.Image; } } catch (Exception exp) { throw new Exception("ERROR: Unable to Retrieve ShortUrlContainer - " + exp.Message.ToString(), exp); } } return(oShortUrl); }
public PartialViewResult _GenerateUrl(UrlGeneratorViewModel model) { ShortUrlContainer oShortUrl = new ShortUrlContainer(); ShortUrlUtils utils = new ShortUrlUtils(); oShortUrl.Title = model.Title; oShortUrl.Description = model.Description; oShortUrl.RealUrl = model.Link; oShortUrl.Image = model.Image; oShortUrl.ShortenedUrl = utils.UniqueShortUrl(); oShortUrl.CreateDate = DateTime.Now; oShortUrl.CreatedBy = Request.UserHostAddress; //utils.AddUrlToDatabase(oShortUrl); oShortUrl.ShortenedUrl = utils.PublicShortUrl(oShortUrl.ShortenedUrl); var generatedUrl = new GeneratedUrlViewModel(); generatedUrl.Link = oShortUrl.ShortenedUrl; return(PartialView("_GeneratedUrlResult", generatedUrl)); }
public void AddUrlToDatabase(ShortUrlContainer oShortUrl) { using (UrlsDBContext _db = new UrlsDBContext()) { // Add product to DB. _db.Urls.Add(oShortUrl); _db.SaveChanges(); } }