Skip to content

Commit 30565eb

Browse files
committed
fix: BeforeImplicitUpdate hook through the Delete pipeline, added a bunch of comments
1 parent c179310 commit 30565eb

File tree

8 files changed

+27
-30
lines changed

8 files changed

+27
-30
lines changed

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5858
.WithOne(t => t.ParentTodoItem)
5959
.HasForeignKey(t => t.ParentTodoItemId);
6060

61-
modelBuilder.Entity<Person>()
62-
.HasOne(p => p.Passport)
63-
.WithOne(p => p.Person)
64-
.HasForeignKey<Person>(p => p.PassportId);
65-
6661
modelBuilder.Entity<Passport>()
6762
.HasOne(p => p.Person)
6863
.WithOne(p => p.Passport)
69-
.HasForeignKey<Person>(p => p.PassportId);
64+
.HasForeignKey<Person>(p => p.PassportId)
65+
.OnDelete(DeleteBehavior.SetNull);
7066

7167
modelBuilder.Entity<TodoItem>()
7268
.HasOne(p => p.ToOnePerson)

src/Examples/JsonApiDotNetCoreExample/Resources/LockableResourceBase.cs renamed to src/Examples/JsonApiDotNetCoreExample/Resources/LockableResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace JsonApiDotNetCoreExample.Resources
99
{
10-
public abstract class LockableResourceBase<T> : ResourceDefinition<T> where T : class, IIsLockable, IIdentifiable
10+
public abstract class LockableResource<T> : ResourceDefinition<T> where T : class, IIsLockable, IIdentifiable
1111
{
12-
protected LockableResourceBase(IResourceGraph graph) : base(graph) { }
12+
protected LockableResource(IResourceGraph graph) : base(graph) { }
1313

1414
protected void DisallowLocked(IEnumerable<T> entities)
1515
{

src/Examples/JsonApiDotNetCoreExample/Resources/PersonResource.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,19 @@
66

77
namespace JsonApiDotNetCoreExample.Resources
88
{
9-
public class PersonResource : LockableResourceBase<Person>
9+
public class PersonResource : LockableResource<Person>
1010
{
1111
public PersonResource(IResourceGraph graph) : base(graph) { }
1212

13-
public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<Person> resourcesByRelationship, ResourcePipeline pipeline)
13+
public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<Person> entitiesByRelationship, ResourcePipeline pipeline)
1414
{
15-
BeforeImplicitUpdateRelationship(resourcesByRelationship, pipeline);
15+
BeforeImplicitUpdateRelationship(entitiesByRelationship, pipeline);
1616
return ids;
1717
}
1818

19-
//[LoadDatabaseValues(true)]
20-
//public override IEnumerable<Person> BeforeUpdate(IResourceDiff<Person> entityDiff, ResourcePipeline pipeline)
21-
//{
22-
// return entityDiff.Entities;
23-
//}
24-
25-
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<Person> resourcesByRelationship, ResourcePipeline pipeline)
19+
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<Person> entitiesByRelationship, ResourcePipeline pipeline)
2620
{
27-
resourcesByRelationship.GetByRelationship<Passport>().ToList().ForEach(kvp => DisallowLocked(kvp.Value));
21+
entitiesByRelationship.GetByRelationship<Passport>().ToList().ForEach(kvp => DisallowLocked(kvp.Value));
2822
}
2923
}
3024
}

