-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize and centralize auditable entity interceptor
- Loading branch information
1 parent
fe52ca3
commit 5d32cab
Showing
8 changed files
with
70 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
...dules.Identity/Persistence/Interceptors/IdentityModuleUpdateAuditableEntityInterceptor.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
...estion.Infrastructure/Data/Interceptors/QuestionModuleUpdateAuditableEntityInterceptor.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Shared/SharedKernel.Infrastructure/Interceptors/PopulateAuditableEntityInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using SharedKernel.Core; | ||
|
||
namespace SharedKernel.Infrastructure.Interceptors; | ||
public sealed class PopulateAuditableEntityInterceptor(ITimeProvider timeProvider, IUser user) : SaveChangesInterceptor | ||
{ | ||
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (eventData.Context is not null) | ||
{ | ||
UpdateIdentityModuleAuditableEntity(eventData.Context); | ||
} | ||
return base.SavingChangesAsync(eventData, result, cancellationToken); | ||
} | ||
|
||
private void UpdateIdentityModuleAuditableEntity(DbContext context) | ||
{ | ||
var entries = context.ChangeTracker.Entries() | ||
.Where(x => x.State != EntityState.Detached && x.State != EntityState.Unchanged); | ||
|
||
foreach (var entry in entries) | ||
{ | ||
switch (entry.State) | ||
{ | ||
case EntityState.Added: | ||
{ | ||
if (entry.Entity is not ICreatedAuditableEntity addedEntity) continue; | ||
addedEntity.CreatedBy = user.Id != null ? | ||
Guid.Parse(user.Id) : | ||
null; | ||
addedEntity.CreatedDate = timeProvider.TimeNow; | ||
break; | ||
} | ||
case EntityState.Modified: | ||
{ | ||
if (entry.Entity is not IUpdatedAuditableEntity updatedEntity) continue; | ||
updatedEntity.UpdatedBy = user.Id != null ? | ||
Guid.Parse(user.Id) : | ||
null; | ||
updatedEntity.UpdatedDate = timeProvider.TimeNow; | ||
break; | ||
} | ||
case EntityState.Deleted: | ||
default: | ||
{ | ||
if (entry.Entity is not IDeletedAuditableEntity updatedEntity) continue; | ||
updatedEntity.DeletedBy = user.Id != null ? | ||
Guid.Parse(user.Id) : | ||
null; | ||
updatedEntity.DeletedDate = timeProvider.TimeNow; | ||
updatedEntity.IsDeleted = true; | ||
entry.State = EntityState.Modified; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |