From 1352a76cc01984193cd2b730260ea051c7a4d497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Strau=C3=9F?= Date: Sat, 24 Feb 2024 13:34:25 +0100 Subject: [PATCH] Added time provider extensions and added simple update method to Repository. --- .../Core/Extensions/ServiceCollectionExtensions.cs | 3 +-- .../Core/Extensions/TimeProviderExtensions.cs | 9 +++++++++ src/DotNetElements.Core/Core/Repository.cs | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/DotNetElements.Core/Core/Extensions/TimeProviderExtensions.cs diff --git a/src/DotNetElements.Core/Core/Extensions/ServiceCollectionExtensions.cs b/src/DotNetElements.Core/Core/Extensions/ServiceCollectionExtensions.cs index 8f9edf7..2386a8c 100644 --- a/src/DotNetElements.Core/Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/DotNetElements.Core/Core/Extensions/ServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; namespace DotNetElements.Core.Extensions; diff --git a/src/DotNetElements.Core/Core/Extensions/TimeProviderExtensions.cs b/src/DotNetElements.Core/Core/Extensions/TimeProviderExtensions.cs new file mode 100644 index 0000000..3749ed8 --- /dev/null +++ b/src/DotNetElements.Core/Core/Extensions/TimeProviderExtensions.cs @@ -0,0 +1,9 @@ +namespace DotNetElements.Core.Extensions; + +public static class TimeProviderExtensions +{ + public static DateTime GetUtcNowDateTime(this TimeProvider timeProvider) + { + return timeProvider.GetUtcNow().UtcDateTime; + } +} diff --git a/src/DotNetElements.Core/Core/Repository.cs b/src/DotNetElements.Core/Core/Repository.cs index c6c15f3..d81056a 100644 --- a/src/DotNetElements.Core/Core/Repository.cs +++ b/src/DotNetElements.Core/Core/Repository.cs @@ -182,6 +182,19 @@ public TRelatedEntity AttachById(TRelatedEnti return DbContext.Set().Attach(TRelatedEntity.CreateRefById(id)).Entity; } + protected async Task UpdateAsync(TEntity entity) + { + if (entity is IAuditedEntity auditedEntity) + auditedEntity.SetModificationAudited(CurrentUserProvider.GetCurrentUserId(), TimeProvider.GetUtcNow()); + + UpdateEntityVersion(entity, entity); + + if (!await SaveChangesWithVersionCheckAsync()) + return CrudResult.ConcurrencyConflict(); + + return CrudResult.Ok(); + } + protected void UpdateEntityVersion(TTargetEntity entityFromDb, Guid? originalVersion) where TTargetEntity : notnull {