mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-24 22:42:30 +01:00
More clarity
This commit is contained in:
parent
b75b6f47c6
commit
e6d170888b
14
README.md
14
README.md
@ -12,7 +12,7 @@ Most common use case would be for serving ASP.NET Core GET queries.
|
|||||||
In this example, consider an app with a `Post` entity.
|
In this example, consider an app with a `Post` entity.
|
||||||
We'll use Sieve to add sorting, filtering, and pagination capabilities when GET-ing all available posts.
|
We'll use Sieve to add sorting, filtering, and pagination capabilities when GET-ing all available posts.
|
||||||
|
|
||||||
### 1. Add required services (`SieveProcessor<TEntity>`)
|
### 1. Add required services
|
||||||
|
|
||||||
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, in `ConfigureServices` in `Startup.cs` add:
|
So to use Sieve with `Post`s, in `ConfigureServices` in `Startup.cs` add:
|
||||||
@ -20,7 +20,7 @@ So to use Sieve with `Post`s, in `ConfigureServices` in `Startup.cs` add:
|
|||||||
services.AddScoped<SieveProcessor<Post>>();
|
services.AddScoped<SieveProcessor<Post>>();
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Add `Sieve` attributes on properties you'd like to sort/filter in your models
|
### 2. Tell Sieve which 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 example:
|
So for our `Post` entity model example:
|
||||||
@ -42,9 +42,9 @@ public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;
|
|||||||
```
|
```
|
||||||
There is also the `name` parameter that you can use to have a different name for use by clients.
|
There is also the `name` parameter that you can use to have a different name for use by clients.
|
||||||
|
|
||||||
### 3. Use `SieveModel` in your controllers
|
### 3. Get sort/filter/page queries by using the Sieve model in your controllers
|
||||||
|
|
||||||
In the action handling returning Posts, use `SieveModel` to get the sort/filter/paginate query.
|
In the action that handles returning Posts, use `SieveModel` to get the sort/filter/page query.
|
||||||
Apply it 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:
|
||||||
```
|
```
|
||||||
@ -52,7 +52,7 @@ For instance:
|
|||||||
public JsonResult GetPosts(SieveModel sieveModel)
|
public JsonResult GetPosts(SieveModel sieveModel)
|
||||||
{
|
{
|
||||||
var result = _dbContext.Posts.AsNoTracking(); // Makes read-only queries faster
|
var result = _dbContext.Posts.AsNoTracking(); // Makes read-only queries faster
|
||||||
result = _sieveProcessor.ApplyAll(sieveModel, result); // Returns `result` after applying the sort/filter/paginate query in `SieveModel` to it
|
result = _sieveProcessor.ApplyAll(sieveModel, result); // Returns `result` after applying the sort/filter/page query in `SieveModel` to it
|
||||||
return Json(result.ToList());
|
return Json(result.ToList());
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -78,7 +78,7 @@ public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
|||||||
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
|
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) : // ThenBy only works on IOrderedQueryable<TEntity>
|
||||||
source.OrderBy(p => p.LikeCount)
|
source.OrderBy(p => p.LikeCount)
|
||||||
.ThenBy(p => p.CommentCount)
|
.ThenBy(p => p.CommentCount)
|
||||||
.ThenBy(p => p.DateCreated);
|
.ThenBy(p => p.DateCreated);
|
||||||
@ -118,7 +118,7 @@ Then you can add the configuration:
|
|||||||
|
|
||||||
## Send a request
|
## Send a request
|
||||||
|
|
||||||
With all the above in place, you can now send a GET request that includes a sort/filter/paginate query.
|
With all the above in place, you can now send a GET request that includes a sort/filter/page query.
|
||||||
An example:
|
An example:
|
||||||
```
|
```
|
||||||
GET /GetPosts
|
GET /GetPosts
|
||||||
|
Loading…
Reference in New Issue
Block a user