Skip to content

Commit

Permalink
Add IFilterSet
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Nov 6, 2023
1 parent 7165640 commit 25e40e0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/BitzArt.LinqExtensions/Extensions/FilterSetExtensions.cs
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);
}
}
8 changes: 8 additions & 0 deletions src/BitzArt.LinqExtensions/Interfaces/IFilterSet.cs
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using BitzArt.OpenTelemetry.BoilerPlate;
using Microsoft.AspNetCore.Http;
using System.Diagnostics;

namespace BitzArt;

Expand Down
61 changes: 61 additions & 0 deletions tests/BitzArt.LinqExtensions.Tests/FilterSetTests.cs
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);
}
}

0 comments on commit 25e40e0

Please sign in to comment.