mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-21 21:12:50 +01:00
Merge branch 'master' of https://github.com/Biarity/Sieve
This commit is contained in:
commit
7f42fa5895
26
README.md
26
README.md
@ -1,5 +1,5 @@
|
|||||||
# 🎛️ Sieve
|
# 🎛️ Sieve
|
||||||
Sieve is a simple and extensible framework for .NET Core that **adds sorting, filtering, and pagination functionality out of the box**.
|
Sieve is a simple, clean, and extensible framework for .NET Core that **adds sorting, filtering, and pagination functionality out of the box**.
|
||||||
Most common use case would be for serving ASP.NET Core GET queries.
|
Most common use case would be for serving ASP.NET Core GET queries.
|
||||||
|
|
||||||
## Usage for ASP.NET Core
|
## Usage for ASP.NET Core
|
||||||
@ -10,7 +10,7 @@ We'll use Sieve to add sorting, filtering, and pagination capabilities when GET-
|
|||||||
### 1. Add required services (`SieveProcessor<TEntity>`)
|
### 1. Add required services (`SieveProcessor<TEntity>`)
|
||||||
|
|
||||||
Inject the `SieveProcessor<TEntity>` service for each entity you'd like to use Sieve with.
|
Inject the `SieveProcessor<TEntity>` service for each entity you'd like to use Sieve with.
|
||||||
So to use Sieve with `Post`s, go to `ConfigureServices` in `Startup.cs` and add:
|
So to use Sieve with `Post`s, in `ConfigureServices` in `Startup.cs` add:
|
||||||
```
|
```
|
||||||
services.AddScoped<SieveProcessor<Post>>();
|
services.AddScoped<SieveProcessor<Post>>();
|
||||||
```
|
```
|
||||||
@ -18,7 +18,7 @@ services.AddScoped<SieveProcessor<Post>>();
|
|||||||
### 2. Add `Sieve` attributes on properties you'd like to sort/filter in your models
|
### 2. Add `Sieve` attributes on properties you'd like to sort/filter in your models
|
||||||
|
|
||||||
Sieve will only sort/filter properties that have the attribute `[Sieve(CanSort = true, CanFilter = true)]` on them (they don't have to be both true).
|
Sieve will only sort/filter properties that have the attribute `[Sieve(CanSort = true, CanFilter = true)]` on them (they don't have to be both true).
|
||||||
So for our `Post` entity model:
|
So for our `Post` entity model example:
|
||||||
```
|
```
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
@ -39,8 +39,8 @@ There is also the `name` parameter that you can use to have a different name for
|
|||||||
|
|
||||||
### 3. Use `SieveModel` in your controllers
|
### 3. Use `SieveModel` in your controllers
|
||||||
|
|
||||||
In the action handling returning Posts, use the `SieveModel` to get the sort/filter/paginate query.
|
In the action handling returning Posts, use `SieveModel` to get the sort/filter/paginate query.
|
||||||
Apply it by to your data by injecting `SieveProcessor<Post>` into the controller and using its `ApplyAll` method.
|
Apply it to your data by injecting `SieveProcessor<Post>` into the controller and using its `ApplyAll` method.
|
||||||
For instance:
|
For instance:
|
||||||
```
|
```
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@ -70,7 +70,7 @@ Where `SieveCustomSortMethodsOfPosts` for example is:
|
|||||||
```
|
```
|
||||||
public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
||||||
{
|
{
|
||||||
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc)
|
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc) // The method is given an indicator of weather to use ThenBy(), and if the query is descending
|
||||||
{
|
{
|
||||||
var result = useThenBy ?
|
var result = useThenBy ?
|
||||||
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
|
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
|
||||||
@ -78,7 +78,7 @@ public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
|||||||
.ThenBy(p => p.CommentCount)
|
.ThenBy(p => p.CommentCount)
|
||||||
.ThenBy(p => p.DateCreated);
|
.ThenBy(p => p.DateCreated);
|
||||||
|
|
||||||
return result;
|
return result; // Must return modified IQueryable<TEntity>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -86,12 +86,12 @@ And `SieveCustomFilterMethodsOfPosts`:
|
|||||||
```
|
```
|
||||||
public class SieveCustomFilterMethodsOfPosts : ISieveCustomFilterMethods<Post>
|
public class SieveCustomFilterMethodsOfPosts : ISieveCustomFilterMethods<Post>
|
||||||
{
|
{
|
||||||
public IQueryable<Post> IsNew(IQueryable<Post> source)
|
public IQueryable<Post> IsNew(IQueryable<Post> source, string op, string value) // The method is given the {Operator} & {Value}
|
||||||
{
|
{
|
||||||
var result = source.Where(p => p.LikeCount < 100 &&
|
var result = source.Where(p => p.LikeCount < 100 &&
|
||||||
p.CommentCount < 5);
|
p.CommentCount < 5);
|
||||||
|
|
||||||
return result;
|
return result; // Must return modified IQueryable<TEntity>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -101,12 +101,12 @@ Use the [ASP.NET Core options pattern](https://docs.microsoft.com/en-us/aspnet/c
|
|||||||
```
|
```
|
||||||
services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));
|
services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));
|
||||||
```
|
```
|
||||||
Then you can add configuration:
|
Then you can add the configuration:
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
"Sieve": {
|
"Sieve": {
|
||||||
"CaseSensitive": `boolean: should property names be case-sensitive? Defaults to false`,
|
"CaseSensitive": `boolean: should property names be case-sensitive? Defaults to false`,
|
||||||
"DefaultPageSize": `number: optional number to trim to when no page argument is given`
|
"DefaultPageSize": `number: optional number to fallback to when no page argument is given`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -128,8 +128,8 @@ More formally:
|
|||||||
* `sorts` is a comma-delimited ordered list of property names to sort by. Adding a `-` before the name switches to sorting descendingly.
|
* `sorts` is a comma-delimited ordered list of property names to sort by. Adding a `-` before the name switches to sorting descendingly.
|
||||||
* `filters` is a comma-delimited list of `{Name}{Operator}{Value}` where
|
* `filters` is a comma-delimited list of `{Name}{Operator}{Value}` where
|
||||||
* `{Name}` is the name of a property with the Sieve attribute or the name of a custom filter method for TEntity
|
* `{Name}` is the name of a property with the Sieve attribute or the name of a custom filter method for TEntity
|
||||||
* `{Operator}` is one of the [Operators](#operators) (not used when using a custom method)
|
* `{Operator}` is one of the [Operators](#operators) (has to be preceeded by a space when using custom filter methods)
|
||||||
* `{Value}` is the value to use for filtering (not used when using a custom method)
|
* `{Value}` is the value to use for filtering
|
||||||
* `page` is the number of page to return
|
* `page` is the number of page to return
|
||||||
* `pageSize` is the number of items returned per page
|
* `pageSize` is the number of items returned per page
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user