Skip to content

Commit

Permalink
add missing documetation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeancarlo13 committed Dec 20, 2020
1 parent d3439eb commit e41af03
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 26 deletions.
26 changes: 15 additions & 11 deletions Controllers/ShortenerController.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using JCTools.Shortener.Models;
using JCTools.Shortener.Settings;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;

namespace JCTools.Shortener.Controllers
{
/// <summary>
/// The controller to be used to require the redirection to the real url
/// from the generate shortened urls
/// </summary>
[Authorize(Settings.Options.PolicyName)]
public class ShortenerController : Controller
{
/// <summary>
/// The name of the route template to be use by the <see cref="RedirectTo"> action
/// The name of the route template to be use by the <see cref="RedirectTo" /> action
/// </summary>
internal const string redirectToRouteName = "ShortenerController_RedirectTo";

Expand All @@ -25,17 +25,21 @@ public class ShortenerController : Controller
/// </summary>
private readonly ILogger<ShortenerController> _logger;
/// <summary>
/// the database context instance to be use
/// The database context instance to be used to review the url relationships
/// </summary>
private readonly IDatabaseContext _context;

/// <summary>
/// Initialize the controller instances
/// </summary>
/// <param name="loggerFactory">The logger factory instance to be used for log creation</param>
/// <param name="context">The database context instance to be used to review the url relationships</param>
public ShortenerController(
ILogger<ShortenerController> logger,
ILoggerFactory loggerFactory,
IDatabaseContext context
)
{
_logger = logger;
this._context = context;
_logger = loggerFactory.CreateLogger<ShortenerController>();
_context = context;
if (context.GetType().IsAssignableFrom(typeof(DbContext)))
throw new ArgumentException($"The argument {nameof(context)} not implement {typeof(DbContext).FullName}.", nameof(context));
}
Expand All @@ -46,7 +50,7 @@ IDatabaseContext context
/// <param name="token">The token to be use for the redirection</param>
/// <returns>The task to be execute</returns>
[HttpGet]
[Route("/{token}", Order = int.MaxValue, Name= redirectToRouteName)]
[Route("/{token}", Order = int.MaxValue, Name = redirectToRouteName)]
public async Task<IActionResult> RedirectTo(string token)
{
var link = await _context.ShortLinks.FirstOrDefaultAsync(sl => sl.Token == token);
Expand Down
3 changes: 1 addition & 2 deletions JCTools.Shortener.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Description>A simple links shortener for include in .net core projects</Description>
<Copyright></Copyright>

<PackageLicenseExpression>MIT</PackageLicenseExpression>

<PackageProjectUrl>https://github.com/jeancarlo13/JCTools.Shortener</PackageProjectUrl>
<RepositoryUrl>https://github.com/jeancarlo13/JCTools.Shortener</RepositoryUrl>
<PackageTags>.Net Core, Asp.Net Core, MVC, link, links, shortener, links shortener</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
34 changes: 34 additions & 0 deletions JCTools.Shortener.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JCTools.Shortener", "JCTools.Shortener.csproj", "{7452333C-19FF-46FB-967D-A9B1F0A84E55}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Debug|x64.ActiveCfg = Debug|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Debug|x64.Build.0 = Debug|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Debug|x86.ActiveCfg = Debug|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Debug|x86.Build.0 = Debug|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Release|Any CPU.Build.0 = Release|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Release|x64.ActiveCfg = Release|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Release|x64.Build.0 = Release|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Release|x86.ActiveCfg = Release|Any CPU
{7452333C-19FF-46FB-967D-A9B1F0A84E55}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions Models/ShortLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace JCTools.Shortener.Models
{
/// <summary>
/// The model to be used for stored the relation
/// between the shortener token and the real URL to be shorted
/// </summary>
[Table("Short_Link")]
public class ShortLink
{
Expand Down
5 changes: 4 additions & 1 deletion Services/ILinkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace JCTools.Shortener.Services
{
/// <summary>
/// Defines the available methods for the generation of shortened tokens
/// and their relations with the real URLS
/// </summary>
public interface ILinkGenerator
{

/// <summary>
/// Generate and save a short link with a random token
/// </summary>
Expand Down
17 changes: 15 additions & 2 deletions Services/LinkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using JCTools.Shortener.Models;
using JCTools.Shortener.Settings;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
Expand All @@ -12,14 +11,28 @@

namespace JCTools.Shortener.Services
{
/// <summary>
/// Implements the available methods for the generation of shortened tokens
/// and their relations with the real URLS
/// </summary>
/// <typeparam name="TDatabaseContext">The database context type to be used to store the relationships between the real urls and the shortened tokens </typeparam>
internal class LinkGenerator<TDatabaseContext> : ILinkGenerator
where TDatabaseContext : DbContext, IDatabaseContext
{
/// <summary>
/// the database context to be use
/// The database context to be used to store the relationships between the real urls and the shortened tokens
/// </summary>
private readonly TDatabaseContext _context;
/// <summary>
/// The <see cref="IUrlHelper" /> instance to be used to build URLs within an application.
/// </summary>
private readonly IUrlHelper _urlHelper;
/// <summary>
/// Initializes an instance
/// </summary>
/// <param name="context">The database context to be used to store the relationships between the real urls and the shortened tokens </param>
/// <param name="urlHelperFactory">The <see cref="IUrlHelperFactory" /> instance to be used to build the required <see cref="IUrlHelper" /> instance</param>
/// <param name="actionContextAccessor">The instance of the action context accessor required for the generation of the required <see cref="IUrlHelper" /> instance</param>
public LinkGenerator(
TDatabaseContext context,
IUrlHelperFactory urlHelperFactory,
Expand Down
3 changes: 3 additions & 0 deletions Settings/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace JCTools.Shortener.Settings
{
/// <summary>
/// Defines all the package settings that are used to suit the application where it will be used
/// </summary>
public class Options
{
/// <summary>
Expand Down
23 changes: 13 additions & 10 deletions StartupExtensors.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JCTools.Shortener.Settings;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using JCTools.Shortener.Services;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace JCTools.Shortener
{
/// <summary>
/// Add the available extensors methods for the configuration and initialization of the package
/// </summary>
public static class StartupExtensors
{
/// <summary>
/// The options to be use for the generation short link process
/// </summary>
internal static Options Options;

/// <summary>
/// Allows registry the required services for the correctly functioning of the package
/// </summary>
/// <typeparam name="TDatabaseContext">The type of the database context to be used for stored the generated links</typeparam>
/// <param name="services">The application services collection to be used for the services register</param>
/// <param name="optionsFactory">Action to be invoke for get the configured options of the package</param>
/// <returns>The modified app services collection</returns>
public static IServiceCollection AddLinksShortener<TDatabaseContext>(
this IServiceCollection services,
Action<Options> optionsActions = null
Action<Options> optionsFactory = null
)
where TDatabaseContext : DbContext, IDatabaseContext
{
Options = new Options();
optionsActions?.Invoke(Options);
optionsFactory?.Invoke(Options);

services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddTransient<ILinkGenerator, LinkGenerator<TDatabaseContext>>();
Expand Down

0 comments on commit e41af03

Please sign in to comment.