Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5f3e04b
refactor: AttachHasOnePointers
maurei Jun 7, 2019
e8d1096
refactor: AssignHasMany pointers
maurei Jun 7, 2019
1420d6d
refactor assign relationships and applied to updateasync method
maurei Jun 7, 2019
24dd97c
refactor: tests passing again
maurei Jun 7, 2019
0304725
test: add implicit removal test
maurei Jun 7, 2019
f60587f
test: all passing
maurei Jun 7, 2019
4cd9465
chore: addedd notes wrt implicit remove
maurei Jun 7, 2019
d1f39e6
comments: added comment to assign method
maurei Jun 7, 2019
bcbf8c3
Merge branch 'master' into fix/reattachment
maurei Jun 7, 2019
7df5233
fix: support for entity resource split
maurei Jun 7, 2019
0296eb4
fix: minor refactor, comments
maurei Jun 8, 2019
f0d5924
fix: foreignkey set null bug
maurei Jun 8, 2019
9f7550c
feat: decoupled repository from JsonApiContext with respect to updati…
maurei Jun 11, 2019
7aea60c
feat: decoupled IJsonApiContext from repository wrt updating resources
maurei Jun 11, 2019
652d65f
fix: resource separation issue|
maurei Jun 11, 2019
9838627
chore: cherry picked inverse relationships from hooks branch
maurei Jun 11, 2019
fbe69fc
fix: tests
maurei Jun 11, 2019
35a2f54
feat: implicit remove support
maurei Jun 11, 2019
6e6f7fa
fix: test
maurei Jun 11, 2019
c1d472d
fix: bugs with inverse relationship loading
maurei Jun 11, 2019
f45972f
tests: implicit remove through create tests
maurei Jun 11, 2019
d8b4217
feat: mark obsolete UpdateAsync(TId id, TEntity entity) method, add n…
maurei Jun 11, 2019
30765c3
fix: #520
maurei Jun 11, 2019
9139852
fix: separation tests
maurei Jun 11, 2019
457e93d
chore: comments
maurei Jun 12, 2019
65f8a3a
Update DefaultEntityRepository.cs
maurei Jun 12, 2019
3ddb6a2
Update DefaultEntityRepository.cs
maurei Jun 17, 2019
2437077
Update TypeExtensions.cs
maurei Jun 17, 2019
415306e
Update DefaultEntityRepository.cs
maurei Jun 17, 2019
52a452d
Update JsonApiReader.cs
maurei Jun 17, 2019
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: minor refactor, comments
  • Loading branch information
maurei committed Jun 8, 2019
commit 0296eb4d90afedc415d08c02f5a56d0039e88d6b
50 changes: 39 additions & 11 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DefaultEntityRepository(
public DefaultEntityRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext,
IDbContextResolver contextResolver,
IDbContextResolver contextResolver,
ResourceDefinition<TEntity> resourceDefinition = null)
: base(loggerFactory, jsonApiContext, contextResolver, resourceDefinition)
{ }
Expand Down Expand Up @@ -222,14 +222,20 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)

if (_jsonApiContext.RelationshipsToUpdate.Any())
{
/// First attach all targeted relationships to the dbcontext.
/// This takes into account that some of these entities are
/// already attached in the dbcontext
AttachRelationships(oldEntity);
AssignRelationshipValues(oldEntity, update: true);
/// load the current state of the relationship to support complete-replacement
LoadCurrentRelationships(oldEntity);
/// assign the actual relationship values.
AssignRelationshipValues(oldEntity);
}
await _context.SaveChangesAsync();
return oldEntity;
}

/// <inheritdoc />

public async Task UpdateRelationshipsAsync(object parent, RelationshipAttribute relationship, IEnumerable<string> relationshipIds)
{
// TODO: it would be better to let this be determined within the relationship attribute...
Expand Down Expand Up @@ -373,30 +379,52 @@ private void AttachHasManyAndHasManyThroughPointers(TEntity entity)
}

/// <summary>
/// todo: comments
/// Before assigning new relationship values (updateasync), we need to
/// attach load the current relationship state into the dbcontext, else
/// there won't be a complete-replace for one-to-many and many-to-many.
/// </summary>
/// <param name="oldEntity">Old entity.</param>
protected void LoadCurrentRelationships(TEntity oldEntity)
{
foreach (var relationshipEntry in _jsonApiContext.RelationshipsToUpdate)
{
var relationshipValue = relationshipEntry.Value;
if (relationshipEntry.Key is HasManyThroughAttribute throughAttribute)
{
_context.Entry(oldEntity).Collection(throughAttribute.InternalThroughName).Load();

}
else if (relationshipEntry.Key is HasManyAttribute hasManyAttribute)
{
_context.Entry(oldEntity).Collection(hasManyAttribute.InternalRelationshipName).Load();
}
}
}

/// <summary>
/// assigns relationships that were set in the request to the target entity of the request
/// todo: partially remove dependency on IJsonApiContext here: it is fine to
/// retrieve from the context WHICH relationships to update, but the actual values should
/// come from the context.
/// </summary>
protected void AssignRelationshipValues(TEntity oldEntity, bool update = false)
protected void AssignRelationshipValues(TEntity oldEntity)
{
foreach (var relationshipEntry in _jsonApiContext.RelationshipsToUpdate)
{
var relationshipValue = relationshipEntry.Value;
if (relationshipEntry.Key is HasManyThroughAttribute throughAttribute)
{
// load relations to enforce complete replace in case of patch
if (update) _context.Entry(oldEntity).Collection(throughAttribute.InternalThroughName).Load();
AssignHasManyThrough(oldEntity, throughAttribute, (IList)relationshipValue);
}
else if (relationshipEntry.Key is HasManyAttribute hasManyAttribute)
{
// load relations to enforce complete replace
if (update) _context.Entry(oldEntity).Collection(hasManyAttribute.InternalRelationshipName).Load();
// todo: need to load inverse relationship here, see issue #502
hasManyAttribute.SetValue(oldEntity, relationshipValue);
hasManyAttribute.SetValue(oldEntity, relationshipValue);
}
else if (relationshipEntry.Key is HasOneAttribute hasOneAttribute)
{
// todo: need to load inverse relationship here, see issue #502
if (update) hasOneAttribute.SetValue(oldEntity, relationshipValue);
hasOneAttribute.SetValue(oldEntity, relationshipValue);
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/JsonApiDotNetCore/Models/HasOneAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ public HasOneAttribute(string publicName = null, Link documentLinks = Link.All,
/// <param name="newValue">The new property value</param>
public override void SetValue(object resource, object newValue)
{
var propertyName = (newValue?.GetType() == Type)
? InternalRelationshipName
: IdentifiablePropertyName;

var propertyInfo = resource
.GetType()
.GetProperty(propertyName);
.GetProperty(InternalRelationshipName);

propertyInfo.SetValue(resource, newValue);
}
Expand Down