diff --git a/src/BitzArt.LinqExtensions/AddFilterExtension.cs b/src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs similarity index 100% rename from src/BitzArt.LinqExtensions/AddFilterExtension.cs rename to src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs diff --git a/src/BitzArt.LinqExtensions/ExpressionExtensions.cs b/src/BitzArt.LinqExtensions/Extensions/ExpressionExtensions.cs similarity index 100% rename from src/BitzArt.LinqExtensions/ExpressionExtensions.cs rename to src/BitzArt.LinqExtensions/Extensions/ExpressionExtensions.cs diff --git a/src/BitzArt.LinqExtensions/Extensions/FilterSetExtensions.cs b/src/BitzArt.LinqExtensions/Extensions/FilterSetExtensions.cs new file mode 100644 index 0000000..54110a1 --- /dev/null +++ b/src/BitzArt.LinqExtensions/Extensions/FilterSetExtensions.cs @@ -0,0 +1,12 @@ +using System.Linq; + +namespace BitzArt; + +public static class FilterSetExtensions +{ + public static IQueryable Apply(this IQueryable query, IFilterSet filterSet) + where TSource : class + { + return filterSet.Apply(query); + } +} diff --git a/src/BitzArt.LinqExtensions/WhereIfExtension.cs b/src/BitzArt.LinqExtensions/Extensions/WhereIfExtension.cs similarity index 100% rename from src/BitzArt.LinqExtensions/WhereIfExtension.cs rename to src/BitzArt.LinqExtensions/Extensions/WhereIfExtension.cs diff --git a/src/BitzArt.LinqExtensions/Interfaces/IFilterSet.cs b/src/BitzArt.LinqExtensions/Interfaces/IFilterSet.cs new file mode 100644 index 0000000..68ef695 --- /dev/null +++ b/src/BitzArt.LinqExtensions/Interfaces/IFilterSet.cs @@ -0,0 +1,8 @@ +using System.Linq; + +namespace BitzArt; + +public interface IFilterSet where TSource : class +{ + public IQueryable Apply(IQueryable query); +} diff --git a/src/OpenTelemetry/BitzArt.OpenTelemetry.BoilerPlate/Extensions/AddTelemetryExtension.cs b/src/OpenTelemetry/BitzArt.OpenTelemetry.BoilerPlate/Extensions/AddTelemetryExtension.cs index c22ae74..3f7d677 100644 --- a/src/OpenTelemetry/BitzArt.OpenTelemetry.BoilerPlate/Extensions/AddTelemetryExtension.cs +++ b/src/OpenTelemetry/BitzArt.OpenTelemetry.BoilerPlate/Extensions/AddTelemetryExtension.cs @@ -7,6 +7,8 @@ using OpenTelemetry.Resources; using OpenTelemetry.Trace; using BitzArt.OpenTelemetry.BoilerPlate; +using Microsoft.AspNetCore.Http; +using System.Diagnostics; namespace BitzArt; diff --git a/tests/BitzArt.LinqExtensions.Tests/FilterSetTests.cs b/tests/BitzArt.LinqExtensions.Tests/FilterSetTests.cs new file mode 100644 index 0000000..640937d --- /dev/null +++ b/tests/BitzArt.LinqExtensions.Tests/FilterSetTests.cs @@ -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 + { + public int? Id { get; set; } + public string? Name { get; set; } + + public IQueryable Apply(IQueryable 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); + } +}