src/Examples/JsonApiDotNetCoreExample/Resources/TodoResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace JsonApiDotNetCoreExample.Resources
99
{
10-
public class TodoResource : LockableResourceBase<TodoItem>
10+
public class TodoResource : LockableResource<TodoItem>
1111
{
1212
public TodoResource(IResourceGraph graph) : base(graph) { }
1313

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public virtual async Task<bool> DeleteAsync(TId id)
376376
var entity = await GetAsync(id);
377377
if (entity == null) return false;
378378
_dbSet.Remove(entity);
379-
await _context.SaveChangesAsync();
379+
await _context.SaveChangesAsync();
380380
return true;
381381
}
382382

src/JsonApiDotNetCore/Hooks/ResourceHookExecutor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ public virtual IEnumerable<TEntity> BeforeDelete<TEntity>(IEnumerable<TEntity> e
8888
node.Reassign(entities);
8989
}
9090

91-
foreach (var entry in node.PrincipalsToNextLayerByType())
91+
/// If we're deleting an article, we're implicitly affected any owners related to it.
92+
/// Here we're loading all relations onto the to-be-deleted article
93+
/// if for that relation the BeforeImplicitUpdateHook is implemented,
94+
/// and this hook is then executed
95+
foreach (var entry in node.PrincipalsToNextLayerByRelationships())
9296
{
9397
var dependentType = entry.Key;
9498
var implicitTargets = entry.Value;

src/JsonApiDotNetCore/Hooks/Traversal/RootNode.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ namespace JsonApiDotNetCore.Hooks
1313
/// </summary>
1414
internal class RootNode<TEntity> : IEntityNode where TEntity : class, IIdentifiable
1515
{
16+
private readonly RelationshipProxy[] _allRelationshipsToNextLayer;
1617
private HashSet<TEntity> _uniqueEntities;
1718
public Type EntityType { get; internal set; }
1819
public IEnumerable UniqueEntities { get { return _uniqueEntities; } }
19-
public RelationshipProxy[] RelationshipsToNextLayer { get; private set; }
20-
public Dictionary<Type, Dictionary<RelationshipAttribute, IEnumerable>> PrincipalsToNextLayerByType()
20+
public RelationshipProxy[] RelationshipsToNextLayer { get; }
21+
22+
public Dictionary<Type, Dictionary<RelationshipAttribute, IEnumerable>> PrincipalsToNextLayerByRelationships()
2123
{
22-
return RelationshipsToNextLayer
24+
return _allRelationshipsToNextLayer
2325
.GroupBy(proxy => proxy.DependentType)
2426
.ToDictionary(gdc => gdc.Key, gdc => gdc.ToDictionary(p => p.Attribute, p => UniqueEntities));
2527
}
@@ -37,11 +39,12 @@ public Dictionary<RelationshipAttribute, IEnumerable> PrincipalsToNextLayer()
3739
/// </summary>
3840
public IRelationshipsFromPreviousLayer RelationshipsFromPreviousLayer { get { return null; } }
3941

40-
public RootNode(IEnumerable<TEntity> uniqueEntities, RelationshipProxy[] relationships)
42+
public RootNode(IEnumerable<TEntity> uniqueEntities, RelationshipProxy[] poplatedRelationships, RelationshipProxy[] allRelationships)
4143
{
4244
EntityType = typeof(TEntity);
4345
_uniqueEntities = new HashSet<TEntity>(uniqueEntities);
44-
RelationshipsToNextLayer = relationships;
46+
RelationshipsToNextLayer = poplatedRelationships;
47+
_allRelationshipsToNextLayer = allRelationships;
4548
}
4649

4750
/// <summary>

src/JsonApiDotNetCore/Hooks/Traversal/TraversalHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public RootNode<TEntity> CreateRootNode<TEntity>(IEnumerable<TEntity> rootEntiti
5757
_processedEntities = new Dictionary<DependentType, HashSet<IIdentifiable>>();
5858
RegisterRelationshipProxies(typeof(TEntity));
5959
var uniqueEntities = ProcessEntities(rootEntities);
60-
var relationshipsToNextLayer = GetPopulatedRelationships(typeof(TEntity), uniqueEntities.Cast<IIdentifiable>());
61-
return new RootNode<TEntity>(uniqueEntities, relationshipsToNextLayer);
60+
var populatedRelationshipsToNextLayer = GetPopulatedRelationships(typeof(TEntity), uniqueEntities.Cast<IIdentifiable>());
61+
var allRelationshipsFromType = RelationshipProxies.Select(entry => entry.Value).Where(proxy => proxy.PrincipalType == typeof(TEntity)).ToArray();
62+
return new RootNode<TEntity>(uniqueEntities, populatedRelationshipsToNextLayer, allRelationshipsFromType);
6263
}
6364

6465
/// <summary>
@@ -108,7 +109,6 @@ public EntityChildLayer CreateNextLayer(IEnumerable<IEntityNode> nodes)
108109
return new EntityChildLayer(nextNodes);
109110
}
110111

111-
112112
/// <summary>
113113
/// iterates throug the <paramref name="relationships"/> dictinary and groups the values
114114
/// by matching dependent type of the keys (which are relationshipattributes)

0 commit comments

Comments
 (0)