Skip to content

Commit 2f3e5d1

Browse files
MilosMilos
authored andcommitted
AttrQuery and RelatedAttrQuery constructor overload.
1 parent 8d46f52 commit 2f3e5d1

File tree

7 files changed

+70
-110
lines changed

7 files changed

+70
-110
lines changed

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public static IOrderedQueryable<TSource> Sort<TSource>(this IQueryable<TSource>
4949
{
5050
if (sortQuery.IsAttributeOfRelationship)
5151
{
52-
var relatedAttrQuery = new RelatedAttrSortQuery(jsonApiContext, sortQuery);
52+
var relatedAttrQuery = new RelatedAttrQuery(jsonApiContext, sortQuery);
5353
return sortQuery.Direction == SortDirection.Descending
5454
? source.OrderByDescending(relatedAttrQuery)
5555
: source.OrderBy(relatedAttrQuery);
5656
}
5757
else
5858
{
59-
var attrQuery = new AttrSortQuery(jsonApiContext, sortQuery);
59+
var attrQuery = new AttrQuery(jsonApiContext, sortQuery);
6060
return sortQuery.Direction == SortDirection.Descending
6161
? source.OrderByDescending(attrQuery)
6262
: source.OrderBy(attrQuery);
@@ -67,14 +67,14 @@ public static IOrderedQueryable<TSource> Sort<TSource>(this IOrderedQueryable<TS
6767
{
6868
if (sortQuery.IsAttributeOfRelationship)
6969
{
70-
var relatedAttrQuery = new RelatedAttrSortQuery(jsonApiContext, sortQuery);
70+
var relatedAttrQuery = new RelatedAttrQuery(jsonApiContext, sortQuery);
7171
return sortQuery.Direction == SortDirection.Descending
7272
? source.OrderByDescending(relatedAttrQuery)
7373
: source.OrderBy(relatedAttrQuery);
7474
}
7575
else
7676
{
77-
var attrQuery = new AttrSortQuery(jsonApiContext, sortQuery);
77+
var attrQuery = new AttrQuery(jsonApiContext, sortQuery);
7878
return sortQuery.Direction == SortDirection.Descending
7979
? source.OrderByDescending(attrQuery)
8080
: source.OrderBy(attrQuery);
@@ -152,12 +152,12 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
152152
return source;
153153

154154
if (filterQuery.IsAttributeOfRelationship)
155-
return source.Filter(new RelatedAttrFilterQuery(jsonApiContext, filterQuery));
155+
return source.Filter(new RelatedAttrQuery(jsonApiContext, filterQuery));
156156

157-
return source.Filter(new AttrFilterQuery(jsonApiContext, filterQuery));
157+
return source.Filter(new AttrQuery(jsonApiContext, filterQuery));
158158
}
159159

160-
public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, AttrFilterQuery filterQuery)
160+
public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, AttrQuery filterQuery)
161161
{
162162
if (filterQuery == null)
163163
return source;
@@ -214,7 +214,7 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
214214
}
215215
}
216216

217-
public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, RelatedAttrFilterQuery filterQuery)
217+
public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, RelatedAttrQuery filterQuery)
218218
{
219219
if (filterQuery == null)
220220
return source;

src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/JsonApiDotNetCore/Internal/Query/AttrQuery.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,46 @@ namespace JsonApiDotNetCore.Internal.Query
88
public class AttrQuery
99
{
1010
private readonly IJsonApiContext _jsonApiContext;
11+
public AttrAttribute Attribute { get; }
12+
13+
// Filter properties
14+
public string PropertyValue { get; }
15+
public FilterOperations FilterOperation { get; }
16+
// Sort properties
17+
public SortDirection Direction { get; set; }
1118

12-
public AttrQuery(IJsonApiContext jsonApiContext, QueryAttribute query)
19+
/// <summary>
20+
/// Build AttrQuery base on FilterQuery values.
21+
/// </summary>
22+
/// <param name="jsonApiContext"></param>
23+
/// <param name="query"></param>
24+
public AttrQuery(IJsonApiContext jsonApiContext, FilterQuery query)
1325
{
1426
_jsonApiContext = jsonApiContext;
1527
Attribute = GetAttribute(query.Attribute);
28+
29+
if (Attribute.IsFilterable == false)
30+
throw new JsonApiException(400, $"Filter is not allowed for attribute '{Attribute.PublicAttributeName}'.");
31+
32+
PropertyValue = query.Value;
33+
FilterOperation = query.OperationType;
1634
}
1735

18-
public AttrAttribute Attribute { get; }
36+
/// <summary>
37+
/// Build AttrQuery base on SortQuery values.
38+
/// </summary>
39+
/// <param name="jsonApiContext"></param>
40+
/// <param name="query"></param>
41+
public AttrQuery(IJsonApiContext jsonApiContext, SortQuery sortQuery)
42+
{
43+
_jsonApiContext = jsonApiContext;
44+
Attribute = GetAttribute(sortQuery.Attribute);
45+
46+
if (Attribute.IsSortable == false)
47+
throw new JsonApiException(400, $"Sort is not allowed for attribute '{Attribute.PublicAttributeName}'.");
48+
49+
Direction = sortQuery.Direction;
50+
}
1951

2052
private AttrAttribute GetAttribute(string attribute)
2153
{

src/JsonApiDotNetCore/Internal/Query/AttrSortQuery.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/JsonApiDotNetCore/Internal/Query/RelatedAttrFilterQuery.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/JsonApiDotNetCore/Internal/Query/RelatedAttrQuery.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,40 @@ namespace JsonApiDotNetCore.Internal.Query
88
public class RelatedAttrQuery
99
{
1010
private readonly IJsonApiContext _jsonApiContext;
11+
public AttrAttribute Attribute { get; }
12+
public RelationshipAttribute RelationshipAttribute { get; }
13+
14+
// Filter properties
15+
public string PropertyValue { get; }
16+
public FilterOperations FilterOperation { get; }
17+
// Sort properties
18+
public SortDirection Direction { get; set; }
1119

12-
public RelatedAttrQuery(IJsonApiContext jsonApiContext, QueryAttribute query)
20+
public RelatedAttrQuery(IJsonApiContext jsonApiContext, FilterQuery filterQuery)
1321
{
1422
_jsonApiContext = jsonApiContext;
1523

16-
RelationshipAttribute = GetRelationshipAttribute(query.RelationshipAttribute);
17-
Attribute = GetAttribute(RelationshipAttribute, query.Attribute);
24+
RelationshipAttribute = GetRelationshipAttribute(filterQuery.RelationshipAttribute);
25+
Attribute = GetAttribute(RelationshipAttribute, filterQuery.Attribute);
26+
27+
if (Attribute.IsFilterable == false)
28+
throw new JsonApiException(400, $"Filter is not allowed for attribute '{Attribute.PublicAttributeName}'.");
29+
30+
PropertyValue = filterQuery.Value;
31+
FilterOperation = filterQuery.OperationType;
1832
}
1933

20-
public AttrAttribute Attribute { get; }
21-
public RelationshipAttribute RelationshipAttribute { get; }
34+
public RelatedAttrQuery(IJsonApiContext jsonApiContext, SortQuery sortQuery)
35+
{
36+
_jsonApiContext = jsonApiContext;
37+
RelationshipAttribute = GetRelationshipAttribute(sortQuery.RelationshipAttribute);
38+
Attribute = GetAttribute(RelationshipAttribute, sortQuery.Attribute);
39+
40+
if (Attribute.IsSortable == false)
41+
throw new JsonApiException(400, $"Sort is not allowed for attribute '{Attribute.PublicAttributeName}'.");
42+
43+
Direction = sortQuery.Direction;
44+
}
2245

2346
private RelationshipAttribute GetRelationshipAttribute(string relationship)
2447
{

src/JsonApiDotNetCore/Internal/Query/RelatedAttrSortQuery.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)