mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
Finished url shortening page.
This commit is contained in:
parent
dada112d84
commit
8e71dd6e2a
@ -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
|
||||
[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
|
||||
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
|
||||
[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();
|
||||
|
||||
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" });
|
||||
}
|
||||
|
||||
db.ShortenedUrls.Add(newUrl);
|
||||
db.SaveChanges();
|
||||
|
||||
return Json(new { result = new { shortUrl = newUrl.ShortUrl, originalUrl = url } });
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
public string OriginalUrl { get; set; }
|
||||
|
||||
public DateTime DateAdded { get; set; }
|
||||
|
||||
public int Views { get; set; }
|
||||
}
|
||||
}
|
31
Teknik/Areas/Shortener/Scripts/Shortener.js
Normal file
31
Teknik/Areas/Shortener/Scripts/Shortener.js
Normal file
@ -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">×</button>' + html.error + '</div>');
|
||||
}
|
||||
$('#url').focus();
|
||||
$('#url').select();
|
||||
}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
$('#url').on('input', function (e) {
|
||||
$("#top_msg").css('display', 'none', 'important');
|
||||
$("#top_msg").html('');
|
||||
});
|
||||
});
|
@ -24,6 +24,7 @@ namespace Teknik.Areas.Shortener
|
||||
ShortenedUrl newUrl = new ShortenedUrl();
|
||||
newUrl.OriginalUrl = url;
|
||||
newUrl.ShortUrl = shortUrl;
|
||||
newUrl.DateAdded = DateTime.Now;
|
||||
|
||||
return newUrl;
|
||||
}
|
||||
|
@ -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
|
||||
"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
|
||||
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"));
|
||||
}
|
||||
}
|
||||
}
|
@ -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">
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
@ -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…
Reference in New Issue
Block a user