2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
4
using System . Linq ;
5
+ using JsonApiDotNetCore . Internal ;
5
6
using JsonApiDotNetCore . Models ;
6
7
7
8
namespace JsonApiDotNetCore . Hooks
8
9
{
9
10
/// <summary>
10
- /// A helper class that provides insight in what is to be updated. The
11
- /// <see cref="IAffectedResourcesDiff{TEntity}.RequestEntities"/> property reflects what was parsed from the incoming request,
12
- /// where the <see cref="IAffectedResourcesDiff{TEntity}.DatabaseValues"/> reflects what is the current state in the database.
11
+ /// A wrapper class that contains information about the resources that are updated by the request.
12
+ /// Contains the resources from the request and the corresponding database values.
13
13
///
14
- /// Any relationships that are updated can be retrieved via the methods implemented on
15
- /// <see cref="IAffectedRelationships{TDependent} "/>.
14
+ /// Also contains information about updated relationships through
15
+ /// implementation of IAffectedRelationshipsDictionary<typeparamref name="TResource "/>>
16
16
/// </summary>
17
- public interface IAffectedResourcesDiff < TResource > : IAffectedResources < TResource > where TResource : class , IIdentifiable
17
+ public interface IAffectedResourcesDiffs < TResource > : IRelationshipsDictionary < TResource > , IEnumerable < ResourceDiffPair < TResource > > where TResource : class , IIdentifiable
18
18
{
19
19
/// <summary>
20
- /// the current database values of the affected resources collection .
20
+ /// The database values of the resources affected by the request .
21
21
/// </summary>
22
22
HashSet < TResource > DatabaseValues { get ; }
23
23
24
24
/// <summary>
25
- /// Matches the resources from the request to the database values that have been loaded
26
- /// and exposes them in ResourceDiffPair wrapper
25
+ /// The resources that were affected by the request.
27
26
/// </summary>
28
- IEnumerable < ResourceDiffPair < TResource > > GetDiffs ( ) ;
27
+ HashSet < TResource > Resources { get ; }
29
28
}
30
29
31
- public class AffectedResourceDiff < TResource > : AffectedResources < TResource > , IAffectedResourcesDiff < TResource > where TResource : class , IIdentifiable
30
+ /// <inheritdoc />
31
+ public class AffectedResourcesDiffs < TResource > : IAffectedResourcesDiffs < TResource > where TResource : class , IIdentifiable
32
32
{
33
33
private readonly HashSet < TResource > _databaseValues ;
34
34
private readonly bool _databaseValuesLoaded ;
35
+
35
36
/// <inheritdoc />
36
37
public HashSet < TResource > DatabaseValues { get => _databaseValues ?? ThrowNoDbValuesError ( ) ; }
38
+ /// <inheritdoc />
39
+ public HashSet < TResource > Resources { get ; private set ; }
40
+ /// <inheritdoc />
41
+ public RelationshipsDictionary < TResource > AffectedRelationships { get ; private set ; }
37
42
38
- public AffectedResourceDiff ( HashSet < TResource > requestEntities ,
43
+ public AffectedResourcesDiffs ( HashSet < TResource > requestEntities ,
39
44
HashSet < TResource > databaseEntities ,
40
- Dictionary < RelationshipAttribute , HashSet < TResource > > relationships ) : base ( requestEntities , relationships )
45
+ Dictionary < RelationshipAttribute , HashSet < TResource > > relationships )
41
46
{
47
+ Resources = requestEntities ;
48
+ AffectedRelationships = new RelationshipsDictionary < TResource > ( relationships ) ;
42
49
_databaseValues = databaseEntities ;
43
50
_databaseValuesLoaded |= _databaseValues != null ;
44
51
}
45
52
46
53
/// <summary>
47
54
/// Used internally by the ResourceHookExecutor to make live a bit easier with generics
48
55
/// </summary>
49
- internal AffectedResourceDiff ( IEnumerable requestEntities ,
56
+ internal AffectedResourcesDiffs ( IEnumerable requestEntities ,
50
57
IEnumerable databaseEntities ,
51
58
Dictionary < RelationshipAttribute , IEnumerable > relationships )
52
- : this ( ( HashSet < TResource > ) requestEntities , ( HashSet < TResource > ) databaseEntities , ConvertRelationshipDictionary ( relationships ) ) { }
59
+ : this ( ( HashSet < TResource > ) requestEntities , ( HashSet < TResource > ) databaseEntities , TypeHelper . ConvertRelationshipDictionary < TResource > ( relationships ) ) { }
60
+
61
+
62
+ /// <inheritdoc />
63
+ public Dictionary < RelationshipAttribute , HashSet < TResource > > GetByRelationship < TPrincipalResource > ( ) where TPrincipalResource : class , IIdentifiable
64
+ {
65
+ return GetByRelationship ( typeof ( TPrincipalResource ) ) ;
66
+ }
53
67
54
68
/// <inheritdoc />
55
- public IEnumerable < ResourceDiffPair < TResource > > GetDiffs ( )
69
+ public Dictionary < RelationshipAttribute , HashSet < TResource > > GetByRelationship ( Type principalType )
70
+ {
71
+ return AffectedRelationships . GetByRelationship ( principalType ) ;
72
+ }
73
+
74
+ /// <inheritdoc />
75
+ public IEnumerator < ResourceDiffPair < TResource > > GetEnumerator ( )
56
76
{
57
77
if ( ! _databaseValuesLoaded ) ThrowNoDbValuesError ( ) ;
58
78
@@ -64,14 +84,18 @@ public IEnumerable<ResourceDiffPair<TResource>> GetDiffs()
64
84
}
65
85
}
66
86
87
+ /// <inheritdoc />
88
+ IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
89
+
67
90
private HashSet < TResource > ThrowNoDbValuesError ( )
68
91
{
69
92
throw new MemberAccessException ( "Cannot access database entities if the LoadDatabaseValues option is set to false" ) ;
70
93
}
71
94
}
72
95
73
96
/// <summary>
74
- /// A wrapper that contains a resource from the request matches to its current database value
97
+ /// A wrapper that contains an resource that is affected by the request,
98
+ /// matched to its current database value
75
99
/// </summary>
76
100
public class ResourceDiffPair < TResource > where TResource : class , IIdentifiable
77
101
{
0 commit comments