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:
parent
f848ff9f42
commit
289b430836
@ -36,6 +36,7 @@ namespace Teknik.Areas.API
|
|||||||
new { controller = "APIv1", action = "Upload" }, // Parameter defaults
|
new { controller = "APIv1", action = "Upload" }, // Parameter defaults
|
||||||
new[] { typeof(Controllers.APIv1Controller).Namespace }
|
new[] { typeof(Controllers.APIv1Controller).Namespace }
|
||||||
);
|
);
|
||||||
|
// Pastes
|
||||||
context.MapSubdomainRoute(
|
context.MapSubdomainRoute(
|
||||||
"API.v1.Paste", // Route name
|
"API.v1.Paste", // Route name
|
||||||
new List<string>() { "api" },
|
new List<string>() { "api" },
|
||||||
@ -44,6 +45,15 @@ namespace Teknik.Areas.API
|
|||||||
new { controller = "APIv1", action = "Paste" }, // Parameter defaults
|
new { controller = "APIv1", action = "Paste" }, // Parameter defaults
|
||||||
new[] { typeof(Controllers.APIv1Controller).Namespace }
|
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
|
#endregion
|
||||||
|
|
||||||
// Default Routing
|
// Default Routing
|
||||||
|
@ -11,6 +11,7 @@ using Teknik.Controllers;
|
|||||||
using Teknik.Helpers;
|
using Teknik.Helpers;
|
||||||
using Teknik.Models;
|
using Teknik.Models;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Teknik.Areas.Shortener.Models;
|
||||||
|
|
||||||
namespace Teknik.Areas.API.Controllers
|
namespace Teknik.Areas.API.Controllers
|
||||||
{
|
{
|
||||||
@ -132,8 +133,11 @@ namespace Teknik.Areas.API.Controllers
|
|||||||
|
|
||||||
db.Pastes.Add(paste);
|
db.Pastes.Add(paste);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
|
||||||
return Json(new { result = new {
|
return Json(new
|
||||||
|
{
|
||||||
|
result = new
|
||||||
|
{
|
||||||
id = paste.Url,
|
id = paste.Url,
|
||||||
url = Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = password }),
|
url = Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = password }),
|
||||||
title = paste.Title,
|
title = paste.Title,
|
||||||
@ -148,5 +152,32 @@ namespace Teknik.Areas.API.Controllers
|
|||||||
return Json(new { error = new { message = "Exception: " + ex.Message } });
|
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" } });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,7 @@
|
|||||||
<ul class="list-unstyled">
|
<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 = "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 = "Paste" })">Paste Service</a></li>
|
||||||
|
<li><a href="@Url.SubRouteUrl("help", "Help.API", new { version = "v1", service = "Shorten" })">Url Shortening Service</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -132,7 +132,7 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<h4>Response</h4>
|
<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>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
81
Teknik/Areas/Help/Views/Help/API/v1/Shorten.cshtml
Normal file
81
Teknik/Areas/Help/Views/Help/API/v1/Shorten.cshtml
Normal 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>
|
@ -64,10 +64,7 @@ namespace Teknik.Areas.Shortener.Controllers
|
|||||||
|
|
||||||
return Json(new { result = new { shortUrl = shortUrl, originalUrl = url } });
|
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" });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,8 +14,13 @@
|
|||||||
$('#url').val(html.result.shortUrl);
|
$('#url').val(html.result.shortUrl);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
var errorMsg = "Invalid Url";
|
||||||
|
if (html.error)
|
||||||
|
{
|
||||||
|
errorMsg = html.error;
|
||||||
|
}
|
||||||
$("#top_msg").css('display', 'inline', 'important');
|
$("#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>');
|
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + errorMsg + '</div>');
|
||||||
}
|
}
|
||||||
$('#url').focus();
|
$('#url').focus();
|
||||||
$('#url').select();
|
$('#url').select();
|
||||||
|
@ -485,6 +485,7 @@
|
|||||||
<Content Include="Areas\Shortener\Views\web.config" />
|
<Content Include="Areas\Shortener\Views\web.config" />
|
||||||
<Content Include="Areas\Shortener\Views\_ViewStart.cshtml" />
|
<Content Include="Areas\Shortener\Views\_ViewStart.cshtml" />
|
||||||
<Content Include="Areas\Shortener\Views\Shortener\Index.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 Dev.pubxml" />
|
||||||
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
|
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
|
||||||
<None Include="Scripts\jquery-2.1.4.intellisense.js" />
|
<None Include="Scripts\jquery-2.1.4.intellisense.js" />
|
||||||
|
Loading…
Reference in New Issue
Block a user