Browse Source

Finished url shortening page.

master
Teknikode 7 years ago
parent
commit
8e71dd6e2a
  1. 35
      Teknik/Areas/Shortener/Controllers/ShortenerController.cs
  2. 4
      Teknik/Areas/Shortener/Models/ShortenedUrl.cs
  3. 31
      Teknik/Areas/Shortener/Scripts/Shortener.js
  4. 1
      Teknik/Areas/Shortener/Shortener.cs
  5. 7
      Teknik/Areas/Shortener/ShortenerAreaRegistration.cs
  6. 12
      Teknik/Areas/Shortener/Views/Shortener/Index.cshtml
  7. 11
      Teknik/Helpers/UrlExtensions.cs
  8. BIN
      Teknik/Scripts/_references.js
  9. 1
      Teknik/Teknik.csproj

35
Teknik/Areas/Shortener/Controllers/ShortenerController.cs

@ -4,6 +4,7 @@ using System.Linq; @@ -4,6 +4,7 @@ using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teknik.Areas.Shortener.Models;
using Teknik.Areas.Shortener.ViewModels;
using Teknik.Controllers;
using Teknik.Models;
@ -16,7 +17,9 @@ namespace Teknik.Areas.Shortener.Controllers @@ -16,7 +17,9 @@ namespace Teknik.Areas.Shortener.Controllers
[AllowAnonymous]
public ActionResult Index()
{
return View();
ViewBag.Title = "Url Shortener - " + Config.Title;
ShortenViewModel model = new ShortenViewModel();
return View(model);
}
[AllowAnonymous]
@ -25,6 +28,9 @@ namespace Teknik.Areas.Shortener.Controllers @@ -25,6 +28,9 @@ namespace Teknik.Areas.Shortener.Controllers
ShortenedUrl shortUrl = db.ShortenedUrls.Where(s => s.ShortUrl == url).FirstOrDefault();
if (shortUrl != null)
{
shortUrl.Views += 1;
db.Entry(shortUrl).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToUrl(shortUrl.OriginalUrl);
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
@ -34,21 +40,28 @@ namespace Teknik.Areas.Shortener.Controllers @@ -34,21 +40,28 @@ namespace Teknik.Areas.Shortener.Controllers
[AllowAnonymous]
public ActionResult ShortenUrl(string url)
{
ShortenedUrl newUrl = Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
if (User.Identity.IsAuthenticated)
if (url.IsValidUrl())
{
Profile.Models.User foundUser = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
if (foundUser != null)
ShortenedUrl newUrl = Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
if (User.Identity.IsAuthenticated)
{
newUrl.UserId = foundUser.UserId;
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();
db.ShortenedUrls.Add(newUrl);
db.SaveChanges();
return Json(new { result = new { shortUrl = newUrl.ShortUrl, originalUrl = url } });
return Json(new { result = new { shortUrl = string.Format("http://{0}/{1}", Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl), originalUrl = url } });
}
else
{
return Json(new { error = "Must be a valid HTTP or HTTPS Url" });
}
}
}
}

4
Teknik/Areas/Shortener/Models/ShortenedUrl.cs

@ -8,7 +8,7 @@ namespace Teknik.Areas.Shortener.Models @@ -8,7 +8,7 @@ namespace Teknik.Areas.Shortener.Models
{
public class ShortenedUrl
{
public int ShortenId { get; set; }
public int ShortenedUrlId { get; set; }
public int? UserId { get; set; }
@ -19,5 +19,7 @@ namespace Teknik.Areas.Shortener.Models @@ -19,5 +19,7 @@ namespace Teknik.Areas.Shortener.Models
public string OriginalUrl { get; set; }
public DateTime DateAdded { get; set; }
public int Views { get; set; }
}
}

31
Teknik/Areas/Shortener/Scripts/Shortener.js

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
$(document).ready(function () {
$('#url').focus();
$("#shortenSubmit").click(function () {
$("#top_msg").css('display', 'none', 'important');
$("#top_msg").html('');
url = $("#url").val();
$.ajax({
type: "POST",
url: $("#shortenerForm").attr('action'),
data: { url: url },
success: function (html) {
if (html.result) {
$('#url').val(html.result.shortUrl);
}
else {
$("#top_msg").css('display', 'inline', 'important');
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + html.error + '</div>');
}
$('#url').focus();
$('#url').select();
}
});
return false;
});
$('#url').on('input', function (e) {
$("#top_msg").css('display', 'none', 'important');
$("#top_msg").html('');
});
});

