@@ -22,87 +22,89 @@ public interface IQueryParser
22
22
23
23
public class QueryParser : IQueryParser
24
24
{
25
- private readonly IIncludeService _includeService ;
26
- private readonly ISparseFieldsService _fieldQuery ;
27
- private readonly IPageQueryService _pageQuery ;
25
+ private readonly IncludeService _includeService ;
26
+ private readonly SparseFieldsService _sparseFieldsService ;
27
+ private readonly FilterService _filterService ;
28
+ private readonly SortService _sortService ;
29
+ private readonly OmitDefaultService _omitDefaultService ;
30
+ private readonly OmitNullService _omitNull ;
31
+ private readonly PageService _pageService ;
32
+
28
33
private readonly ICurrentRequest _currentRequest ;
29
34
private readonly IContextEntityProvider _provider ;
30
35
private readonly IJsonApiOptions _options ;
31
36
private readonly IServiceProvider _sp ;
32
37
private ContextEntity _primaryResource ;
33
38
34
- public QueryParser ( IIncludeService includeService ,
35
- ISparseFieldsService fieldQuery ,
39
+ public QueryParser (
36
40
ICurrentRequest currentRequest ,
37
41
IContextEntityProvider provider ,
38
- IPageQueryService pageQuery ,
39
42
IJsonApiOptions options )
40
43
{
41
- _includeService = includeService ;
42
- _fieldQuery = fieldQuery ;
43
44
_currentRequest = currentRequest ;
44
- _pageQuery = pageQuery ;
45
45
_provider = provider ;
46
46
_options = options ;
47
47
}
48
48
49
- public virtual QuerySet Parse ( IQueryCollection query )
49
+ public virtual void Parse ( IQueryCollection query )
50
50
{
51
- var type = typeof ( IQueryParameterService ) ;
52
- var types = AppDomain . CurrentDomain . GetAssemblies ( )
53
- . SelectMany ( a => a . GetTypes ( ) )
54
- . Where ( t => t . IsInterface && t . Inherits ( type ) )
55
- . Select ( t => ( IQueryParameterService ) _sp . GetService ( t ) ) ;
56
-
57
51
58
52
_primaryResource = _currentRequest . GetRequestResource ( ) ;
59
- var querySet = new QuerySet ( ) ;
60
53
var disabledQueries = _currentRequest . DisabledQueryParams ;
54
+
55
+
56
+
61
57
foreach ( var pair in query )
62
58
{
63
59
if ( pair . Key . StartsWith ( QueryConstants . FILTER , StringComparison . Ordinal ) )
64
60
{
65
61
if ( disabledQueries . HasFlag ( QueryParams . Filters ) == false )
66
- querySet . Filters . AddRange ( ParseFilterQuery ( pair . Key , pair . Value ) ) ;
62
+ // querySet.Filters.AddRange(ParseFilterQuery(pair.Key, pair.Value));
67
63
continue ;
68
64
}
69
65
70
66
if ( pair . Key . StartsWith ( QueryConstants . SORT , StringComparison . Ordinal ) )
71
67
{
72
68
if ( disabledQueries . HasFlag ( QueryParams . Sort ) == false )
73
- querySet . SortParameters = ParseSortParameters ( pair . Value ) ;
69
+ // querySet.SortParameters = ParseSortParameters(pair.Value);
74
70
continue ;
75
71
}
76
72
77
73
if ( pair . Key . StartsWith ( _includeService . Name , StringComparison . Ordinal ) )
78
74
{
79
75
if ( disabledQueries . HasFlag ( QueryParams . Include ) == false )
80
- _includeService . Parse ( pair . Value ) ;
76
+ _includeService . Parse ( null , pair . Value ) ;
81
77
continue ;
82
78
}
83
79
84
80
if ( pair . Key . StartsWith ( QueryConstants . PAGE , StringComparison . Ordinal ) )
85
81
{
86
82
if ( disabledQueries . HasFlag ( QueryParams . Page ) == false )
87
- querySet . PageQuery = ParsePageQuery ( querySet . PageQuery , pair . Key , pair . Value ) ;
83
+ // querySet.PageQuery = ParsePageQuery(querySet.PageQuery, pair.Key, pair.Value);
88
84
continue ;
89
85
}
90
86
91
87
if ( pair . Key . StartsWith ( QueryConstants . FIELDS , StringComparison . Ordinal ) )
92
88
{
93
89
if ( disabledQueries . HasFlag ( QueryParams . Fields ) == false )
94
- querySet . Fields = ParseFieldsQuery ( pair . Key , pair . Value ) ;
90
+ _sparseFieldsService . Parse ( pair . Key , pair . Value ) ;
95
91
continue ;
96
92
}
97
93
98
94
if ( _options . AllowCustomQueryParameters == false )
99
95
throw new JsonApiException ( 400 , $ "{ pair } is not a valid query.") ;
100
96
}
101
-
102
- return querySet ;
97
+ ;
103
98
}
104
99
105
-
100
+ private void GetQueryParameterServices ( )
101
+ {
102
+ var type = typeof ( IQueryParameterService ) ;
103
+ var types = AppDomain . CurrentDomain . GetAssemblies ( )
104
+ . SelectMany ( a => a . GetTypes ( ) )
105
+ . Where ( t => t . IsInterface && t . Inherits ( type ) )
106
+ . Select ( t => ( IQueryParameterService ) _sp . GetService ( t ) ) ;
107
+ }
106
108
107
109
protected virtual List < FilterQuery > ParseFilterQuery ( string key , string value )
108
110
{
@@ -204,45 +206,6 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
204
206
}
205
207
206
208
207
- protected virtual List < string > ParseFieldsQuery ( string key , string value )
208
- {
209
- // expected: fields[TYPE]=prop1,prop2
210
- var typeName = key . Split ( QueryConstants . OPEN_BRACKET , QueryConstants . CLOSE_BRACKET ) [ 1 ] ;
211
- var includedFields = new List < string > { nameof ( Identifiable . Id ) } ;
212
-
213
- var relationship = _primaryResource . Relationships . SingleOrDefault ( a => a . Is ( typeName ) ) ;
214
- if ( relationship == default && string . Equals ( typeName , _primaryResource . EntityName , StringComparison . OrdinalIgnoreCase ) == false )
215
- return includedFields ;
216
-
217
- var fields = value . Split ( QueryConstants . COMMA ) ;
218
- foreach ( var field in fields )
219
- {
220
- if ( relationship != default )
221
- {
222
- var relationProperty = _provider . GetContextEntity ( relationship . DependentType ) ;
223
- var attr = relationProperty . Attributes . SingleOrDefault ( a => a . Is ( field ) ) ;
224
- if ( attr == null )
225
- throw new JsonApiException ( 400 , $ "'{ relationship . DependentType . Name } ' does not contain '{ field } '.") ;
226
-
227
- _fieldQuery . Register ( attr , relationship ) ;
228
- // e.g. "Owner.Name"
229
- includedFields . Add ( relationship . InternalRelationshipName + "." + attr . InternalAttributeName ) ;
230
-
231
- }
232
- else
233
- {
234
- var attr = _primaryResource . Attributes . SingleOrDefault ( a => a . Is ( field ) ) ;
235
- if ( attr == null )
236
- throw new JsonApiException ( 400 , $ "'{ _primaryResource . EntityName } ' does not contain '{ field } '.") ;
237
-
238
- _fieldQuery . Register ( attr , relationship ) ;
239
- // e.g. "Name"
240
- includedFields . Add ( attr . InternalAttributeName ) ;
241
- }
242
- }
243
-
244
- return includedFields ;
245
- }
246
209
247
210
protected virtual AttrAttribute GetAttribute ( string propertyName )
248
211
{
0 commit comments