-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7165640
commit 25e40e0
Showing
7 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
src/BitzArt.LinqExtensions/Extensions/FilterSetExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Linq; | ||
|
||
namespace BitzArt; | ||
|
||
public static class FilterSetExtensions | ||
{ | ||
public static IQueryable<TSource> Apply<TSource>(this IQueryable<TSource> query, IFilterSet<TSource> filterSet) | ||
where TSource : class | ||
{ | ||
return filterSet.Apply(query); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Linq; | ||
|
||
namespace BitzArt; | ||
|
||
public interface IFilterSet<TSource> where TSource : class | ||
{ | ||
public IQueryable<TSource> Apply(IQueryable<TSource> query); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace BitzArt.LinqExtensions.Tests; | ||
|
||
public class FilterSetTests | ||
{ | ||
private class TestEntity | ||
{ | ||
public int? Id { get; set; } | ||
public string? Name { get; set; } | ||
} | ||
|
||
private class TestFilterSet : IFilterSet<TestEntity> | ||
{ | ||
public int? Id { get; set; } | ||
public string? Name { get; set; } | ||
|
||
public IQueryable<TestEntity> Apply(IQueryable<TestEntity> query) | ||
{ | ||
return query | ||
.AddFilter(x => x.Id, Id) | ||
.AddFilter(x => x.Name, Name); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Apply_WithIdFilter_Filters() | ||
{ | ||
var filterSet = new TestFilterSet | ||
{ | ||
Id = 1 | ||
}; | ||
|
||
var query = Enumerable.Range(1, 10) | ||
.Select(x => new TestEntity { Id = x }) | ||
.AsQueryable(); | ||
|
||
var result = query.Apply(filterSet); | ||
|
||
Assert.Equal(1, result.Count()); | ||
} | ||
|
||
[Fact] | ||
public void Apply_WithNameFilter_Filters() | ||
{ | ||
var filterSet = new TestFilterSet | ||
{ | ||
Name = "Test 1" | ||
}; | ||
|
||
var query = Enumerable.Range(1, 10) | ||
.Select(x => new TestEntity { Name = $"Test {x}" }) | ||
.AsQueryable(); | ||
|
||
var result = query.Apply(filterSet); | ||
|
||
Assert.Equal(1, result.Count()); | ||
|
||
var entity = result.Single(); | ||
|
||
Assert.Equal("Test 1", entity.Name); | ||
} | ||
} |