From 289b4308360e9c3db6f85a77903ac93d4ad70ecc Mon Sep 17 00:00:00 2001
From: Uncled1023
Date: Wed, 24 Feb 2016 13:53:14 -0800
Subject: [PATCH] - Added Url Shortening API. - Fixed wrong Paste API Result -
Added help for Url Shortening API
---
Teknik/Areas/API/APIAreaRegistration.cs | 10 +++
.../Areas/API/Controllers/APIv1Controller.cs | 35 +++++++-
Teknik/Areas/Help/Views/Help/API/API.cshtml | 1 +
.../Areas/Help/Views/Help/API/v1/Paste.cshtml | 2 +-
.../Help/Views/Help/API/v1/Shorten.cshtml | 81 +++++++++++++++++++
.../Controllers/ShortenerController.cs | 5 +-
Teknik/Areas/Shortener/Scripts/Shortener.js | 7 +-
Teknik/Teknik.csproj | 1 +
8 files changed, 134 insertions(+), 8 deletions(-)
create mode 100644 Teknik/Areas/Help/Views/Help/API/v1/Shorten.cshtml
diff --git a/Teknik/Areas/API/APIAreaRegistration.cs b/Teknik/Areas/API/APIAreaRegistration.cs
index c5f9b4f..7fc52d4 100644
--- a/Teknik/Areas/API/APIAreaRegistration.cs
+++ b/Teknik/Areas/API/APIAreaRegistration.cs
@@ -36,6 +36,7 @@ namespace Teknik.Areas.API
new { controller = "APIv1", action = "Upload" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
+ // Pastes
context.MapSubdomainRoute(
"API.v1.Paste", // Route name
new List() { "api" },
@@ -44,6 +45,15 @@ namespace Teknik.Areas.API
new { controller = "APIv1", action = "Paste" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
+ // Url Shortening
+ context.MapSubdomainRoute(
+ "API.v1.Shortener", // Route name
+ new List() { "api" },
+ new List() { config.Host },
+ "v1/Shorten", // URL with parameters
+ new { controller = "APIv1", action = "Shorten" }, // Parameter defaults
+ new[] { typeof(Controllers.APIv1Controller).Namespace }
+ );
#endregion
// Default Routing
diff --git a/Teknik/Areas/API/Controllers/APIv1Controller.cs b/Teknik/Areas/API/Controllers/APIv1Controller.cs
index 6804789..d1df9d5 100644
--- a/Teknik/Areas/API/Controllers/APIv1Controller.cs
+++ b/Teknik/Areas/API/Controllers/APIv1Controller.cs
@@ -11,6 +11,7 @@ using Teknik.Controllers;
using Teknik.Helpers;
using Teknik.Models;
using System.Text;
+using Teknik.Areas.Shortener.Models;
namespace Teknik.Areas.API.Controllers
{
@@ -132,8 +133,11 @@ namespace Teknik.Areas.API.Controllers
db.Pastes.Add(paste);
db.SaveChanges();
-
- return Json(new { result = new {
+
+ return Json(new
+ {
+ result = new
+ {
id = paste.Url,
url = Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = password }),
title = paste.Title,
@@ -148,5 +152,32 @@ namespace Teknik.Areas.API.Controllers
return Json(new { error = new { message = "Exception: " + ex.Message } });
}
}
+
+ public ActionResult Shorten(string url)
+ {
+ if (url.IsValidUrl())
+ {
+ ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
+
+ db.ShortenedUrls.Add(newUrl);
+ db.SaveChanges();
+
+ string shortUrl = Url.SubRouteUrl(string.Empty, "Shortener.View", new { url = newUrl.ShortUrl });
+ if (Config.DevEnvironment)
+ {
+ shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
+ }
+
+ return Json(new
+ {
+ result = new
+ {
+ shortUrl = shortUrl,
+ originalUrl = url
+ }
+ });
+ }
+ return Json(new { error = new { message = "Must be a valid Url" } });
+ }
}
}
\ No newline at end of file
diff --git a/Teknik/Areas/Help/Views/Help/API/API.cshtml b/Teknik/Areas/Help/Views/Help/API/API.cshtml
index 9d7b890..d49b8e4 100644
--- a/Teknik/Areas/Help/Views/Help/API/API.cshtml
+++ b/Teknik/Areas/Help/Views/Help/API/API.cshtml
@@ -11,6 +11,7 @@
diff --git a/Teknik/Areas/Help/Views/Help/API/v1/Paste.cshtml b/Teknik/Areas/Help/Views/Help/API/v1/Paste.cshtml
index 04b5692..b9e1a9c 100644
--- a/Teknik/Areas/Help/Views/Help/API/v1/Paste.cshtml
+++ b/Teknik/Areas/Help/Views/Help/API/v1/Paste.cshtml
@@ -132,7 +132,7 @@
Response
- {"result":{"id":id "url":"url", "title":"title", "syntax":"auto-detect", "expiration":"datetime", "password":"password"}}
+ {"result":{"id":id, "url":"url", "title":"title", "syntax":"auto-detect", "expiration":"datetime", "password":"password"}}
diff --git a/Teknik/Areas/Help/Views/Help/API/v1/Shorten.cshtml b/Teknik/Areas/Help/Views/Help/API/v1/Shorten.cshtml
new file mode 100644
index 0000000..f0c2cc1
--- /dev/null
+++ b/Teknik/Areas/Help/Views/Help/API/v1/Shorten.cshtml
@@ -0,0 +1,81 @@
+@model Teknik.Areas.Help.ViewModels.HelpViewModel
+
+@Styles.Render("~/Content/help");
+
+
+
+
Url Shortening Service
+
+
This is a description of the API commands available for the Url Shortening service.
+
Shorten a Url
+
POST @Url.SubRouteUrl("api", "API.v1.Shortener")
+
Parameters
+
+
+
+ Name |
+ Type |
+ Default |
+ Description |
+
+
+
+
+
+ url
+ |
+
+ string
+ |
+
+ NULL
+ |
+
+ Required
+ The url you want to shorten. This url must be an absolute url.
+ |
+
+
+
+
Response
+
{"result":{"shortUrl":"shortened url", "originalUrl":"original url"}}
+
+
+
+ Name |
+ Type |
+ Description |
+
+
+
+
+
+ shortUrl
+ |
+
+ string
+ |
+
+ The shortened url.
+ |
+
+
+
+ originalUrl
+ |
+
+ string
+ |
+
+ The original url that was shortened.
+ |
+
+
+
+
Example
+
$ curl --data "url=http://www.example.com/long/url/is/long" @Url.SubRouteUrl("api", "API.v1.Shortener")
+
+ This will shorten the supplied url.
+
+
+
diff --git a/Teknik/Areas/Shortener/Controllers/ShortenerController.cs b/Teknik/Areas/Shortener/Controllers/ShortenerController.cs
index fda372c..108d6b9 100644
--- a/Teknik/Areas/Shortener/Controllers/ShortenerController.cs
+++ b/Teknik/Areas/Shortener/Controllers/ShortenerController.cs
@@ -64,10 +64,7 @@ namespace Teknik.Areas.Shortener.Controllers
return Json(new { result = new { shortUrl = shortUrl, originalUrl = url } });
}
- else
- {
- return Json(new { error = "Must be a valid Url" });
- }
+ return Json(new { error = "Must be a valid Url" });
}
}
}
\ No newline at end of file
diff --git a/Teknik/Areas/Shortener/Scripts/Shortener.js b/Teknik/Areas/Shortener/Scripts/Shortener.js
index 4e6b660..64bcad3 100644
--- a/Teknik/Areas/Shortener/Scripts/Shortener.js
+++ b/Teknik/Areas/Shortener/Scripts/Shortener.js
@@ -14,8 +14,13 @@
$('#url').val(html.result.shortUrl);
}
else {
+ var errorMsg = "Invalid Url";
+ if (html.error)
+ {
+ errorMsg = html.error;
+ }
$("#top_msg").css('display', 'inline', 'important');
- $("#top_msg").html('' + html.error + '
');
+ $("#top_msg").html('' + errorMsg + '
');
}
$('#url').focus();
$('#url').select();
diff --git a/Teknik/Teknik.csproj b/Teknik/Teknik.csproj
index 45ecbf5..4220fca 100644
--- a/Teknik/Teknik.csproj
+++ b/Teknik/Teknik.csproj
@@ -485,6 +485,7 @@
+