diff --git a/Teknik/Areas/Shortener/Controllers/ShortenerController.cs b/Teknik/Areas/Shortener/Controllers/ShortenerController.cs index 1914ab3..a6ab95f 100644 --- a/Teknik/Areas/Shortener/Controllers/ShortenerController.cs +++ b/Teknik/Areas/Shortener/Controllers/ShortenerController.cs @@ -31,10 +31,24 @@ namespace Teknik.Areas.Shortener.Controllers } [HttpPost] + [AllowAnonymous] public ActionResult ShortenUrl(string url) { + ShortenedUrl newUrl = Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength); - return Json(new { result = true }); + if (User.Identity.IsAuthenticated) + { + Profile.Models.User foundUser = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault(); + if (foundUser != null) + { + newUrl.UserId = foundUser.UserId; + } + } + + db.ShortenedUrls.Add(newUrl); + db.SaveChanges(); + + return Json(new { result = new { shortUrl = newUrl.ShortUrl, originalUrl = url } }); } } } \ No newline at end of file diff --git a/Teknik/Areas/Shortener/Shortener.cs b/Teknik/Areas/Shortener/Shortener.cs new file mode 100644 index 0000000..9840469 --- /dev/null +++ b/Teknik/Areas/Shortener/Shortener.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Teknik.Areas.Shortener.Models; +using Teknik.Models; + +namespace Teknik.Areas.Shortener +{ + public static class Shortener + { + public static ShortenedUrl ShortenUrl(string url, int length) + { + TeknikEntities db = new TeknikEntities(); + + // Generate the shortened url + string shortUrl = Utility.RandomString(length); + while (db.ShortenedUrls.Where(s => s.ShortUrl == shortUrl).FirstOrDefault() != null) + { + shortUrl = Utility.RandomString(length); + } + + ShortenedUrl newUrl = new ShortenedUrl(); + newUrl.OriginalUrl = url; + newUrl.ShortUrl = shortUrl; + + return newUrl; + } + } +} diff --git a/Teknik/Areas/Shortener/ShortenerAreaRegistration.cs b/Teknik/Areas/Shortener/ShortenerAreaRegistration.cs index 55f7753..1600ca9 100644 --- a/Teknik/Areas/Shortener/ShortenerAreaRegistration.cs +++ b/Teknik/Areas/Shortener/ShortenerAreaRegistration.cs @@ -25,6 +25,14 @@ namespace Teknik.Areas.Shortener new { controller = "Shortener", action = "Index" }, // Parameter defaults new[] { typeof(Controllers.ShortenerController).Namespace } ); + context.MapSubdomainRoute( + "Shortener.Action", // Route name + new List() { "dev", "shorten", "s" }, // Subdomains + new List() { config.Host }, // domains + "Action/{controller}/{action}", // URL with parameters + new { controller = "Shortener", action = "Index" }, // Parameter defaults + new[] { typeof(Controllers.ShortenerController).Namespace } + ); context.MapSubdomainRoute( "Shortener.View", // Route name new List() { "dev", "*" }, // Subdomains diff --git a/Teknik/Configuration/ShortenerConfig.cs b/Teknik/Configuration/ShortenerConfig.cs index 8f1ff24..d3b604e 100644 --- a/Teknik/Configuration/ShortenerConfig.cs +++ b/Teknik/Configuration/ShortenerConfig.cs @@ -9,9 +9,12 @@ namespace Teknik.Configuration { public string ShortenerHost { get; set; } + public int UrlLength { get; set; } + public ShortenerConfig() { ShortenerHost = string.Empty; + UrlLength = 4; } } } \ No newline at end of file diff --git a/Teknik/Teknik.csproj b/Teknik/Teknik.csproj index 2485605..de57d5c 100644 --- a/Teknik/Teknik.csproj +++ b/Teknik/Teknik.csproj @@ -210,6 +210,7 @@ +