Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make properties nullable in filter objects #76

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AutoFilterer/Abstractions/IOrderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
[IgnoreFilter] Sorting SortBy { get; set; }

[IgnoreFilter] string Sort { get; }
[IgnoreFilter] string? Sort { get; }

Check warning on line 13 in src/AutoFilterer/Abstractions/IOrderable.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

IOrderedQueryable<TSource> ApplyOrder<TSource>(IQueryable<TSource> source);
}
4 changes: 2 additions & 2 deletions src/AutoFilterer/Abstractions/IPaginationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace AutoFilterer.Abstractions;

public interface IPaginationFilter : IFilter
{
[IgnoreFilter] int Page { get; set; }
[IgnoreFilter] int PerPage { get; set; }
[IgnoreFilter] int? Page { get; set; }
[IgnoreFilter] int? PerPage { get; set; }

IQueryable<T> ApplyFilterWithoutPagination<T>(IQueryable<T> query);
}
2 changes: 1 addition & 1 deletion src/AutoFilterer/Types/OrderableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
private static readonly MethodInfo orderByDescending = typeof(Queryable).GetMethods().First(x => x.Name == nameof(Queryable.OrderByDescending));

[IgnoreFilter] public virtual Sorting SortBy { get; set; }
[IgnoreFilter] public virtual string Sort { get; }
[IgnoreFilter] public virtual string? Sort { get; }

Check warning on line 19 in src/AutoFilterer/Types/OrderableBase.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public virtual IOrderedQueryable<TSource> ApplyOrder<TSource>(IQueryable<TSource> queryable)
{
Expand Down
8 changes: 4 additions & 4 deletions src/AutoFilterer/Types/PaginationFilterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ namespace AutoFilterer.Types;
public class PaginationFilterBase : OrderableFilterBase, IOrderablePaginationFilter
{
[IgnoreFilter]
public virtual int Page { get; set; } = 1;
public virtual int? Page { get; set; } = 1;

[IgnoreFilter]
public virtual int PerPage { get; set; } = 10;
public virtual int? PerPage { get; set; } = 10;

public override IQueryable<TEntity> ApplyFilterTo<TEntity>(IQueryable<TEntity> query)
{
if (query is IOrderedQueryable<TEntity> ordered)
return this.ApplyFilterTo(ordered);

return base.ApplyFilterTo(query).ToPaged(Page, PerPage);
return base.ApplyFilterTo(query).ToPaged(Page ?? 1, PerPage ?? 10);
}

public virtual IQueryable<TEntity> ApplyFilterTo<TEntity>(IOrderedQueryable<TEntity> query)
=> base.ApplyFilterTo(query).ToPaged(Page, PerPage);
=> base.ApplyFilterTo(query).ToPaged(Page ?? 1, PerPage ?? 10);

public virtual IQueryable<T> ApplyFilterWithoutPagination<T>(IQueryable<T> query)
=> base.ApplyFilterTo(query);
Expand Down
18 changes: 9 additions & 9 deletions src/AutoFilterer/Types/StringFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,47 @@
/// <summary>
/// Provides parameter for equal operator '==' in query.
/// </summary>
public virtual string Eq { get; set; }
public virtual string? Eq { get; set; }

Check warning on line 18 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to not equal operator '!=' in query.
/// </summary>
public virtual string Not { get; set; }
public virtual string? Not { get; set; }

Check warning on line 23 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to String.Equals method query.
/// </summary>
public virtual new string Equals { get; set; }
public virtual new string? Equals { get; set; }

Check warning on line 28 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to String.Contains method query.
/// </summary>
public virtual string Contains { get; set; }
public virtual string? Contains { get; set; }

Check warning on line 33 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to !String.Contains method query.
/// </summary>
public virtual string NotContains { get; set; }
public virtual string? NotContains { get; set; }

Check warning on line 38 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to String.StartsWith method query.
/// </summary>
public virtual string StartsWith { get; set; }
public virtual string? StartsWith { get; set; }

Check warning on line 43 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to !String.StartsWith method query.
/// </summary>
public virtual string NotStartsWith { get; set; }
public virtual string? NotStartsWith { get; set; }

Check warning on line 48 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to String.EndsWith method query.
/// </summary>
public virtual string EndsWith { get; set; }
public virtual string? EndsWith { get; set; }

Check warning on line 53 in src/AutoFilterer/Types/StringFilter.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Provides parameter to !String.EndsWith method query.
/// </summary>
public virtual string NotEndsWith { get; set; }
public virtual string? NotEndsWith { get; set; }

/// <summary>
/// Provides parameter to check is null.
Expand Down Expand Up @@ -87,74 +87,74 @@
/// </summary>
public virtual StringComparison? Compare { get; set; }

public virtual Expression BuildExpression(ExpressionBuildContext context)
{
Expression expression = null;

if (Eq != null)
expression = expression.Combine(OperatorComparisonAttribute.Equal.BuildExpression(ContextFor(context, nameof(Eq), Eq)), CombineWith);

if (Not != null)
expression = expression.Combine(OperatorComparisonAttribute.NotEqual.BuildExpression(ContextFor(context, nameof(Not), Not)), CombineWith);

if (IsNull != null)
expression = expression.Combine(OperatorComparisonAttribute.IsNull.BuildExpression(ContextFor(context, nameof(IsNull), null)), CombineWith);

if (IsNotNull != null)
expression = expression.Combine(OperatorComparisonAttribute.IsNotNull.BuildExpression(ContextFor(context, nameof(IsNotNull), null)), CombineWith);

if (Equals != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.Equals) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(Equals), Equals)), CombineWith);

if (Contains != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.Contains) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(Contains), Contains)), CombineWith);

if (NotContains != null)
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.Contains) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(NotContains), NotContains))), CombineWith);

if (StartsWith != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.StartsWith) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(StartsWith), StartsWith)), CombineWith);

if (NotStartsWith != null)
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.StartsWith) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(NotStartsWith), NotStartsWith))), CombineWith);

if (EndsWith != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.EndsWith) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(EndsWith), EndsWith)), CombineWith);

if (NotEndsWith != null)
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.EndsWith) { Comparison = Compare }.BuildExpression(ContextFor(context, nameof(NotEndsWith), NotEndsWith))), CombineWith);

if (IsEmpty != null)
{
if (IsEmpty.Value)
{
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.Equals) { Comparison = Compare }
.BuildExpression(ContextForConstant(context, string.Empty)), CombineWith);
}
else
{
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.Equals) { Comparison = Compare }
.BuildExpression(ContextForConstant(context, string.Empty))), CombineWith);
}
}

if (IsNotEmpty != null)
{
if (IsNotEmpty.Value)
{
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.Equals) { Comparison = Compare }
.BuildExpression(ContextForConstant(context, string.Empty))), CombineWith);
}
else
{
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.Equals) { Comparison = Compare }
.BuildExpression(ContextForConstant(context, string.Empty)), CombineWith);
}
}

return expression;
}

Check notice on line 157 in src/AutoFilterer/Types/StringFilter.cs

View check run for this annotation

codefactor.io / CodeFactor

src/AutoFilterer/Types/StringFilter.cs#L90-L157

Complex Method
private ExpressionBuildContext ContextFor(ExpressionBuildContext originalContext, string propertyName, string value)
{
var innerProperty = originalContext.FilterProperty.PropertyType.GetProperty(propertyName);
Expand Down