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

Added support for Filter when using ByKey #122

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using OData.QueryBuilder.Conventions.AddressingEntities.Options;
using System;
using System.Linq.Expressions;
using OData.QueryBuilder.Conventions.Functions;
using OData.QueryBuilder.Conventions.Operators;

namespace OData.QueryBuilder.Conventions.AddressingEntities.Query
{
public interface IODataQueryKey<TEntity> : IODataOptionKey<IODataQueryKey<TEntity>, TEntity>, IODataQuery
{
public IAddressingEntries<TResource> For<TResource>(Expression<Func<TEntity, object>> resource);
IODataQueryKey<TEntity> Filter(Expression<Func<TEntity, bool>> filter, bool useParenthesis = false);
IODataQueryKey<TEntity> Filter(Expression<Func<TEntity, IODataFunction, bool>> filter, bool useParenthesis = false);
IODataQueryKey<TEntity> Filter(Expression<Func<TEntity, IODataFunction, IODataOperator, bool>> filter, bool useParenthesis = false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
using System;
using System.Linq.Expressions;
using System.Text;
using OData.QueryBuilder.Conventions.Functions;
using OData.QueryBuilder.Conventions.Operators;

namespace OData.QueryBuilder.Conventions.AddressingEntities.Query
{
internal class ODataQueryKey<TEntity> : ODataQuery, IODataQueryKey<TEntity>
{
private bool _hasMultyExpands;
private bool _hasMultyFilters;

public ODataQueryKey(StringBuilder stringBuilder, ODataQueryBuilderOptions odataQueryBuilderOptions)
: base(stringBuilder, odataQueryBuilderOptions)
{
_hasMultyExpands = false;
_hasMultyFilters = false;
}

public IAddressingEntries<TResource> For<TResource>(Expression<Func<TEntity, object>> resource)
Expand Down Expand Up @@ -51,6 +55,27 @@ public IODataQueryKey<TEntity> Select(Expression<Func<TEntity, object>> select)

return this;
}

public IODataQueryKey<TEntity> Filter(Expression<Func<TEntity, bool>> filter, bool useParenthesis = false)
{
var query = new ODataOptionFilterExpressionVisitor(_odataQueryBuilderOptions).ToQuery(filter, useParenthesis);

return Filter(query);
}

public IODataQueryKey<TEntity> Filter(Expression<Func<TEntity, IODataFunction, bool>> filter, bool useParenthesis = false)
{
var query = new ODataOptionFilterExpressionVisitor(_odataQueryBuilderOptions).ToQuery(filter, useParenthesis);

return Filter(query);
}

public IODataQueryKey<TEntity> Filter(Expression<Func<TEntity, IODataFunction, IODataOperator, bool>> filter, bool useParenthesis = false)
{
var query = new ODataOptionFilterExpressionVisitor(_odataQueryBuilderOptions).ToQuery(filter, useParenthesis);

return Filter(query);
}

private IODataQueryKey<TEntity> Expand<T>(T query) where T : class
{
Expand All @@ -67,5 +92,21 @@ private IODataQueryKey<TEntity> Expand<T>(T query) where T : class

return this;
}

private IODataQueryKey<TEntity> Filter(string query)
{
if (_hasMultyFilters)
{
_stringBuilder.Merge(ODataOptionNames.Filter, QuerySeparators.Main, $" {ODataLogicalOperations.And} {query}");
}
else
{
_stringBuilder.Append($"{ODataOptionNames.Filter}{QuerySeparators.EqualSign}{query}{QuerySeparators.Main}");
}

_hasMultyFilters = true;

return this;
}
}
}
35 changes: 35 additions & 0 deletions test/OData.QueryBuilder.Test/ODataQueryKeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,41 @@ public void ODataQueryBuilderKey_Expand_Union_Success()
"&" +
"$select=IdRule");
}

[Fact(DisplayName = "Filter not bool => Success")]
public void ODataQueryBuilderKey_Filter_Not__Bool_Success()
{
var uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByKey(123)
.Filter(s => s.IsActive && !(bool)s.IsOpen)
.ToUri();

uri.Should().Be("http://mock/odata/ODataType(123)?$filter=IsActive and not IsOpen");
}

[Fact(DisplayName = "Filter string with ReplaceCharacters => Success")]
public void ODataQueryBuilderKey_Filter_string_with_ReplaceCharacters_Success()
{
var dictionary = new Dictionary<string, string>()
{
{ "%", "%25" },
{ "/", "%2f" },
{ "?", "%3f" },
{ "#", "%23" },
{ "&", "%26" }
};

var constValue = "3 & 4 / 7 ? 8 % 9 # 1";

var uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByKey(123)
.Filter((s, f) => s.ODataKind.ODataCode.Code == f.ReplaceCharacters(constValue, dictionary))
.ToUri();

uri.Should().Be("http://mock/odata/ODataType(123)?$filter=ODataKind/ODataCode/Code eq '3 %26 4 %2f 7 %3f 8 %25 9 %23 1'");
}

[Fact(DisplayName = "ToDicionary => Success")]
public void ToDicionaryTest()
Expand Down
Loading