Skip to content
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
37 changes: 37 additions & 0 deletions OData.Neo.Core.Tests.Unit/Clients/Extensions/ClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OData.Neo.Core.Clients.Extensions;
using Xunit;

namespace OData.Neo.Core.Tests.Unit.Clients.Extensions
{
public class ClientTests
{
[Fact]
public async Task ShouldFilterStudent()
{
// given
var students = new List<Student>
{
new Student { Id = Guid.NewGuid(), Name = "John" },
new Student { Id = Guid.NewGuid(), Name = "Mary" },
new Student { Id = Guid.NewGuid(), Name = "Peter" }
}.AsQueryable();

var filteredStuff =
await students.ApplyODataQueryAsync(
"$select=Name");

Assert.True(true);
}

public class Student
{
public Guid Id { get; set; }
public string Name { get; set; }
}
}
}
78 changes: 78 additions & 0 deletions OData.Neo.Core/Clients/Extensions/ODataEnumerableClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using OData.Neo.Core.Brokers.Expressions;
using OData.Neo.Core.Brokers.SqlQueries;
using OData.Neo.Core.Services.Coordinations.OQueries;
using OData.Neo.Core.Services.Foundations.OExpressions;
using OData.Neo.Core.Services.Foundations.OQueries;
using OData.Neo.Core.Services.Foundations.OSqls;
using OData.Neo.Core.Services.Foundations.OTokenizations;
using OData.Neo.Core.Services.Foundations.Projections;
using OData.Neo.Core.Services.Foundations.Tokenizations;
using OData.Neo.Core.Services.Orchestrations.OQueries;
using OData.Neo.Core.Services.Orchestrations.OTokenizations;

namespace OData.Neo.Core.Clients.Extensions
{
public static class ODataEnumerableClient
{
public static async ValueTask<IQueryable> ApplyODataQueryAsync<T>(
this IQueryable<T> queryable,
string odataQuery) where T : class
{
var otokenizationService = new OTokenizationService();
var projectionService = new ProjectionService();
var tokenizationService = new TokenizationService();

var oTokenizationOrchestrationService =
new OTokenizationOrchestrationService(
tokenizationService,
projectionService,
otokenizationService);

var expressionBroker = new ExpressionBroker();
var sqlQueryBroker = new SqlQueryBroker<T>();

var oExpressionService = new OExpressionService(expressionBroker);
var oQueryService = new OQueryService(sqlQueryBroker);
var oSqlService = new OSqlService(sqlQueryBroker);

var oQueryOrchestrationService =
new OQueryOrchestrationService(
oExpressionService,
oQueryService,
oSqlService);

var oQueryCoordinationService = new OQueryCoordinationService(
oTokenizationOrchestrationService,
oQueryOrchestrationService);

Expression oQueryExpression =
await oQueryCoordinationService
.ProcessOQueryAsync<T>(odataQuery);

return Execute(queryable, oQueryExpression);
}

public static IQueryable Execute<T>(IQueryable<T> sources, Expression exp)
{
MethodCallExpression call = exp as MethodCallExpression;

// Select(...)
MethodInfo methodInfo = call.Method;

UnaryExpression unary = call.Arguments[1] as UnaryExpression;

LambdaExpression lambda = unary.Operand as LambdaExpression;

// Delegate delegateFunc = lambda.Compile();

return methodInfo.Invoke(null, new object[] { sources, lambda }) as IQueryable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ private string CovertToLinqExp(OToken token)
{
string properties = string.Join(
separator: ",",
values: child.Children.Select(child => $"obj.{child.RawValue}"));
values: child.Children.Where(c => c.ProjectedType == Models.ProjectedTokens.ProjectedTokenType.Property)
.Select(c => $"obj.{c.RawValue}"));

stringBuilder.Append($"Select(obj => new {{{properties}}})");
}
Expand Down