.NET 10 solution for Ping, a Discord style chat app: Domain, Application and Infrastructure projects plus the Api host. SignalR for chat and presence, LiveKit for call tokens, EF Core on Postgres, S3 compatible storage for attachments, and OIDC auth shared with the Musify Zitadel instance. Includes the Compose stack for local development.
46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Ping.Domain.Abstractions;
|
|
|
|
namespace Ping.Infrastructure.Persistence
|
|
{
|
|
public sealed class AuditableEntityInterceptor : SaveChangesInterceptor
|
|
{
|
|
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
|
|
{
|
|
ApplyTimestamps(eventData.Context);
|
|
return base.SavingChanges(eventData, result);
|
|
}
|
|
|
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
|
|
DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
|
|
{
|
|
ApplyTimestamps(eventData.Context);
|
|
return base.SavingChangesAsync(eventData, result, cancellationToken);
|
|
}
|
|
|
|
private static void ApplyTimestamps(DbContext? context)
|
|
{
|
|
if (context is null)
|
|
return;
|
|
|
|
context.ChangeTracker.DetectChanges();
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
foreach (var entry in context.ChangeTracker.Entries<IAuditable>())
|
|
{
|
|
if (entry.State == EntityState.Added)
|
|
{
|
|
entry.Entity.CreatedAt = now;
|
|
entry.Entity.UpdatedAt = now;
|
|
}
|
|
else if (entry.State == EntityState.Modified)
|
|
{
|
|
entry.Entity.UpdatedAt = now;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|