22using System . Collections ;
33using System . Collections . Generic ;
44using System . Linq ;
5+ using JsonApiDotNetCore . Internal ;
56using JsonApiDotNetCore . Models ;
67
78namespace JsonApiDotNetCore . Hooks
89{
910 /// <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.
1313 ///
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 "/>>
1616 /// </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
1818 {
1919 /// <summary>
20- /// the current database values of the affected resources collection .
20+ /// The database values of the resources affected by the request .
2121 /// </summary>
2222 HashSet < TResource > DatabaseValues { get ; }
2323
2424 /// <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.
2726 /// </summary>
28- IEnumerable < ResourceDiffPair < TResource > > GetDiffs ( ) ;
27+ HashSet < TResource > Resources { get ; }
2928 }
3029
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
3232 {
3333 private readonly HashSet < TResource > _databaseValues ;
3434 private readonly bool _databaseValuesLoaded ;
35+
3536 /// <inheritdoc />
3637 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 ; }
3742
38- public AffectedResourceDiff ( HashSet < TResource > requestEntities ,
43+ public AffectedResourcesDiffs ( HashSet < TResource > requestEntities ,
3944 HashSet < TResource > databaseEntities ,
40- Dictionary < RelationshipAttribute , HashSet < TResource > > relationships ) : base ( requestEntities , relationships )
45+ Dictionary < RelationshipAttribute , HashSet < TResource > > relationships )
4146 {
47+ Resources = requestEntities ;
48+ AffectedRelationships = new RelationshipsDictionary < TResource > ( relationships ) ;
4249 _databaseValues = databaseEntities ;
4350 _databaseValuesLoaded |= _databaseValues != null ;
4451 }
4552
4653 /// <summary>
4754 /// Used internally by the ResourceHookExecutor to make live a bit easier with generics
4855 /// </summary>
49- internal AffectedResourceDiff ( IEnumerable requestEntities ,
56+ internal AffectedResourcesDiffs ( IEnumerable requestEntities ,
5057 IEnumerable databaseEntities ,
5158 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+ }
5367
5468 /// <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 ( )
5676 {
5777 if ( ! _databaseValuesLoaded ) ThrowNoDbValuesError ( ) ;
5878
@@ -64,14 +84,18 @@ public IEnumerable<ResourceDiffPair<TResource>> GetDiffs()
6484 }
6585 }
6686
87+ /// <inheritdoc />
88+ IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
89+
6790 private HashSet < TResource > ThrowNoDbValuesError ( )
6891 {
6992 throw new MemberAccessException ( "Cannot access database entities if the LoadDatabaseValues option is set to false" ) ;
7093 }
7194 }
7295
7396 /// <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
7599 /// </summary>
76100 public class ResourceDiffPair < TResource > where TResource : class , IIdentifiable
77101 {
0 commit comments