Skip to content

Commit 94fdff2

Browse files
MilosMilos
authored andcommitted
Nested sort - Init
1 parent 491a6cb commit 94fdff2

File tree

20 files changed

+517
-180
lines changed

20 files changed

+517
-180
lines changed

src/Examples/JsonApiDotNetCoreExample/Models/Person.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class Person : Identifiable, IHasMeta
1919
[Attr("last-name")]
2020
public string LastName { get; set; }
2121

22+
[Attr("age")]
23+
public int Age { get; set; }
24+
2225
[HasMany("todo-items")]
2326
public virtual List<TodoItem> TodoItems { get; set; }
2427

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private bool ShouldIncludeAttribute(AttrAttribute attr, object attributeValue)
142142
return OmitNullValuedAttribute(attr, attributeValue) == false
143143
&& ((_jsonApiContext.QuerySet == null
144144
|| _jsonApiContext.QuerySet.Fields.Count == 0)
145-
|| _jsonApiContext.QuerySet.Fields.Contains(attr.InternalAttributeName));
145+
|| _jsonApiContext.QuerySet.Fields.Any(i => i.Attribute == attr.InternalAttributeName));
146146
}
147147

148148
private bool OmitNullValuedAttribute(AttrAttribute attr, object attributeValue)

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public DefaultEntityRepository(
5858
public virtual IQueryable<TEntity> Get()
5959
{
6060
if (_jsonApiContext.QuerySet?.Fields != null && _jsonApiContext.QuerySet.Fields.Count > 0)
61-
return _dbSet.Select(_jsonApiContext.QuerySet?.Fields);
61+
return _dbSet.Select(_jsonApiContext.QuerySet.Fields);
6262

6363
return _dbSet;
6464
}
@@ -72,7 +72,7 @@ public virtual IQueryable<TEntity> Filter(IQueryable<TEntity> entities, FilterQu
7272
/// <inheritdoc />
7373
public virtual IQueryable<TEntity> Sort(IQueryable<TEntity> entities, List<SortQuery> sortQueries)
7474
{
75-
return entities.Sort(sortQueries);
75+
return entities.Sort(_jsonApiContext, sortQueries);
7676
}
7777

7878
/// <inheritdoc />

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs

Lines changed: 127 additions & 52 deletions
Large diffs are not rendered by default.

src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,22 @@
44

55
namespace JsonApiDotNetCore.Internal.Query
66
{
7-
public class AttrFilterQuery : BaseFilterQuery
7+
public class AttrFilterQuery : AttrQuery
88
{
9-
private readonly IJsonApiContext _jsonApiContext;
10-
119
public AttrFilterQuery(
1210
IJsonApiContext jsonApiContext,
1311
FilterQuery filterQuery)
12+
:base(jsonApiContext, filterQuery)
1413
{
15-
_jsonApiContext = jsonApiContext;
16-
17-
var attribute = GetAttribute(filterQuery.Attribute);
18-
19-
if (attribute == null)
20-
throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute.");
21-
22-
if (attribute.IsFilterable == false)
23-
throw new JsonApiException(400, $"Filter is not allowed for attribute '{attribute.PublicAttributeName}'.");
14+
if (Attribute.IsFilterable == false)
15+
throw new JsonApiException(400, $"Filter is not allowed for attribute '{Attribute.PublicAttributeName}'.");
2416

25-
FilteredAttribute = attribute;
2617
PropertyValue = filterQuery.Value;
27-
FilterOperation = GetFilterOperation(filterQuery.Operation);
18+
FilterOperation = FilterOperations.GetFilterOperation(filterQuery.Operation);
2819
}
2920

30-
public AttrAttribute FilteredAttribute { get; }
3121
public string PropertyValue { get; }
32-
public FilterOperations FilterOperation { get; }
22+
public FilterOperationsEnum FilterOperation { get; }
3323

34-
private AttrAttribute GetAttribute(string attribute) =>
35-
_jsonApiContext.RequestEntity.Attributes.FirstOrDefault(attr => attr.Is(attribute));
3624
}
3725
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using JsonApiDotNetCore.Models;
2+
using JsonApiDotNetCore.Services;
3+
using System;
4+
using System.Linq;
5+
6+
namespace JsonApiDotNetCore.Internal.Query
7+
{
8+
public class AttrQuery
9+
{
10+
private readonly IJsonApiContext _jsonApiContext;
11+
12+
public AttrQuery(IJsonApiContext jsonApiContext, QueryAttribute query)
13+
{
14+
_jsonApiContext = jsonApiContext;
15+
Attribute = GetAttribute(query.Attribute);
16+
}
17+
18+
public AttrAttribute Attribute { get; }
19+
20+
private AttrAttribute GetAttribute(string attribute)
21+
{
22+
try
23+
{
24+
return _jsonApiContext
25+
.RequestEntity
26+
.Attributes
27+
.Single(attr => attr.Is(attribute));
28+
}
29+
catch (InvalidOperationException e)
30+
{
31+
throw new JsonApiException(400, $"Attribute '{attribute}' does not exist on resource '{_jsonApiContext.RequestEntity.EntityName}'", e);
32+
}
33+
}
34+
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Linq;
2+
using JsonApiDotNetCore.Models;
3+
using JsonApiDotNetCore.Services;
4+
5+
namespace JsonApiDotNetCore.Internal.Query
6+
{
7+
public class AttrSortQuery : AttrQuery
8+
{
9+
public AttrSortQuery(
10+
IJsonApiContext jsonApiContext,
11+
SortQuery sortQuery)
12+
:base(jsonApiContext, sortQuery)
13+
{
14+
if (Attribute.IsSortable == false)
15+
throw new JsonApiException(400, $"Sort is not allowed for attribute '{Attribute.PublicAttributeName}'.");
16+
17+
Direction = sortQuery.Direction;
18+
}
19+
20+
public SortDirection Direction { get; set; }
21+
22+
}
23+
}

src/JsonApiDotNetCore/Internal/Query/BaseFilterQuery.cs

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

src/JsonApiDotNetCore/Internal/Query/FilterOperations.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// ReSharper disable InconsistentNaming
2+
using System;
3+
24
namespace JsonApiDotNetCore.Internal.Query
35
{
4-
public enum FilterOperations
6+
public enum FilterOperationsEnum
57
{
68
eq = 0,
79
lt = 1,
@@ -15,4 +17,33 @@ public enum FilterOperations
1517
isnull = 9,
1618
isnotnull = 10
1719
}
20+
21+
public class FilterOperations
22+
{
23+
public static FilterOperationsEnum GetFilterOperation(string prefix)
24+
{
25+
if (prefix.Length == 0) return FilterOperationsEnum.eq;
26+
27+
if (Enum.TryParse(prefix, out FilterOperationsEnum opertion) == false)
28+
throw new JsonApiException(400, $"Invalid filter prefix '{prefix}'");
29+
30+
return opertion;
31+
}
32+
33+
public static string GetFilterOperationFromQuery(string query)
34+
{
35+
var values = query.Split(QueryConstants.COLON);
36+
37+
if (values.Length == 1)
38+
return string.Empty;
39+
40+
var operation = values[0];
41+
// remove prefix from value
42+
if (Enum.TryParse(operation, out FilterOperationsEnum op) == false)
43+
return string.Empty;
44+
45+
return operation;
46+
}
47+
48+
}
1849
}

src/JsonApiDotNetCore/Internal/Query/FilterQuery.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace JsonApiDotNetCore.Internal.Query
55
{
6-
public class FilterQuery
6+
public class FilterQuery: QueryAttribute
77
{
88
public FilterQuery(string attribute, string value, string operation)
9+
:base(attribute)
910
{
10-
Attribute = attribute;
1111
Key = attribute.ToProperCase();
1212
Value = value;
1313
Operation = operation;
1414
}
1515

1616
[Obsolete("Key has been replaced by '" + nameof(Attribute) + "'. Members should be located by their public name, not by coercing the provided value to the internal name.")]
1717
public string Key { get; set; }
18-
public string Attribute { get; }
1918
public string Value { get; set; }
2019
public string Operation { get; set; }
21-
public bool IsAttributeOfRelationship => Attribute.Contains(".");
20+
2221
}
2322
}

0 commit comments

Comments
 (0)