Skip to content

Commit

Permalink
Automatically set update timestamp.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTedder committed Sep 6, 2024
1 parent dd4ac5d commit 47511bc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions LeaderboardBackend/Models/Entities/ApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ static ApplicationContext()
NpgsqlConnection.GlobalTypeMapper.MapEnum<RunType>();
}

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)
{
entity.CreatedAt = _clock.GetCurrentInstant();
}
}

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<ApplicationContext> options, IClock clock)
: base(options)
{
_clock = clock;
ChangeTracker.Tracked += AddCreationTimeStamp;
ChangeTracker.Tracked += AddCreationTimestamp;
ChangeTracker.Tracked += SetUpdateTimestamp;
}

public DbSet<AccountRecovery> AccountRecoveries { get; set; } = null!;
Expand Down

0 comments on commit 47511bc

Please sign in to comment.