1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

- Added Url Shortening API.

- Fixed wrong Paste API Result
- Added help for Url Shortening API
This commit is contained in:
Uncled1023 2016-02-24 13:53:14 -08:00
parent f848ff9f42
commit 289b430836
8 changed files with 134 additions and 8 deletions

View File

@ -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<string>() { "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<string>() { "api" },
new List<string>() { config.Host },
"v1/Shorten", // URL with parameters
new { controller = "APIv1", action = "Shorten" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
#endregion
// Default Routing

View File

@ -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" } });
}
}
}

View File

@ -11,6 +11,7 @@
<ul class="list-unstyled">
<li><a href="@Url.SubRouteUrl("help", "Help.API", new { version = "v1", service = "Upload" })">Upload Service</a></li>
<li><a href="@Url.SubRouteUrl("help", "Help.API", new { version = "v1", service = "Paste" })">Paste Service</a></li>
<li><a href="@Url.SubRouteUrl("help", "Help.API", new { version = "v1", service = "Shorten" })">Url Shortening Service</a></li>
</ul>
</p>
</div>

View File

@ -132,7 +132,7 @@
</tbody>
</table>
<h4>Response</h4>
<pre><code>{"result":{"id":<var>id</var> "url":"<var>url</var>", "title":"<var>title</var>", "syntax":"<var>auto-detect</var>", "expiration":"<var>datetime</var>", "password":"<var>password</var>"}}</code></pre>
<pre><code>{"result":{"id":<var>id</var>, "url":"<var>url</var>", "title":"<var>title</var>", "syntax":"<var>auto-detect</var>", "expiration":"<var>datetime</var>", "password":"<var>password</var>"}}</code></pre>
<table>
<thead>
<tr>

View File

@ -0,0 +1,81 @@
@model Teknik.Areas.Help.ViewModels.HelpViewModel
@Styles.Render("~/Content/help");
<div class="container">
<div class="row api">
<h2><b>Url Shortening Service</b></h2>
<hr>
<p>This is a description of the API commands available for the Url Shortening service.</p>
<h3>Shorten a Url</h3>
<pre><code>POST @Url.SubRouteUrl("api", "API.v1.Shortener")</code></pre>
<h4>Parameters</h4>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>url</code>
</td>
<td>
<code>string</code>
</td>
<td>
<var>NULL</var>
</td>
<td>
<strong>Required</strong>
The url you want to shorten. This url must be an absolute url.
</td>
</tr>
</tbody>
</table>
<h4>Response</h4>
<pre><code>{"result":{"shortUrl":"<var>shortened url</var>", "originalUrl":"<var>original url</var>"}}</code></pre>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>shortUrl</code>
</td>
<td>
<code>string</code>
</td>
<td>
The shortened url.
</td>
</tr>
<tr>
<td>
<code>originalUrl</code>
</td>
<td>
<code>string</code>
</td>
<td>
The original url that was shortened.
</td>
</tr>
</tbody>
</table>
<h4>Example</h4>
<pre><code>$ curl --data "url=http://www.example.com/long/url/is/long" @Url.SubRouteUrl("api", "API.v1.Shortener")</code></pre>
<p>
This will shorten the supplied url.
</p>
</div>
</div>

View File

@ -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" });
}
}
}

View File

@ -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('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + html.error + '</div>');
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + errorMsg + '</div>');
}
$('#url').focus();
$('#url').select();

View File

@ -485,6 +485,7 @@
<Content Include="Areas\Shortener\Views\web.config" />
<Content Include="Areas\Shortener\Views\_ViewStart.cshtml" />
<Content Include="Areas\Shortener\Views\Shortener\Index.cshtml" />
<Content Include="Areas\Help\Views\Help\API\v1\Shorten.cshtml" />
<None Include="Properties\PublishProfiles\Teknik Dev.pubxml" />
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
<None Include="Scripts\jquery-2.1.4.intellisense.js" />