@@ -85,29 +85,32 @@ protected virtual List<FilterQuery> ParseFilterQuery(string key, string value)
8585 var propertyName = key . Split ( QueryConstants . OPEN_BRACKET , QueryConstants . CLOSE_BRACKET ) [ 1 ] ;
8686
8787 // 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+ }
9195 else
9296 {
9397 var values = value . Split ( QueryConstants . COMMA ) ;
9498 foreach ( var val in values )
9599 {
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 ) ) ;
98102 }
99103 }
100104
101105 return queries ;
102106 }
103107
104- [ Obsolete ( "Use " + nameof ( ParseFilterOperationAndValue ) + " method instead." ) ]
105108 protected virtual ( string operation , string value ) ParseFilterOperation ( string value )
106109 {
107110 if ( value . Length < 3 )
108111 return ( string . Empty , value ) ;
109112
110- var operation = GetFilterOperationOld ( value ) ;
113+ var operation = GetFilterOperation ( value ) ;
111114 var values = value . Split ( QueryConstants . COLON ) ;
112115
113116 if ( string . IsNullOrEmpty ( operation ) )
@@ -118,63 +121,6 @@ protected virtual (string operation, string value) ParseFilterOperation(string v
118121 return ( operation , value ) ;
119122 }
120123
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-
178124 protected virtual PageQuery ParsePageQuery ( PageQuery pageQuery , string key , string value )
179125 {
180126 // expected input = page[size]=10
@@ -247,15 +193,35 @@ protected virtual List<string> ParseFieldsQuery(string key, string value)
247193 var fields = value . Split ( QueryConstants . COMMA ) ;
248194 foreach ( var field in fields )
249195 {
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+
251202 var internalAttrName = attr . InternalAttributeName ;
252203 includedFields . Add ( internalAttrName ) ;
253204 }
254205
255206 return includedFields ;
256207 }
257208
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 )
259225 {
260226 var values = value . Split ( QueryConstants . COLON ) ;
261227
@@ -269,20 +235,5 @@ private string GetFilterOperationOld(string value)
269235
270236 return operation ;
271237 }
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- }
287238 }
288239}
0 commit comments