Skip to content

Commit c7390ad

Browse files
committed
refactor(FilterQuery): generalize query using string values
1 parent 0faabbe commit c7390ad

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
using JsonApiDotNetCore.Models;
2-
31
namespace JsonApiDotNetCore.Internal.Query
42
{
53
public class FilterQuery
64
{
7-
public FilterQuery(AttrAttribute filteredAttribute, string propertyValue, FilterOperations filterOperation)
5+
public FilterQuery(string key, string value, string operation)
86
{
9-
FilteredAttribute = filteredAttribute;
10-
PropertyValue = propertyValue;
11-
FilterOperation = filterOperation;
7+
Key = key;
8+
Value = value;
9+
Operation = operation;
1210
}
1311

14-
public AttrAttribute FilteredAttribute { get; set; }
15-
public string PropertyValue { get; set; }
16-
public FilterOperations FilterOperation { get; set; }
12+
public string Key { get; set; }
13+
public string Value { get; set; }
14+
public string Operation { get; set; }
1715
}
1816
}

src/JsonApiDotNetCore/Internal/Query/QuerySet.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,49 +74,32 @@ private List<FilterQuery> ParseFilterQuery(string key, string value)
7474
var queries = new List<FilterQuery>();
7575

7676
var propertyName = key.Split('[', ']')[1].ToProperCase();
77-
var attribute = GetAttribute(propertyName);
78-
79-
if (attribute == null)
80-
throw new JsonApiException("400", $"{propertyName} is not a valid property.");
8177

8278
var values = value.Split(',');
8379
foreach(var val in values)
84-
queries.Add(ParseFilterOperation(attribute, val));
80+
{
81+
(var operation, var filterValue) = ParseFilterOperation(val);
82+
queries.Add(new FilterQuery(propertyName, filterValue, operation));
83+
}
8584

8685
return queries;
8786
}
8887

89-
private FilterQuery ParseFilterOperation(AttrAttribute attribute, string value)
88+
private (string operation, string value) ParseFilterOperation(string value)
9089
{
9190
if(value.Length < 3)
92-
return new FilterQuery(attribute, value, FilterOperations.eq);
91+
return (string.Empty, value);
9392

9493
var operation = value.Split(':');
9594

9695
if(operation.Length == 1)
97-
return new FilterQuery(attribute, value, FilterOperations.eq);
96+
return (string.Empty, value);
9897

9998
// remove prefix from value
10099
var prefix = operation[0];
101100
value = operation[1];
102101

103-
switch(prefix)
104-
{
105-
case "eq":
106-
return new FilterQuery(attribute, value, FilterOperations.eq);
107-
case "lt":
108-
return new FilterQuery(attribute, value, FilterOperations.lt);
109-
case "gt":
110-
return new FilterQuery(attribute, value, FilterOperations.gt);
111-
case "le":
112-
return new FilterQuery(attribute, value, FilterOperations.le);
113-
case "ge":
114-
return new FilterQuery(attribute, value, FilterOperations.ge);
115-
case "like":
116-
return new FilterQuery(attribute, value, FilterOperations.like);
117-
}
118-
119-
throw new JsonApiException("400", $"Invalid filter prefix '{prefix}'");
102+
return (prefix, value);;
120103
}
121104

122105
private PageQuery ParsePageQuery(string key, string value)

0 commit comments

Comments
 (0)