Skip to content

Hotfixes related to Resource Hooks #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 25, 2019
6 changes: 3 additions & 3 deletions src/JsonApiDotNetCore/Hooks/Execution/EntityDiffs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace JsonApiDotNetCore.Hooks
/// Also contains information about updated relationships through
/// implementation of IRelationshipsDictionary<typeparamref name="TEntity"/>>
/// </summary>
public interface IEntityDiff<TEntity> : IRelationshipsDictionary<TEntity>, IEnumerable<EntityDiffPair<TEntity>> where TEntity : class, IIdentifiable
public interface IEntityDiff<TEntity> : IExposeRelationshipsDictionary<TEntity>, IEnumerable<EntityDiffPair<TEntity>> where TEntity : class, IIdentifiable
{
/// <summary>
/// The database values of the resources affected by the request.
Expand Down Expand Up @@ -42,7 +42,7 @@ public class EntityDiffs<TEntity> : IEntityDiff<TEntity> where TEntity : class,

public EntityDiffs(HashSet<TEntity> requestEntities,
HashSet<TEntity> databaseEntities,
Dictionary<RelationshipAttribute, HashSet<TEntity>> relationships)
Dictionary<RelationshipAttribute, HashSet<TEntity>> relationships)
{
Entities = requestEntities;
AffectedRelationships = new RelationshipsDictionary<TEntity>(relationships);
Expand All @@ -55,7 +55,7 @@ public EntityDiffs(HashSet<TEntity> requestEntities,
/// </summary>
internal EntityDiffs(IEnumerable requestEntities,
IEnumerable databaseEntities,
Dictionary<RelationshipAttribute, IEnumerable> relationships)
Dictionary<RelationshipAttribute, IEnumerable> relationships)
: this((HashSet<TEntity>)requestEntities, (HashSet<TEntity>)databaseEntities, TypeHelper.ConvertRelationshipDictionary<TEntity>(relationships)) { }


Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Hooks/Execution/EntityHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace JsonApiDotNetCore.Hooks
/// Also contains information about updated relationships through
/// implementation of IAffectedRelationshipsDictionary<typeparamref name="TResource"/>>
/// </summary>
public interface IEntityHashSet<TResource> : IRelationshipsDictionary<TResource>, IEnumerable<TResource> where TResource : class, IIdentifiable { }
public interface IEntityHashSet<TResource> : IExposeRelationshipsDictionary<TResource>, IEnumerable<TResource> where TResource : class, IIdentifiable { }

/// <summary>
/// Implementation of IResourceHashSet{TResource}.
Expand Down
18 changes: 14 additions & 4 deletions src/JsonApiDotNetCore/Hooks/Execution/RelationshipsDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public interface IRelationshipsDictionary { }
/// <summary>
/// An interface that is implemented to expose a relationship dictionary on another class.
/// </summary>
public interface IExposeRelationshipsDictionary<TDependentResource> : IRelationshipsDictionary<TDependentResource> where TDependentResource : class, IIdentifiable
public interface IExposeRelationshipsDictionary<TDependentResource> :
IRelationshipsDictionaryGetters<TDependentResource> where TDependentResource : class, IIdentifiable
{
/// <summary>
/// Gets a dictionary of affected resources grouped by affected relationships.
Expand All @@ -24,7 +25,15 @@ public interface IExposeRelationshipsDictionary<TDependentResource> : IRelations
/// <summary>
/// A helper class that provides insights in which relationships have been updated for which entities.
/// </summary>
public interface IRelationshipsDictionary<TDependentResource> : IRelationshipsDictionary where TDependentResource : class, IIdentifiable
public interface IRelationshipsDictionary<TDependentResource> :
IRelationshipsDictionaryGetters<TDependentResource>,
IReadOnlyDictionary<RelationshipAttribute, HashSet<TDependentResource>>,
IRelationshipsDictionary where TDependentResource : class, IIdentifiable { }

/// <summary>
/// A helper class that provides insights in which relationships have been updated for which entities.
/// </summary>
public interface IRelationshipsDictionaryGetters<TDependentResource> where TDependentResource : class, IIdentifiable
{
/// <summary>
/// Gets a dictionary of all entities that have an affected relationship to type <typeparamref name="TPrincipalResource"/>
Expand All @@ -42,7 +51,9 @@ public interface IRelationshipsDictionary<TDependentResource> : IRelationshipsDi
/// It is practically a ReadOnlyDictionary{RelationshipAttribute, HashSet{TDependentResource}} dictionary
/// with the two helper methods defined on IAffectedRelationships{TDependentResource}.
/// </summary>
public class RelationshipsDictionary<TDependentResource> : ReadOnlyDictionary<RelationshipAttribute, HashSet<TDependentResource>>, IRelationshipsDictionary<TDependentResource> where TDependentResource : class, IIdentifiable
public class RelationshipsDictionary<TDependentResource> :
ReadOnlyDictionary<RelationshipAttribute, HashSet<TDependentResource>>,
IRelationshipsDictionary<TDependentResource> where TDependentResource : class, IIdentifiable
{
/// <summary>
/// a dictionary with affected relationships as keys and values being the corresponding resources
Expand All @@ -62,7 +73,6 @@ public RelationshipsDictionary(Dictionary<RelationshipAttribute, HashSet<TDepend
internal RelationshipsDictionary(Dictionary<RelationshipAttribute, IEnumerable> relationships)
: this(TypeHelper.ConvertRelationshipDictionary<TDependentResource>(relationships)) { }


/// <inheritdoc />
public Dictionary<RelationshipAttribute, HashSet<TDependentResource>> GetByRelationship<TPrincipalResource>() where TPrincipalResource : class, IIdentifiable
{
Expand Down
1 change: 0 additions & 1 deletion src/JsonApiDotNetCore/Hooks/ResourceHookExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ void ValidateHookResponse<T>(IEnumerable<T> returnedList, ResourcePipeline pipel
PrincipalType principalType = dependentEntities.First().Key.PrincipalType;
var byInverseRelationship = dependentEntities.Where(kvp => kvp.Key.InverseNavigation != null).ToDictionary(kvp => GetInverseRelationship(kvp.Key), kvp => kvp.Value);
return (byInverseRelationship, principalType);

}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/JsonApiDotNetCore/Models/ResourceDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ public virtual void AfterUpdate(HashSet<T> entities, ResourcePipeline pipeline)
/// <inheritdoc/>
public virtual void AfterDelete(HashSet<T> entities, ResourcePipeline pipeline, bool succeeded) { }
/// <inheritdoc/>
public virtual void AfterUpdateRelationship(IRelationshipsDictionary<T> resourcesByRelationship, ResourcePipeline pipeline) { }
public virtual void AfterUpdateRelationship(IRelationshipsDictionary<T> entitiesByRelationship, ResourcePipeline pipeline) { }
/// <inheritdoc/>
public virtual IEnumerable<T> BeforeCreate(IEntityHashSet<T> affected, ResourcePipeline pipeline) { return affected; }
public virtual IEnumerable<T> BeforeCreate(IEntityHashSet<T> entities, ResourcePipeline pipeline) { return entities; }
/// <inheritdoc/>
public virtual void BeforeRead(ResourcePipeline pipeline, bool isIncluded = false, string stringId = null) { }
/// <inheritdoc/>
public virtual IEnumerable<T> BeforeUpdate(IEntityDiff<T> ResourceDiff, ResourcePipeline pipeline) { return ResourceDiff.Entities; }
public virtual IEnumerable<T> BeforeUpdate(IEntityDiff<T> entityDiff, ResourcePipeline pipeline) { return entityDiff.Entities; }
/// <inheritdoc/>
public virtual IEnumerable<T> BeforeDelete(IEntityHashSet<T> affected, ResourcePipeline pipeline) { return affected; }
public virtual IEnumerable<T> BeforeDelete(IEntityHashSet<T> entities, ResourcePipeline pipeline) { return entities; }
/// <inheritdoc/>
public virtual IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<T> resourcesByRelationship, ResourcePipeline pipeline) { return ids; }
public virtual IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<T> entitiesByRelationship, ResourcePipeline pipeline) { return ids; }
/// <inheritdoc/>
public virtual void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<T> resourcesByRelationship, ResourcePipeline pipeline) { }
public virtual void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<T> entitiesByRelationship, ResourcePipeline pipeline) { }
/// <inheritdoc/>
public virtual IEnumerable<T> OnReturn(HashSet<T> entities, ResourcePipeline pipeline) { return entities; }

Expand Down