@@ -85,29 +85,32 @@ protected virtual List<FilterQuery> ParseFilterQuery(string key, string value)
85
85
var propertyName = key . Split ( QueryConstants . OPEN_BRACKET , QueryConstants . CLOSE_BRACKET ) [ 1 ] ;
86
86
87
87
// InArray case
88
- var arrOpVal = ParseFilterOperationAndValue ( value ) ;
89
- if ( arrOpVal . operation == FilterOperations . @in || arrOpVal . operation == FilterOperations . nin )
90
- queries . Add ( new FilterQuery ( propertyName , arrOpVal . value , arrOpVal . operation ) ) ;
88
+ string op = GetFilterOperation ( value ) ;
89
+ if ( string . Equals ( op , FilterOperations . @in . ToString ( ) , StringComparison . OrdinalIgnoreCase )
90
+ || string . Equals ( op , FilterOperations . nin . ToString ( ) , StringComparison . OrdinalIgnoreCase ) )
91
+ {
92
+ ( var operation , var filterValue ) = ParseFilterOperation ( value ) ;
93
+ queries . Add ( new FilterQuery ( propertyName , filterValue , op ) ) ;
94
+ }
91
95
else
92
96
{
93
97
var values = value . Split ( QueryConstants . COMMA ) ;
94
98
foreach ( var val in values )
95
99
{
96
- var opVal = ParseFilterOperationAndValue ( val ) ;
97
- queries . Add ( new FilterQuery ( propertyName , opVal . value , opVal . operation ) ) ;
100
+ ( var operation , var filterValue ) = ParseFilterOperation ( val ) ;
101
+ queries . Add ( new FilterQuery ( propertyName , filterValue , operation ) ) ;
98
102
}
99
103
}
100
104
101
105
return queries ;
102
106
}
103
107
104
- [ Obsolete ( "Use " + nameof ( ParseFilterOperationAndValue ) + " method instead." ) ]
105
108
protected virtual ( string operation , string value ) ParseFilterOperation ( string value )
106
109
{
107
110
if ( value . Length < 3 )
108
111
return ( string . Empty , value ) ;
109
112
110
- var operation = GetFilterOperationOld ( value ) ;
113
+ var operation = GetFilterOperation ( value ) ;
111
114
var values = value . Split ( QueryConstants . COLON ) ;
112
115
113
116
if ( string . IsNullOrEmpty ( operation ) )
@@ -118,63 +121,6 @@ protected virtual (string operation, string value) ParseFilterOperation(string v
118
121
return ( operation , value ) ;
119
122
}
120
123
121
- /// <summary>
122
- /// Parse filter operation enum and value by string value.
123
- /// Input string can contain:
124
- /// a) property value only, then FilterOperations.eq and value is returned
125
- /// b) filter prefix and value e.g. "prefix:value", then FilterOperations.prefix and value is returned
126
- /// In case of prefix is provided and is not in FilterOperations enum,
127
- /// invalid filter prefix exception is thrown.
128
- /// </summary>
129
- /// <param name="input"></param>
130
- /// <returns></returns>
131
- protected virtual ( FilterOperations operation , string value ) ParseFilterOperationAndValue ( string input )
132
- {
133
- // value is empty
134
- if ( input . Length == 0 )
135
- return ( FilterOperations . eq , input ) ;
136
-
137
- // split value
138
- var values = input . Split ( QueryConstants . COLON ) ;
139
- // value only
140
- if ( values . Length == 1 )
141
- return ( FilterOperations . eq , input ) ;
142
- // prefix:value
143
- else if ( values . Length == 2 )
144
- {
145
- var ( operation , succeeded ) = GetFilterOperation ( values [ 0 ] ) ;
146
- if ( succeeded == false )
147
- throw new JsonApiException ( 400 , $ "Invalid filter prefix '{ values [ 0 ] } '") ;
148
-
149
- return ( operation , values [ 1 ] ) ;
150
- }
151
- // some:colon:value OR prefix:some:colon:value (datetime)
152
- else
153
- {
154
- // succeeded = false means no prefix found => some value with colons(datetime)
155
- // succeeded = true means prefix provide + some value with colons(datetime)
156
- var ( operation , succeeded ) = GetFilterOperation ( values [ 0 ] ) ;
157
- var value = "" ;
158
- // datetime
159
- if ( succeeded == false )
160
- value = string . Join ( QueryConstants . COLON_STR , values ) ;
161
- else
162
- value = string . Join ( QueryConstants . COLON_STR , values . Skip ( 1 ) ) ;
163
- return ( operation , value ) ;
164
- }
165
- }
166
-
167
- /// <summary>
168
- /// Returns typed operation result and info about parsing success
169
- /// </summary>
170
- /// <param name="operation">String represented operation</param>
171
- /// <returns></returns>
172
- private static ( FilterOperations operation , bool succeeded ) GetFilterOperation ( string operation )
173
- {
174
- var success = Enum . TryParse ( operation , out FilterOperations opertion ) ;
175
- return ( opertion , success ) ;
176
- }
177
-
178
124
protected virtual PageQuery ParsePageQuery ( PageQuery pageQuery , string key , string value )
179
125
{
180
126
// expected input = page[size]=10
@@ -247,15 +193,35 @@ protected virtual List<string> ParseFieldsQuery(string key, string value)
247
193
var fields = value . Split ( QueryConstants . COMMA ) ;
248
194
foreach ( var field in fields )
249
195
{
250
- var attr = GetAttribute ( field ) ;
196
+ var attr = _controllerContext . RequestEntity
197
+ . Attributes
198
+ . SingleOrDefault ( a => a . Is ( field ) ) ;
199
+
200
+ if ( attr == null ) throw new JsonApiException ( 400 , $ "'{ _controllerContext . RequestEntity . EntityName } ' does not contain '{ field } '.") ;
201
+
251
202
var internalAttrName = attr . InternalAttributeName ;
252
203
includedFields . Add ( internalAttrName ) ;
253
204
}
254
205
255
206
return includedFields ;
256
207
}
257
208
258
- private string GetFilterOperationOld ( string value )
209
+ protected virtual AttrAttribute GetAttribute ( string propertyName )
210
+ {
211
+ try
212
+ {
213
+ return _controllerContext
214
+ . RequestEntity
215
+ . Attributes
216
+ . Single ( attr => attr . Is ( propertyName ) ) ;
217
+ }
218
+ catch ( InvalidOperationException e )
219
+ {
220
+ throw new JsonApiException ( 400 , $ "Attribute '{ propertyName } ' does not exist on resource '{ _controllerContext . RequestEntity . EntityName } '", e ) ;
221
+ }
222
+ }
223
+
224
+ private string GetFilterOperation ( string value )
259
225
{
260
226
var values = value . Split ( QueryConstants . COLON ) ;
261
227
@@ -269,20 +235,5 @@ private string GetFilterOperationOld(string value)
269
235
270
236
return operation ;
271
237
}
272
-
273
- protected virtual AttrAttribute GetAttribute ( string attribute )
274
- {
275
- try
276
- {
277
- return _controllerContext
278
- . RequestEntity
279
- . Attributes
280
- . Single ( attr => attr . Is ( attribute ) ) ;
281
- }
282
- catch ( InvalidOperationException e )
283
- {
284
- throw new JsonApiException ( 400 , $ "Attribute '{ attribute } ' does not exist on resource '{ _controllerContext . RequestEntity . EntityName } '", e ) ;
285
- }
286
- }
287
238
}
288
239
}
0 commit comments