Skip to content

Commit

Permalink
Add BitzArt.CoreExtensions package reference to BitzArt.LinqExtensions (
Browse files Browse the repository at this point in the history
#14)

* Add BitzArt.CoreExtensions package reference to BitzArt.LinqExtensions

* Fix namespaces
  • Loading branch information
ligowsky authored Oct 23, 2024
1 parent 216acc7 commit 51e5e7a
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BitzArt.LinqExtensions.Batching;
using BitzArt;
using System.Linq.Expressions;

namespace System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitzArt.LinqExtensions.Batching;
namespace BitzArt;

/// <summary>
/// A builder of a batch query.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitzArt.LinqExtensions.Batching;
namespace BitzArt;

/// <summary>
/// A strategy to compose a batch query.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections;
using System.Linq.Expressions;

namespace BitzArt.LinqExtensions.Batching;
namespace BitzArt;

internal class BatchQueryBuilder<TSource> : IBatchQueryBuilder<TSource>, IAsyncEnumerable<TSource>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitzArt.LinqExtensions.Batching;
namespace BitzArt;

internal class BatchingStrategy<TSource> : IBatchingStrategy<TSource>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq.Expressions;

namespace BitzArt.LinqExtensions.Batching;
namespace BitzArt;

internal class LastValueBatchingStrategy<TSource, TProperty>(
IBatchQueryBuilder<TSource> builder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitzArt.LinqExtensions.Batching;
namespace BitzArt;

internal class OffsetBatchingStrategy<TSource>(IBatchQueryBuilder<TSource> builder, int offset)
: BatchingStrategy<TSource>(builder)
Expand Down
4 changes: 4 additions & 0 deletions src/BitzArt.LinqExtensions/BitzArt.LinqExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<PackageProjectUrl>https://github.com/BitzArt/Miscellaneous</PackageProjectUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BitzArt.CoreExtensions" Version="1.0.0" />
</ItemGroup>

</Project>
37 changes: 0 additions & 37 deletions src/BitzArt.LinqExtensions/Enums/FilterOperation.cs

This file was deleted.

39 changes: 0 additions & 39 deletions src/BitzArt.LinqExtensions/Enums/OrderDirection.cs

This file was deleted.

29 changes: 15 additions & 14 deletions src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Expressions;
using BitzArt;
using System.Linq.Expressions;

namespace System.Linq;

Expand All @@ -10,18 +11,18 @@ public static class AddFilterExtension
/// <summary>
/// Adds a filter to the query if the filter value is not null.
/// </summary>
public static IQueryable<TSource> AddFilter<TSource, TProperty>(this IQueryable<TSource> source, Expression<Func<TSource, TProperty?>> expression, TProperty? filter, FilterOperation filterOperation = FilterOperation.Equal)
public static IQueryable<TSource> AddFilter<TSource, TProperty>(this IQueryable<TSource> source, Expression<Func<TSource, TProperty?>> expression, TProperty? filter, ComparisonType comparisonType = ComparisonType.Equal)
where TProperty : class
{
if (filter is null) return source;

return BuildExpression(source, filter, expression, filterOperation);
return BuildExpression(source, filter, expression, comparisonType);
}

/// <summary>
/// Adds a filter to the query if the filter value is not null.
/// </summary>
public static IQueryable<TSource> AddFilter<TSource, TProperty>(this IQueryable<TSource> source, Expression<Func<TSource, TProperty?>> expression, TProperty? filter, FilterOperation filterOperation = FilterOperation.Equal)
public static IQueryable<TSource> AddFilter<TSource, TProperty>(this IQueryable<TSource> source, Expression<Func<TSource, TProperty?>> expression, TProperty? filter, ComparisonType comparisonType = ComparisonType.Equal)
where TProperty : struct
{
if (filter is null) return source;
Expand All @@ -30,24 +31,24 @@ public static IQueryable<TSource> AddFilter<TSource, TProperty>(this IQueryable<
Expression<Func<TProperty?, TProperty>> getValueExpression = x => x!.Value;
var valueExpression = expression.Compose(getValueExpression);

return BuildExpression(source, filterValue, valueExpression, filterOperation);
return BuildExpression(source, filterValue, valueExpression, comparisonType);
}

private static IQueryable<TSource> BuildExpression<TSource, TProperty>(IQueryable<TSource> source, TProperty filter, Expression<Func<TSource, TProperty>> expression, FilterOperation filterOperation)
private static IQueryable<TSource> BuildExpression<TSource, TProperty>(IQueryable<TSource> source, TProperty filter, Expression<Func<TSource, TProperty>> expression, ComparisonType comparisonType)
{
var argument = Expression.Parameter(typeof(TSource));
var left = Expression.Invoke(expression, argument);
var right = Expression.Constant(filter);

var eq = filterOperation switch
var eq = comparisonType switch
{
FilterOperation.Equal => Expression.Equal(left, right),
FilterOperation.NotEqual => Expression.NotEqual(left, right),
FilterOperation.GreaterThan => Expression.GreaterThan(left, right),
FilterOperation.GreaterThanOrEqual => Expression.GreaterThanOrEqual(left, right),
FilterOperation.LessThan => Expression.LessThan(left, right),
FilterOperation.LessThanOrEqual => Expression.LessThanOrEqual(left, right),
_ => throw new NotImplementedException($"Unsupported Filter Operation: '{filterOperation}'")
ComparisonType.Equal => Expression.Equal(left, right),
ComparisonType.NotEqual => Expression.NotEqual(left, right),
ComparisonType.GreaterThan => Expression.GreaterThan(left, right),
ComparisonType.GreaterThanOrEqual => Expression.GreaterThanOrEqual(left, right),
ComparisonType.LessThan => Expression.LessThan(left, right),
ComparisonType.LessThanOrEqual => Expression.LessThanOrEqual(left, right),
_ => throw new NotImplementedException($"Unsupported Comparison Type: '{comparisonType}'")
};

var lambda = Expression
Expand Down
3 changes: 2 additions & 1 deletion src/BitzArt.LinqExtensions/Extensions/OrderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Expressions;
using BitzArt;
using System.Linq.Expressions;

namespace System.Linq;

Expand Down
4 changes: 1 addition & 3 deletions tests/BitzArt.LinqExtensions.Batching.Tests/BatchTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BitzArt.LinqExtensions.Batching;

namespace BitzArt.LinqExtensions.Tests;
namespace BitzArt.LinqExtensions.Batching.Tests;

public class BatchTests
{
Expand Down
10 changes: 5 additions & 5 deletions tests/BitzArt.LinqExtensions.Tests/AddFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void NotEqual_Filter_Filters(int notEqualTo)

int? filter = notEqualTo;

var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.NotEqual).ToList();
var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.NotEqual).ToList();

Assert.Equal(9, filtered.Count);

Expand All @@ -111,7 +111,7 @@ public void GreaterThan_Filter_Filters()

int? filter = 5;

var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.GreaterThan).ToList();
var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.GreaterThan).ToList();

Assert.Equal(5, filtered.Count);
Assert.True(filtered.First().Id == 6);
Expand All @@ -127,7 +127,7 @@ public void GreaterThanOrEqual_Filter_Filters()

int? filter = 5;

var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.GreaterThanOrEqual).ToList();
var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.GreaterThanOrEqual).ToList();

Assert.Equal(6, filtered.Count);
Assert.True(filtered.First().Id == 5);
Expand All @@ -143,7 +143,7 @@ public void LessThan_Filter_Filters()

int? filter = 5;

var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.LessThan).ToList();
var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.LessThan).ToList();

Assert.Equal(4, filtered.Count);
Assert.True(filtered.First().Id == 1);
Expand All @@ -159,7 +159,7 @@ public void LessThanOrEqual_Filter_Filters()

int? filter = 5;

var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.LessThanOrEqual).ToList();
var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.LessThanOrEqual).ToList();

Assert.Equal(5, filtered.Count);
Assert.True(filtered.First().Id == 1);
Expand Down

0 comments on commit 51e5e7a

Please sign in to comment.