public ActionResult Create(ShortenedUrl shortUrl) { ModelError shortTokenError = EnsureValidShortToken(shortUrl); if (shortTokenError != null) { ModelState.AddModelError("ShortToken", shortTokenError.ErrorMessage); } if (ModelState.IsValid) { shortUrl.TargetUrl = EnsureHasHttpScheme(shortUrl.TargetUrl); if (IsValidUri(shortUrl.TargetUrl) == false) { ModelState.AddModelError("TargetUrl", "That isn't a valid URL"); return View(shortUrl); } shortUrl.CreatedBy = HttpContext.User.Identity.Name; shortUrl.CreatedOnUTC = DateTime.UtcNow; webApi.CreateNewUrl(shortUrl); return RedirectToAction("Index"); } else { return View("Index", shortUrl); } }
/// <summary> /// Ensure the supplied short token isn't taken and or is URL encoded. /// </summary> /// <param name="shortUrl"></param> public ModelError EnsureValidShortToken(ShortenedUrl shortUrl) { if (shortUrl.ShortToken.IsNullOrWhiteSpace()) { do { shortUrl.ShortToken = Data.RandomStringGenerator.GenerateRandomString(4); } while (ShortTokenIsAvailable(shortUrl.ShortToken) == false); } else { if (ShortTokenIsAvailable(shortUrl.ShortToken) == false) { return new ModelError("Token [{0}] has already been taken.".FormatWith(shortUrl.ShortToken)); } } return null; }
public void Update(ShortenedUrl shortUrl) { repo.Update(shortUrl); }
public void Delete(ShortenedUrl shortUrl) { repo.Remove(shortUrl); }
public void CreateNewUrl(ShortenedUrl shortUrl) { repo.Save(shortUrl); }
private void IncrementVisitCount(ShortenedUrl shortUrl) { shortUrl.VisitsCount += 1; webApi.Update(shortUrl); }
public void Update(ShortenedUrl shortUrl) { db.Update(shortUrl); }
public void Save(ShortenedUrl shortened) { db.Add(shortened); }
public void Remove(ShortenedUrl shortUrl) { db.Remove(shortUrl); }