Skip to content

Commit d8b4217

Browse files
committed
feat: mark obsolete UpdateAsync(TId id, TEntity entity) method, add new one without id parameter
1 parent f45972f commit d8b4217

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,16 @@ public void DetachRelationshipPointers(TEntity entity)
238238
}
239239
}
240240

241-
/// <inheritdoc />
241+
[Obsolete("Use overload UpdateAsync(TEntity updatedEntity): providing parameter ID does no longer add anything relevant")]
242242
public virtual async Task<TEntity> UpdateAsync(TId id, TEntity updatedEntity)
243243
{
244-
/// WHY is parameter "entity" even passed along to this method??
245-
/// It does nothing!
246-
247-
var oldEntity = await GetAsync(id);
244+
return await UpdateAsync(updatedEntity);
245+
}
248246

247+
/// <inheritdoc />
248+
public virtual async Task<TEntity> UpdateAsync(TEntity updatedEntity)
249+
{
250+
var oldEntity = await GetAsync(updatedEntity.Id);
249251
if (oldEntity == null)
250252
return null;
251253

@@ -259,6 +261,7 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity updatedEntity)
259261
LoadInverseRelationships(trackedRelationshipValue, relationshipAttr);
260262
AssignRelationshipValue(oldEntity, trackedRelationshipValue, relationshipAttr);
261263
}
264+
262265
await _context.SaveChangesAsync();
263266
return oldEntity;
264267
}

src/JsonApiDotNetCore/Data/IEntityWriteRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using JsonApiDotNetCore.Models;
@@ -14,6 +15,9 @@ public interface IEntityWriteRepository<TEntity, in TId>
1415
{
1516
Task<TEntity> CreateAsync(TEntity entity);
1617

18+
Task<TEntity> UpdateAsync(TEntity entity);
19+
20+
[Obsolete("Use overload UpdateAsync(TEntity updatedEntity): providing parameter ID does no longer add anything relevant")]
1721
Task<TEntity> UpdateAsync(TId id, TEntity entity);
1822

1923
Task UpdateRelationshipsAsync(object parent, RelationshipAttribute relationship, IEnumerable<string> relationshipIds);

src/JsonApiDotNetCore/Services/EntityResourceService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource resource)
154154
{
155155
var entity = MapIn(resource);
156156

157-
158-
159-
entity = await _entities.UpdateAsync(id, entity);
157+
entity = await _entities.UpdateAsync(entity);
160158

161159
return MapOut(entity);
162160
}

test/UnitTests/Data/DefaultEntityRepository_Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ public async Task UpdateAsync_Updates_Attributes_In_AttributesToUpdate()
6060
{
6161
{
6262
descAttr,
63-
todoItemUpdates.Description
63+
null //todoItemUpdates.Description
6464
}
6565
};
6666

6767
var repository = GetRepository();
6868

6969
// act
70-
var updatedItem = await repository.UpdateAsync(_todoItem.Id, todoItemUpdates);
70+
var updatedItem = await repository.UpdateAsync(todoItemUpdates);
7171

7272
// assert
7373
Assert.NotNull(updatedItem);

0 commit comments

Comments
 (0)