Sorting and filtering made easy for your ASP.NET Core project! From CSV input to a filtered/sorted IQueryable with no effort using Panner, with extra shortcuts, custom model binders, and other goodies.
For more options, see Panner's documentation.
// Your action method!
[HttpGet]
// Sample request: /entities?sorts=CreatedTimeStamp,Id&filters=Name=Foo||Name=Bar,IsVisible=True
public async Task<IActionResult> GetAllPosts(
[FromServices] DbContext myDbContext,
// Add the following parameters to parse/validate csv inputs into particles.
// Custom model binders take care of converting the csv string into a collection of particles.
[FromQuery] IReadOnlyCollection<ISortParticle<MyEntity>> sorts,
[FromQuery] IReadOnlyCollection<IFilterParticle<MyEntity>> filters
)
{
var result = await myDbContext.MyEntity
.Apply(filters) // Apply validated and parsed filters
.Apply(sorts) // Apply validated and parsed sorts
.ToArrayAsync();
return Ok(result);
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); // Or maybe you're using .AddControllers()!
services.UsePanner(c =>
{
// Entity wide configuration (Option 1)
c.Entity<Post>()
.AllPropertiesAreSortableByName();
.AllPropertiesAreFilterableByName();
// A more granular approach (Option 2)
c.Entity<MyEntity>()
.Property(x => x.Id, o => o
.IsSortableAs(nameof(Views.Post.Id))
.IsFilterableAs(nameof(Views.Post.Id))
)
.Property(x => x.Title, o => o
.IsSortableAs(nameof(Views.Post.Title))
)
.Property(x => x.CreatedOn, o => o
.IsSortableAs(nameof(Views.Post.Creation))
.IsFilterableAs(nameof(Views.Post.Creation))
);
});
}