/// <summary> /// create new short url /// </summary> /// <param name="model">most important thing is in the model is the ActualURL</param> /// <returns></returns> public ShortUrlResponseModel Create(ShortURLRequestModel model) { //check if the url saved perviously ShortURLModel foundUrl = URLExist(model.ActualURL); if (foundUrl != null) { return(new ShortUrlResponseModel { Model = foundUrl, Success = false, Message = "This url has been saved befor" }); } //create new model to save in data base ShortURLModel shortURLModel = new ShortURLModel { DateOfCreation = DateTime.Now, ActualURL = model.ActualURL, //try to create a random alphanumeric short url ShortenedURL = AutoURLGenerator.ShortUrlGenerator(8) }; //save the model to database var result = _shortUrlRepository.Create(shortURLModel); //return the result return(new ShortUrlResponseModel { Model = result, Success = true, Message = "Saved successfully" }); }
public static ShortURLModel MapRequestModelToDBModel(ShortURLRequestModel requestModel) { ShortURLModel result = new ShortURLModel { LongURL = requestModel.LongURL }; result.ShortURL = TokenGenerator.GenerateShortUrl(); return(result); }
public ShortURLModel Create(ShortURLModel model) { try { _mongoCollection.InsertOne(model); return(model); } catch (Exception) { return(null); } }
public static ShortURLModel MapRequestModelToDBModel(ShortURLRequestModel requestModel) { ShortURLModel result = new ShortURLModel { DateOfCreation = DateTime.Now, ActualURL = requestModel.LongURL }; result.ShortenedURL = TokenGenerator.GenerateShortUrl(); return(result); }
/// <summary> /// check if the url exist in the database /// </summary> /// <param name="url">the url that must be checked</param> /// <returns></returns> private ShortURLModel URLExist(string url) { ShortURLModel foundUrl = _shortUrlRepository.GetByActualUrl(url); if (foundUrl != null) { return(null); } else { return(foundUrl); } }
// Request for share URL functionality like Aiim.ly/ijil public ActionResult ShortURL(string shortlink) { if (string.IsNullOrEmpty(shortlink)) { return(View("Index")); } string currenturl = Request.Url.GetLeftPart(UriPartial.Authority); if (currenturl.Contains(persiandomain)) { hosturl = persiandomain; } //var shorturlList = urlShortnerRepository.GetAllShortUrl(hosturl); var shorturlList = urlShortnerRepository.GetUrlByShortUrlKey(hosturl, shortlink); if (shorturlList == null) { return(View("Index")); } //if (!shorturlList.Contains(shortlink)) // return View("Index"); var path = Server.MapPath("~\\Ad\\" + shortlink + ".html"); string htmlContent = System.IO.File.ReadAllText(path); ShortURLModel model = new ShortURLModel { HtmlPage = htmlContent }; //Insert into UrlVisitorLog table for count visitors UrlVisitorLog visitorlog = new UrlVisitorLog() { UrlId = shorturlList.UrlId, RegistId = shorturlList.RegisId, VisitedDate = DateTime.Now.Date, ShortUrl = shorturlList.ShortUrlKey, OriginalUrl = shorturlList.OriginalUrl }; _urlVisitorLogRepository.AddUrl(visitorlog); _urlVisitorLogRepository.SaveAll(); return(View(model)); }
public IActionResult Get(string shorturl, [FromQuery(Name = "redirect")] bool redirect = true) { ShortURLModel shortUrl = shortUrlService.GetItemFromDataStore(shorturl); if (shortUrl != null) { if (redirect) { return(Redirect(shortUrl.LongURL)); } else { return(Ok(shortUrl)); } } return(NotFound()); }
public ShortUrlResponseModel SaveItemToDataStore(ShortURLRequestModel model) { ShortURLModel previouslySaved = shortUrlRepository.GetItemFromDataStoreByLongUrl(model.LongURL); if (previouslySaved != null) { return(new ShortUrlResponseModel { Model = previouslySaved, Success = true, Message = "This url has been saved previously" }); } else { ShortURLModel savedModel = shortUrlRepository.SaveItemToDataStore(ShortUrlModelMapper.MapRequestModelToDBModel(model)); return(new ShortUrlResponseModel { Model = savedModel, Success = true, Message = "Saved successfully" }); } }
//public ShortURLModel Update(string id, ShortURLModel model) //{ // var docId = new ObjectId(id); // _mongoCollection.ReplaceOne(m => m.Id == docId, model); // return model; //} public ShortURLModel Update(ShortURLModel model) { _mongoCollection.ReplaceOne(m => m.Id == model.Id, model); return(model); }