Skip to content

Commit 1cc467d

Browse files
committed
feat(filter-query): add RelatedAttrFilterQuery for relational filters
1 parent 9089bbd commit 1cc467d

File tree

4 files changed

+100
-42
lines changed

4 files changed

+100
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,39 @@
1-
using System;
21
using System.Linq;
32
using JsonApiDotNetCore.Models;
43
using JsonApiDotNetCore.Services;
54

65
namespace JsonApiDotNetCore.Internal.Query
76
{
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)
7+
public class AttrFilterQuery : BaseFilterQuery
448
{
45-
return _jsonApiContext.RequestEntity.Attributes
46-
.FirstOrDefault(attr =>
47-
attr.InternalAttributeName.ToLower() == propertyName.ToLower()
48-
);
9+
private readonly IJsonApiContext _jsonApiContext;
10+
11+
public AttrFilterQuery(
12+
IJsonApiContext jsonApiCopntext,
13+
FilterQuery filterQuery)
14+
{
15+
_jsonApiContext = jsonApiCopntext;
16+
17+
var attribute = GetAttribute(filterQuery.Key);
18+
19+
if (attribute == null)
20+
throw new JsonApiException("400", $"{filterQuery.Key} is not a valid property.");
21+
22+
FilteredAttribute = attribute;
23+
PropertyValue = filterQuery.Value;
24+
FilterOperation = GetFilterOperation(filterQuery.Operation);
25+
}
26+
27+
public AttrAttribute FilteredAttribute { get; set; }
28+
public string PropertyValue { get; set; }
29+
public FilterOperations FilterOperation { get; set; }
30+
31+
private AttrAttribute GetAttribute(string propertyName)
32+
{
33+
return _jsonApiContext.RequestEntity.Attributes
34+
.FirstOrDefault(attr =>
35+
attr.InternalAttributeName.ToLower() == propertyName.ToLower()
36+
);
37+
}
4938
}
50-
}
5139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace JsonApiDotNetCore.Internal.Query
4+
{
5+
public class BaseFilterQuery
6+
{
7+
protected FilterOperations GetFilterOperation(string prefix)
8+
{
9+
if (prefix.Length == 0) return FilterOperations.eq;
10+
11+
FilterOperations opertion;
12+
if (!Enum.TryParse<FilterOperations>(prefix, out opertion))
13+
throw new JsonApiException("400", $"Invalid filter prefix '{prefix}'");
14+
15+
return opertion;
16+
}
17+
}
18+
}

src/JsonApiDotNetCore/Internal/Query/FilterQuery.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public FilterQuery(string key, string value, string operation)
1212
public string Key { get; set; }
1313
public string Value { get; set; }
1414
public string Operation { get; set; }
15+
public bool IsAttributeOfRelationship => Key.Contains(".");
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Linq;
2+
using JsonApiDotNetCore.Models;
3+
using JsonApiDotNetCore.Services;
4+
5+
namespace JsonApiDotNetCore.Internal.Query
6+
{
7+
public class RelatedAttrFilterQuery : BaseFilterQuery
8+
{
9+
private readonly IJsonApiContext _jsonApiContext;
10+
11+
public RelatedAttrFilterQuery(
12+
IJsonApiContext jsonApiCopntext,
13+
FilterQuery filterQuery)
14+
{
15+
_jsonApiContext = jsonApiCopntext;
16+
17+
var relationshipArray = filterQuery.Key.Split('.');
18+
19+
var relationship = GetRelationship(relationshipArray[0]);
20+
if (relationship == null)
21+
throw new JsonApiException("400", $"{relationshipArray[0]} is not a valid relationship.");
22+
23+
var attribute = GetAttribute(relationship, relationshipArray[1]);
24+
if (attribute == null)
25+
throw new JsonApiException("400", $"{relationshipArray[1]} is not a valid attribute on {relationshipArray[0]}.");
26+
27+
FilteredRelationship = relationship;
28+
FilteredAttribute = attribute;
29+
PropertyValue = filterQuery.Value;
30+
FilterOperation = GetFilterOperation(filterQuery.Operation);
31+
}
32+
33+
public AttrAttribute FilteredAttribute { get; set; }
34+
public string PropertyValue { get; set; }
35+
public FilterOperations FilterOperation { get; set; }
36+
public RelationshipAttribute FilteredRelationship { get; private set; }
37+
38+
private RelationshipAttribute GetRelationship(string propertyName)
39+
{
40+
return _jsonApiContext.RequestEntity.Relationships
41+
.FirstOrDefault(r => r.InternalRelationshipName.ToLower() == propertyName.ToLower());
42+
}
43+
44+
private AttrAttribute GetAttribute(RelationshipAttribute relationship, string attribute)
45+
{
46+
var relatedContextExntity = _jsonApiContext.ContextGraph.GetContextEntity(relationship.Type);
47+
return relatedContextExntity.Attributes
48+
.FirstOrDefault(a => a.InternalAttributeName.ToLower() == attribute.ToLower());
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)