Skip to content

Implement count in expansion options #197

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

Open
wants to merge 2 commits into
base: master
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
39 changes: 29 additions & 10 deletions AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public static Expression GetOrderByMethod<T>(this Expression expression,
options.OrderBy?.OrderByClause,
typeof(T),
options.Skip?.Value,
GetPageSize()
GetPageSize(),
options.Count?.Value
);

int? GetPageSize()
Expand All @@ -153,12 +154,12 @@ public static Expression GetOrderByMethod<T>(this Expression expression,
}

public static Expression GetQueryableMethod(this Expression expression,
ODataQueryContext context, OrderByClause orderByClause, Type type, int? skip, int? top)
ODataQueryContext context, OrderByClause orderByClause, Type type, int? skip, int? top, bool? count)
{
if (orderByClause is null && skip is null && top is null)
if (orderByClause is null && skip is null && top is null && count is null)
return null;

if (orderByClause is null && (skip is not null || top is not null))
if (orderByClause is null && (skip is not null || top is not null || count is not null))
{
var orderBySettings = context.FindSortableProperties(type);

Expand All @@ -168,13 +169,15 @@ public static Expression GetQueryableMethod(this Expression expression,
return expression
.GetDefaultOrderByCall(orderBySettings)
.GetSkipCall(skip)
.GetTakeCall(top);
.GetTakeCall(top)
.GetCountCall(count);
}

return expression
.GetOrderByCall(orderByClause, context)
.GetSkipCall(skip)
.GetTakeCall(top);
.GetTakeCall(top)
.GetCountCall(count);
}

private static bool NoQueryableMethod(ODataQueryOptions options, ODataSettings oDataSettings)
Expand Down Expand Up @@ -333,6 +336,20 @@ public static List<string> GetReferencePath(this List<string> list, SingleResour
return list;
}
}

public static Expression GetCountCall(this Expression expression, CountQueryOption count)
{
if (count == null) return expression;

return expression.GetCountCall(count.Value);
}

public static Expression GetCountCall(this Expression expression, bool? count)
{
if (count == null) return expression;

return expression.GetCountCall();
}

public static Expression GetSkipCall(this Expression expression, SkipQueryOption skip)
{
Expand Down Expand Up @@ -560,14 +577,14 @@ FilterOptions GetFilter()

QueryOptions GetQuery()
=> HasQuery()
? new QueryOptions(next.OrderByOption, (int?)next.SkipOption, (int?)next.TopOption)
? new QueryOptions(next.OrderByOption, (int?)next.SkipOption, (int?)next.TopOption, next.CountOption)
: null;

bool HasFilter()
=> memberType.IsList() && next.FilterOption != null;

bool HasQuery()
=> memberType.IsList() && (next.OrderByOption != null || next.SkipOption.HasValue || next.TopOption.HasValue);
=> memberType.IsList() && (next.OrderByOption != null || next.SkipOption.HasValue || next.TopOption.HasValue || next.CountOption.HasValue);
});
}

Expand Down Expand Up @@ -731,7 +748,7 @@ List<List<ODataExpansionOptions>> GetQueryMethods()
MemberName = next.MemberName,
MemberType = next.MemberType,
ParentType = next.ParentType,
QueryOptions = new QueryOptions(next.QueryOptions.OrderByClause, next.QueryOptions.Skip, next.QueryOptions.Top)
QueryOptions = new QueryOptions(next.QueryOptions.OrderByClause, next.QueryOptions.Skip, next.QueryOptions.Top, next.QueryOptions.Count)
}
);//add expansion with query options

Expand Down Expand Up @@ -759,13 +776,15 @@ public class ODataExpansionOptions : Expansion

public class QueryOptions
{
public QueryOptions(OrderByClause orderByClause, int? skip, int? top)
public QueryOptions(OrderByClause orderByClause, int? skip, int? top, bool? count)
{
OrderByClause = orderByClause;
Skip = skip;
Top = top;
Count = count;
}

public bool? Count { get; set; }
public OrderByClause OrderByClause { get; set; }
public int? Skip { get; set; }
public int? Top { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
expansion.QueryOptions.OrderByClause,
elementType,
expansion.QueryOptions.Skip,
expansion.QueryOptions.Top
expansion.QueryOptions.Top,
expansion.QueryOptions.Count
);
}

Expand Down
19 changes: 19 additions & 0 deletions AutoMapper.OData.EFCore.Tests/GetQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,25 @@ static void Test(ICollection<CategoryModel> collection)
}
}

[Fact]
public async void CountingOnChildCollection()
{
string query = "/CategoryModel?$expand=Products($count=true)";
Test
(
Get<CategoryModel, Category>
(
query,
GetCategories()
)
);

static void Test(ICollection<CategoryModel> collection)
{
// TODO: Assert count value is equal to 2
}
}

[Fact]
public async void FilteringOnRoot_AndChildCollection_WithNoMatches()
{
Expand Down