1+ using System ;
2+ using System . Linq ;
3+ using JsonApiDotNetCore . Models ;
4+ using JsonApiDotNetCore . Services ;
5+
6+ namespace JsonApiDotNetCore . Internal . Query
7+ {
8+ public class AttrFilterQuery
9+ {
10+ private readonly IJsonApiContext _jsonApiContext ;
11+
12+ public AttrFilterQuery (
13+ IJsonApiContext jsonApiCopntext ,
14+ FilterQuery filterQuery )
15+ {
16+ _jsonApiContext = jsonApiCopntext ;
17+
18+ var attribute = GetAttribute ( filterQuery . Key ) ;
19+
20+ if ( attribute == null )
21+ throw new JsonApiException ( "400" , $ "{ filterQuery . Key } is not a valid property.") ;
22+
23+ FilteredAttribute = attribute ;
24+ PropertyValue = filterQuery . Value ;
25+ FilterOperation = GetFilterOperation ( filterQuery . Operation ) ;
26+ }
27+
28+ public AttrAttribute FilteredAttribute { get ; set ; }
29+ public string PropertyValue { get ; set ; }
30+ public FilterOperations FilterOperation { get ; set ; }
31+
32+ private FilterOperations GetFilterOperation ( string prefix )
33+ {
34+ if ( prefix . Length == 0 ) return FilterOperations . eq ;
35+
36+ FilterOperations opertion ;
37+ if ( ! Enum . TryParse < FilterOperations > ( prefix , out opertion ) )
38+ throw new JsonApiException ( "400" , $ "Invalid filter prefix '{ prefix } '") ;
39+
40+ return opertion ;
41+ }
42+
43+ private AttrAttribute GetAttribute ( string propertyName )
44+ {
45+ return _jsonApiContext . RequestEntity . Attributes
46+ . FirstOrDefault ( attr =>
47+ attr . InternalAttributeName . ToLower ( ) == propertyName . ToLower ( )
48+ ) ;
49+ }
50+ }
51+ }
0 commit comments