Skip to content

Commit

Permalink
Added time provider extensions and added simple update method to Repo…
Browse files Browse the repository at this point in the history
…sitory.
  • Loading branch information
Felix-CodingClimber committed Feb 24, 2024
1 parent 132ef48 commit 1352a76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetElements.Core.Extensions;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DotNetElements.Core.Extensions;

public static class TimeProviderExtensions
{
public static DateTime GetUtcNowDateTime(this TimeProvider timeProvider)
{
return timeProvider.GetUtcNow().UtcDateTime;
}
}
13 changes: 13 additions & 0 deletions src/DotNetElements.Core/Core/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ public TRelatedEntity AttachById<TRelatedEntity, TRelatedEntityKey>(TRelatedEnti
return DbContext.Set<TRelatedEntity>().Attach(TRelatedEntity.CreateRefById(id)).Entity;
}

protected async Task<CrudResult> UpdateAsync(TEntity entity)
{
if (entity is IAuditedEntity<TKey> auditedEntity)
auditedEntity.SetModificationAudited(CurrentUserProvider.GetCurrentUserId(), TimeProvider.GetUtcNow());

UpdateEntityVersion(entity, entity);

if (!await SaveChangesWithVersionCheckAsync())
return CrudResult.ConcurrencyConflict();

return CrudResult.Ok();
}

protected void UpdateEntityVersion<TTargetEntity>(TTargetEntity entityFromDb, Guid? originalVersion)
where TTargetEntity : notnull
{
Expand Down

0 comments on commit 1352a76

Please # to comment.