Exemple #1
0
        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); }
        }
Exemple #2
0
        /// <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);
 }
Exemple #6
0
 private void IncrementVisitCount(ShortenedUrl shortUrl)
 {
     shortUrl.VisitsCount += 1;
     webApi.Update(shortUrl);
 }
Exemple #7
0
 public void Update(ShortenedUrl shortUrl)
 {
     db.Update(shortUrl);
 }
Exemple #8
0
 public void Save(ShortenedUrl shortened)
 {
     db.Add(shortened);
 }
Exemple #9
0
 public void Remove(ShortenedUrl shortUrl)
 {
     db.Remove(shortUrl);
 }