This repository was archived by the owner on Jun 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathClientQueryProvider.cs
More file actions
155 lines (131 loc) · 5.37 KB
/
ClientQueryProvider.cs
File metadata and controls
155 lines (131 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections.Generic;
using commercetools.Sdk.Domain;
using commercetools.Sdk.Linq;
using commercetools.Sdk.Registration;
namespace commercetools.Sdk.Client
{
using System;
using System.Linq;
using System.Linq.Expressions;
using Domain.Query;
public class ClientQueryProvider<T> : IQueryProvider
{
private readonly IClient client;
private IList<T> result = new List<T>();
private IEnumerator<T> enumerator;
public ClientQueryProvider(IClient client, QueryCommand<T> command, bool queryAll)
{
this.client = client;
this.Command = command;
if (queryAll)
{
enumerator = new ClientQueryAllEnumerator<T>(client, command);
}
}
public QueryCommand<T> Command { get; }
public IQueryable CreateQuery(Expression expression)
{
return (this as IQueryProvider).CreateQuery<T>(expression);
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
if (typeof(TElement) != typeof(T))
{
throw new ArgumentException("Only " + typeof(T).FullName + " objects are supported");
}
bool isMethodCallExpression = expression is MethodCallExpression;
if (!isMethodCallExpression)
{
return new ClientQueryableCollection<TElement>(this as ClientQueryProvider<TElement>, expression);
}
var cmd = this.Command as QueryCommand<TElement>;
MethodCallExpression mc = expression as MethodCallExpression;
switch (mc.Method.Name)
{
case "Where":
if (mc.Arguments[1] is UnaryExpression where)
{
var t = where.Operand as Expression<Func<TElement, bool>>;
var queryPredicate = new QueryPredicate<TElement>(t);
if (cmd != null && cmd.QueryParameters is IPredicateQueryable queryParameters)
{
queryParameters.Where.Add(queryPredicate.ToString());
}
}
break;
case "Take":
if (mc.Arguments[1] is ConstantExpression limit)
{
if (cmd != null && cmd.QueryParameters is IPageable queryParameters)
{
queryParameters.Limit = (int)limit.Value;
}
}
break;
case "Skip":
if (mc.Arguments[1] is ConstantExpression offset)
{
if (cmd != null && cmd.QueryParameters is IPageable queryParameters)
{
queryParameters.Offset = (int)offset.Value;
}
}
break;
case "OrderBy":
case "ThenBy":
case "OrderByDescending":
case "ThenByDescending":
if (mc.Arguments[1] is UnaryExpression sort)
{
if (cmd != null && cmd.QueryParameters is ISortable queryParameters)
{
if (mc.Method.Name.StartsWith("OrderBy", StringComparison.Ordinal))
{
queryParameters.Sort.Clear();
}
var direction = SortDirection.Ascending;
if (mc.Method.Name.EndsWith("Descending", StringComparison.Ordinal))
{
direction = SortDirection.Descending;
}
var render = sort.Operand.RenderSort();
queryParameters.Sort.Add(new Sort<T>(render, direction).ToString());
}
}
break;
case "Expand":
if (mc.Arguments[1] is UnaryExpression expand)
{
if (cmd != null && cmd.QueryParameters is IExpandable queryParameters)
{
queryParameters.Expand.Add(new Expansion<TElement>(expand.Operand).ToString());
}
}
break;
default:
break;
}
return new ClientQueryableCollection<TElement>(this as ClientQueryProvider<TElement>, expression);
}
public object Execute(Expression expression)
{
throw new NotImplementedException();
}
public TResult Execute<TResult>(Expression expression)
{
if (enumerator != null)
{
enumerator.Reset();
return (TResult)enumerator;
}
if (this.client == null)
{
throw new FieldAccessException("Client cannot be null");
}
var queryResult = this.client.ExecuteAsync(this.Command);
var returnedSet = queryResult.Result;
this.result = returnedSet.Results;
return (TResult)this.result.GetEnumerator();
}
}
}