2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
4
using System . Linq ;
5
+ using System . Linq . Expressions ;
6
+ using System . Reflection ;
5
7
using JsonApiDotNetCore . Internal ;
6
8
using JsonApiDotNetCore . Models ;
7
9
@@ -15,13 +17,9 @@ public interface IRelationshipsDictionary { }
15
17
/// <summary>
16
18
/// An interface that is implemented to expose a relationship dictionary on another class.
17
19
/// </summary>
18
- public interface IByAffectedRelationships < TDependentResource > :
20
+ public interface IByAffectedRelationships < TDependentResource > :
19
21
IRelationshipGetters < TDependentResource > where TDependentResource : class , IIdentifiable
20
22
{
21
- /// todo: expose getters that behave something like this:
22
- /// relationshipDictionary.GetAffected( entity => entity.NavigationProperty ).
23
- /// see https://stackoverflow.com/a/17116267/4441216
24
-
25
23
/// <summary>
26
24
/// Gets a dictionary of affected resources grouped by affected relationships.
27
25
/// </summary>
@@ -31,10 +29,11 @@ public interface IByAffectedRelationships<TDependentResource> :
31
29
/// <summary>
32
30
/// A helper class that provides insights in which relationships have been updated for which entities.
33
31
/// </summary>
34
- public interface IRelationshipsDictionary < TDependentResource > :
35
- IRelationshipGetters < TDependentResource > ,
36
- IReadOnlyDictionary < RelationshipAttribute , HashSet < TDependentResource > > ,
37
- IRelationshipsDictionary where TDependentResource : class , IIdentifiable { }
32
+ public interface IRelationshipsDictionary < TDependentResource > :
33
+ IRelationshipGetters < TDependentResource > ,
34
+ IReadOnlyDictionary < RelationshipAttribute , HashSet < TDependentResource > > ,
35
+ IRelationshipsDictionary where TDependentResource : class , IIdentifiable
36
+ { }
38
37
39
38
/// <summary>
40
39
/// A helper class that provides insights in which relationships have been updated for which entities.
@@ -49,16 +48,24 @@ public interface IRelationshipGetters<TResource> where TResource : class, IIdent
49
48
/// Gets a dictionary of all entities that have an affected relationship to type <paramref name="principalType"/>
50
49
/// </summary>
51
50
Dictionary < RelationshipAttribute , HashSet < TResource > > GetByRelationship ( Type relatedResourceType ) ;
51
+
52
+ /// <summary>
53
+ /// Gets a collection of all the entities for the property within <paramref name="NavigationAction"/>
54
+ /// has been affected by the request
55
+ /// </summary>
56
+ /// <param name="NavigationAction"></param>
57
+ HashSet < TResource > GetAffected ( Expression < Func < TResource , object > > NavigationAction ) ;
52
58
}
53
59
60
+
54
61
/// <summary>
55
62
/// Implementation of IAffectedRelationships{TDependentResource}
56
63
///
57
64
/// It is practically a ReadOnlyDictionary{RelationshipAttribute, HashSet{TDependentResource}} dictionary
58
65
/// with the two helper methods defined on IAffectedRelationships{TDependentResource}.
59
66
/// </summary>
60
67
public class RelationshipsDictionary < TResource > :
61
- Dictionary < RelationshipAttribute , HashSet < TResource > > ,
68
+ Dictionary < RelationshipAttribute , HashSet < TResource > > ,
62
69
IRelationshipsDictionary < TResource > where TResource : class , IIdentifiable
63
70
{
64
71
/// <summary>
@@ -70,7 +77,7 @@ public RelationshipsDictionary(Dictionary<RelationshipAttribute, HashSet<TResour
70
77
/// <summary>
71
78
/// Used internally by the ResourceHookExecutor to make live a bit easier with generics
72
79
/// </summary>
73
- internal RelationshipsDictionary ( Dictionary < RelationshipAttribute , IEnumerable > relationships )
80
+ internal RelationshipsDictionary ( Dictionary < RelationshipAttribute , IEnumerable > relationships )
74
81
: this ( TypeHelper . ConvertRelationshipDictionary < TResource > ( relationships ) ) { }
75
82
76
83
/// <inheritdoc />
@@ -84,5 +91,12 @@ public Dictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship(T
84
91
{
85
92
return this . Where ( p => p . Key . DependentType == relatedType ) . ToDictionary ( p => p . Key , p => p . Value ) ;
86
93
}
94
+
95
+ /// <inheritdoc />
96
+ public HashSet < TResource > GetAffected ( Expression < Func < TResource , object > > NavigationAction )
97
+ {
98
+ var property = TypeHelper . ParseNavigationExpression ( NavigationAction ) ;
99
+ return this . Where ( p => p . Key . InternalRelationshipName == property . Name ) . Select ( p => p . Value ) . SingleOrDefault ( ) ;
100
+ }
87
101
}
88
102
}
0 commit comments