1-
2- using System ;
1+ using System ;
32using System . Collections ;
43using System . Collections . Generic ;
54using System . Linq ;
6- using JsonApiDotNetCore . Internal ;
75using JsonApiDotNetCore . Models ;
86
97namespace JsonApiDotNetCore . Hooks
108{
11-
129 /// <summary>
1310 /// A helper class that provides insight in what is to be updated. The
1411 /// <see cref="IEntityDiff{TEntity}.RequestEntities"/> property reflects what was parsed from the incoming request,
@@ -17,29 +14,55 @@ namespace JsonApiDotNetCore.Hooks
1714 /// Any relationships that are updated can be retrieved via the methods implemented on
1815 /// <see cref="IAffectedRelationships{TDependent}"/>.
1916 /// </summary>
20- public interface IEntityDiff < TEntity > : IAffectedRelationships < TEntity > where TEntity : class , IIdentifiable
17+ public interface IEntityDiff < TEntity > : IEnumerable < EntityDiffPair < TEntity > > , IAffectedResourcesBase < TEntity > where TEntity : class , IIdentifiable
2118 {
22- HashSet < TEntity > RequestEntities { get ; }
2319 HashSet < TEntity > DatabaseEntities { get ; }
2420 }
2521
26- public class EntityDiff < TEntity > : AffectedRelationships < TEntity > , IEntityDiff < TEntity > where TEntity : class , IIdentifiable
22+ public class EntityDiff < TEntity > : AffectedResourcesBase < TEntity > , IEntityDiff < TEntity > where TEntity : class , IIdentifiable
2723 {
2824 private readonly HashSet < TEntity > _databaseEntities ;
25+ private readonly bool _databaseValuesLoaded ;
2926 public HashSet < TEntity > DatabaseEntities { get => _databaseEntities ?? ThrowNoDbValuesError ( ) ; }
3027
31- public HashSet < TEntity > RequestEntities { get ; private set ; }
32- public EntityDiff ( IEnumerable requestEntities ,
28+ internal EntityDiff ( IEnumerable requestEntities ,
3329 IEnumerable databaseEntities ,
34- Dictionary < RelationshipProxy , IEnumerable > relationships ) : base ( relationships )
30+ Dictionary < RelationshipProxy , IEnumerable > relationships ) : base ( requestEntities , relationships )
3531 {
36- RequestEntities = ( HashSet < TEntity > ) requestEntities ;
3732 _databaseEntities = ( HashSet < TEntity > ) databaseEntities ;
33+ _databaseValuesLoaded |= _databaseEntities != null ;
3834 }
3935
4036 private HashSet < TEntity > ThrowNoDbValuesError ( )
4137 {
4238 throw new MemberAccessException ( "Cannot access database entities if the LoadDatabaseValues option is set to false" ) ;
4339 }
40+
41+ public IEnumerator < EntityDiffPair < TEntity > > GetEnumerator ( )
42+ {
43+ foreach ( var entity in Entities )
44+ {
45+ TEntity currentValueInDatabase = null ;
46+ if ( _databaseValuesLoaded ) currentValueInDatabase = _databaseEntities . Single ( e => entity . StringId == e . StringId ) ;
47+ yield return new EntityDiffPair < TEntity > ( entity , currentValueInDatabase ) ;
48+ }
49+ }
50+
51+ IEnumerator IEnumerable . GetEnumerator ( )
52+ {
53+ return GetEnumerator ( ) ;
54+ }
55+ }
56+
57+ public class EntityDiffPair < TEntity > where TEntity : class , IIdentifiable
58+ {
59+ internal EntityDiffPair ( TEntity entity , TEntity databaseValue )
60+ {
61+ Entity = entity ;
62+ DatabaseValue = databaseValue ;
63+ }
64+
65+ public TEntity Entity { get ; private set ; }
66+ public TEntity DatabaseValue { get ; private set ; }
4467 }
4568}
0 commit comments