diff --git a/LeaderboardBackend/Models/Entities/ApplicationContext.cs b/LeaderboardBackend/Models/Entities/ApplicationContext.cs index 7c57fd01..bfccd3c1 100644 --- a/LeaderboardBackend/Models/Entities/ApplicationContext.cs +++ b/LeaderboardBackend/Models/Entities/ApplicationContext.cs @@ -20,7 +20,7 @@ static ApplicationContext() NpgsqlConnection.GlobalTypeMapper.MapEnum(); } - private void AddCreationTimeStamp(object? sender, EntityEntryEventArgs e) + private void AddCreationTimestamp(object? sender, EntityEntryEventArgs e) { if (e.Entry.State is EntityState.Added && e.Entry.Entity is IHasCreationTimestamp entity) { @@ -28,11 +28,20 @@ private void AddCreationTimeStamp(object? sender, EntityEntryEventArgs e) } } + private void SetUpdateTimestamp(object? sender, EntityEntryEventArgs e) + { + if (e.Entry.State is EntityState.Modified && e.Entry.Entity is IHasUpdateTimestamp entity) + { + entity.UpdatedAt = _clock.GetCurrentInstant(); + } + } + public ApplicationContext(DbContextOptions options, IClock clock) : base(options) { _clock = clock; - ChangeTracker.Tracked += AddCreationTimeStamp; + ChangeTracker.Tracked += AddCreationTimestamp; + ChangeTracker.Tracked += SetUpdateTimestamp; } public DbSet AccountRecoveries { get; set; } = null!;