1
Teknik/Areas/Shortener/Shortener.cs

@ -24,6 +24,7 @@ namespace Teknik.Areas.Shortener @@ -24,6 +24,7 @@ namespace Teknik.Areas.Shortener
ShortenedUrl newUrl = new ShortenedUrl();
newUrl.OriginalUrl = url;
newUrl.ShortUrl = shortUrl;
newUrl.DateAdded = DateTime.Now;
return newUrl;
}

7
Teknik/Areas/Shortener/ShortenerAreaRegistration.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Optimization;
using Teknik.Configuration;
namespace Teknik.Areas.Shortener
@ -29,7 +30,7 @@ namespace Teknik.Areas.Shortener @@ -29,7 +30,7 @@ namespace Teknik.Areas.Shortener
"Shortener.Action", // Route name
new List<string>() { "dev", "shorten", "s" }, // Subdomains
new List<string>() { config.Host }, // domains
"Action/{controller}/{action}", // URL with parameters
"Action/{action}", // URL with parameters
new { controller = "Shortener", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.ShortenerController).Namespace }
);
@ -41,6 +42,10 @@ namespace Teknik.Areas.Shortener @@ -41,6 +42,10 @@ namespace Teknik.Areas.Shortener
new { controller = "Shortener", action = "RedirectToUrl" }, // Parameter defaults
new[] { typeof(Controllers.ShortenerController).Namespace }
);
// Register Script Bundles
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/shortener").Include(
"~/Areas/Shortener/Scripts/Shortener.js"));
}
}
}

12
Teknik/Areas/Shortener/Views/Shortener/Index.cshtml

@ -1,17 +1,21 @@ @@ -1,17 +1,21 @@
@model Teknik.Areas.Shortener.ViewModels.ShortenViewModel
@Scripts.Render("~/bundles/shortener")
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="col-sm-12 text-center">
<h1>Shorten a Url</h1>
</div>
</div>
<br />
<br />
<div class="row">
<div class="col-sm-12 text-center">
<form class="form-horizontal" name="shortenerForm" method="post" action="@Url.SubRouteUrl("shortener", "Shortener.Action", new { action = "ShortenUrl" })">
<form class="form-horizontal" name="shortenerForm" id="shortenerForm" method="post" action="@Url.SubRouteUrl("shorten", "Shortener.Action", new { action = "ShortenUrl" })">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-1">
<input type="text" class="form-control" name="Url" id="url" placeholder="https://www.example.com/really/long/url">
<input type="text" class="form-control input-lg" name="Url" id="url" placeholder="https://www.example.com/really/long/url" style="text-align:center;">
</div>
</div>
<div class="form-group">

11
Teknik/Helpers/UrlExtensions.cs

@ -170,5 +170,16 @@ namespace Teknik @@ -170,5 +170,16 @@ namespace Teknik
}
return string.Empty;
}
public static bool IsValidUrl(this string url)
{
Uri uriResult;
bool result = Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uriResult);
if (result)
{
result = uriResult.IsWellFormedOriginalString();
}
return result;
}
}
}

BIN
Teknik/Scripts/_references.js

Binary file not shown.

1
Teknik/Teknik.csproj

@ -290,6 +290,7 @@ @@ -290,6 +290,7 @@
<Content Include="Areas\Podcast\Content\Podcast.css" />
<Content Include="Areas\Podcast\Scripts\Podcast.js" />
<Content Include="Areas\Profile\Scripts\Profile.js" />
<Content Include="Areas\Shortener\Scripts\Shortener.js" />
<Content Include="Areas\Upload\Scripts\Download.js" />
<Content Include="Content\audioplayer.css" />
<Content Include="Content\bootstrap-switch\bootstrap2\bootstrap-switch.css" />

Loading…
Cancel
Save