feat: add timestamp interceptor and infrastructure service registration
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
using FiscalOS.Core.Data;
|
||||||
|
|
||||||
|
namespace FiscalOS.Infra.Data;
|
||||||
|
|
||||||
|
internal sealed class TimestampInterceptor : SaveChangesInterceptor
|
||||||
|
{
|
||||||
|
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
|
||||||
|
{
|
||||||
|
UpdateEntities(eventData.Context);
|
||||||
|
return base.SavingChanges(eventData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
UpdateEntities(eventData.Context);
|
||||||
|
return base.SavingChangesAsync(eventData, result, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void UpdateEntities(DbContext? context)
|
||||||
|
{
|
||||||
|
if (context is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var entries = context.ChangeTracker
|
||||||
|
.Entries<Entity>()
|
||||||
|
.Where(static e => e.State is EntityState.Added or EntityState.Modified);
|
||||||
|
|
||||||
|
var utcNow = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
foreach (var entry in entries)
|
||||||
|
{
|
||||||
|
if (entry.State == EntityState.Added)
|
||||||
|
{
|
||||||
|
entry.Entity.SetCreatedAt(utcNow);
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.Entity.SetUpdatedAt(utcNow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace FiscalOS.Infra.DependencyInjection;
|
||||||
|
|
||||||
|
public static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IPasswordHasher, PasswordHasher>(static (_) => PasswordHasher.New());
|
||||||
|
services.ConfigureOptions<AppDbContextOptionsSetup>();
|
||||||
|
services.AddDbContext<AppDbContext>();
|
||||||
|
services.AddHostedService<MigrationService>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user