Skip to content

Commit

Permalink
Merge pull request #1 from neekgreen/updates
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
Nicholas Myers authored Jul 27, 2017
2 parents 4975118 + d95d4b1 commit cb153a4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
namespace PaginableCollections.AspNetCore
{
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

public class CondensedActionFilter : IAsyncActionFilter
public class CondensedHeadersActionFilter : IAsyncActionFilter
{
private const string HeaderPrefix = "X-Paginable";

private readonly IOptions<MvcJsonOptions> options;

public CondensedActionFilter(IOptions<MvcJsonOptions> options)
public CondensedHeadersActionFilter(IOptions<MvcJsonOptions> options)
{
this.options = options;
}
Expand All @@ -22,9 +21,7 @@ async Task IAsyncActionFilter.OnActionExecutionAsync(ActionExecutingContext cont
{
await next();

var paginable = (context.Result as ObjectResult)?.Value as IPaginable;

if (paginable != null)
if ((context.Result as ObjectResult)?.Value is IPaginable paginable)
{
context.HttpContext.Response.Headers.Add(
HeaderPrefix,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
namespace PaginableCollections.AspNetCore
{
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

public class ExpandedActionFilter : IAsyncActionFilter
public class ExpandedHeadersActionFilter : IAsyncActionFilter
{
private const string HeaderPrefix = "X-Paginable";

async Task IAsyncActionFilter.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await next();

var paginable = (context.Result as ObjectResult)?.Value as IPaginable;

if (paginable != null)
if ((context.Result as ObjectResult)?.Value is IPaginable paginable)
{
context.HttpContext.Response.Headers.Add($"{HeaderPrefix}-PageNumber", paginable.PageNumber.ToString());
context.HttpContext.Response.Headers.Add($"{HeaderPrefix}-ItemCountPerPage", paginable.ItemCountPerPage.ToString());
Expand Down
8 changes: 8 additions & 0 deletions src/PaginableCollections.AspNetCore/HeaderFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PaginableCollections.AspNetCore
{
public enum HeaderFormat
{
Condensed = 0,
Expanded = 1
}
}
23 changes: 23 additions & 0 deletions src/PaginableCollections.AspNetCore/HeaderFormatOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace PaginableCollections.AspNetCore
{
public class HeaderFormatOptions
{
public HeaderFormatOptions() { }


public HeaderFormat HeaderFormat { get; set; } = HeaderFormat.Condensed;


public HeaderFormatOptions UseCondensed()
{
HeaderFormat = HeaderFormat.Condensed;
return this;
}

public HeaderFormatOptions UseExpanded()
{
HeaderFormat = HeaderFormat.Expanded;
return this;
}
}
}
29 changes: 0 additions & 29 deletions src/PaginableCollections.AspNetCore/HeaderOptions.cs

This file was deleted.

20 changes: 14 additions & 6 deletions src/PaginableCollections.AspNetCore/MvcBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@

public static class MvcBuilderExtensions
{
public static IMvcBuilder UsePaginableHeaders(this IMvcBuilder builder, Action<HeaderOptions> options)
public static IMvcBuilder UsePaginableHeaders(this IMvcBuilder builder)
{
var o = new HeaderOptions();
return builder.UsePaginableHeaders(t => t.UseExpanded());
}

public static IMvcBuilder UsePaginableHeaders(this IMvcBuilder builder, Action<HeaderFormatOptions> options)
{
var o = new HeaderFormatOptions();
options(o);

if (o.IsCondensed)
return builder.AddMvcOptions(t => t.Filters.Add(typeof(CondensedActionFilter)));
else
return builder.AddMvcOptions(t => t.Filters.Add(typeof(ExpandedActionFilter)));
switch (o.HeaderFormat)
{
case HeaderFormat.Expanded:
return builder.AddMvcOptions(t => t.Filters.Add(typeof(ExpandedHeadersActionFilter)));
default:
return builder.AddMvcOptions(t => t.Filters.Add(typeof(CondensedHeadersActionFilter)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/neekgreen/paginablecollections.aspnetcore</RepositoryUrl>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.0</NetStandardImplicitPackageVersion>
<Version>0.2.0</Version>
<Version>0.3.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/PaginableCollections.AspNetCore/PaginableHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public class PaginationHeader
{
public PaginationHeader(IPaginable paginable)
{
this.PageNumber = paginable.PageNumber;
this.ItemCountPerPage = paginable.ItemCountPerPage;
this.TotalItemCount = paginable.TotalItemCount;
this.TotalPageCount = paginable.TotalPageCount;
PageNumber = paginable.PageNumber;
ItemCountPerPage = paginable.ItemCountPerPage;
TotalItemCount = paginable.TotalItemCount;
TotalPageCount = paginable.TotalPageCount;
}

public int ItemCountPerPage { get; set; }
Expand Down

0 comments on commit cb153a4

Please sign in to comment.