Initial code commit.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sufi.Demo.PeopleDirectory.Application.Contracts.Services;
|
||||
using Sufi.Demo.PeopleDirectory.Domain.Common;
|
||||
using Sufi.Demo.PeopleDirectory.Domain.Entities.Misc;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Models.Identity;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.Persistence.Contexts
|
||||
{
|
||||
public class ApplicationDbContext(
|
||||
DbContextOptions<ApplicationDbContext> options,
|
||||
ICurrentUserService currentUserService
|
||||
) : AuditableContext(options)
|
||||
{
|
||||
public virtual DbSet<Contact> Contacts { get; set; } = null!;
|
||||
public virtual DbSet<ServerInfo> ServerInfos { get; set; } = null!;
|
||||
|
||||
public override Task<int> SaveChangesAsync(string? userId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
PopulateAuditRecords();
|
||||
|
||||
return base.SaveChangesAsync(userId, cancellationToken);
|
||||
}
|
||||
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
PopulateAuditRecords();
|
||||
|
||||
if (currentUserService.UserId == null)
|
||||
{
|
||||
return await base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
return await base.SaveChangesAsync(currentUserService.UserId, cancellationToken);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
foreach (var property in builder.Model.GetEntityTypes()
|
||||
.SelectMany(t => t.GetProperties())
|
||||
.Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
|
||||
{
|
||||
property.SetColumnType("decimal(18,2)");
|
||||
}
|
||||
|
||||
foreach (var property in builder.Model.GetEntityTypes()
|
||||
.SelectMany(t => t.GetProperties())
|
||||
.Where(p => p.Name is "LastModifiedBy" or "CreatedBy"))
|
||||
{
|
||||
property.SetColumnType("character varying(100)");
|
||||
}
|
||||
|
||||
base.OnModelCreating(builder);
|
||||
|
||||
var assembly = typeof(ApplicationDbContext).Assembly;
|
||||
builder.ApplyConfigurationsFromAssembly(assembly);
|
||||
|
||||
builder.Entity<IdentityUserRole<string>>(entity => entity.ToTable("UserRoles", "Identity"));
|
||||
|
||||
builder.Entity<IdentityUserClaim<string>>(entity => entity.ToTable("UserClaims", "Identity"));
|
||||
|
||||
builder.Entity<IdentityUserLogin<string>>(entity => entity.ToTable("UserLogins", "Identity"));
|
||||
|
||||
builder.Entity<AppRoleClaim>(entity => entity.ToTable("RoleClaims", "Identity"));
|
||||
|
||||
builder.Entity<IdentityUserToken<string>>(entity => entity.ToTable("UserTokens", "Identity"));
|
||||
}
|
||||
|
||||
private void PopulateAuditRecords()
|
||||
{
|
||||
foreach (var entry in ChangeTracker.Entries<IAuditableEntity>().ToList())
|
||||
{
|
||||
switch (entry.State)
|
||||
{
|
||||
case EntityState.Added:
|
||||
entry.Entity.CreatedOn = DateTime.UtcNow;
|
||||
entry.Entity.CreatedBy = currentUserService.UserId;
|
||||
break;
|
||||
|
||||
case EntityState.Modified:
|
||||
entry.Entity.LastModifiedOn = DateTime.UtcNow;
|
||||
entry.Entity.LastModifiedBy = currentUserService.UserId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Sufi.Demo.PeopleDirectory.Application.Enums;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Models.Audit;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Models.Identity;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.Persistence.Contexts
|
||||
{
|
||||
public abstract class AuditableContext(
|
||||
DbContextOptions options
|
||||
) : IdentityDbContext<AppUser, AppRole, string, IdentityUserClaim<string>, IdentityUserRole<string>,
|
||||
IdentityUserLogin<string>, AppRoleClaim, IdentityUserToken<string>>(options)
|
||||
{
|
||||
public DbSet<Audit> AuditTrails { get; set; } = null!;
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
base.OnModelCreating(builder);
|
||||
|
||||
builder.Entity<Audit>().ToTable("AuditTrails", "Audit");
|
||||
}
|
||||
|
||||
public virtual async Task<int> SaveChangesAsync(string? userId = null, CancellationToken cancellationToken = new())
|
||||
{
|
||||
var auditEntries = OnBeforeSaveChanges(userId);
|
||||
var result = await base.SaveChangesAsync(cancellationToken);
|
||||
await OnAfterSaveChanges(auditEntries, cancellationToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<AuditEntry> OnBeforeSaveChanges(string? userId)
|
||||
{
|
||||
ChangeTracker.DetectChanges();
|
||||
var auditEntries = new List<AuditEntry>();
|
||||
foreach (var entry in ChangeTracker.Entries())
|
||||
{
|
||||
if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
|
||||
continue;
|
||||
|
||||
var auditEntry = new AuditEntry(entry)
|
||||
{
|
||||
TableName = entry.Entity.GetType().Name,
|
||||
UserId = userId
|
||||
};
|
||||
auditEntries.Add(auditEntry);
|
||||
foreach (var property in entry.Properties)
|
||||
{
|
||||
if (property.IsTemporary)
|
||||
{
|
||||
auditEntry.TemporaryProperties.Add(property);
|
||||
continue;
|
||||
}
|
||||
|
||||
string propertyName = property.Metadata.Name;
|
||||
if (property.Metadata.IsPrimaryKey())
|
||||
{
|
||||
auditEntry.KeyValues[propertyName] = property.CurrentValue;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (entry.State)
|
||||
{
|
||||
case EntityState.Added:
|
||||
auditEntry.AuditType = AuditType.Create;
|
||||
auditEntry.NewValues[propertyName] = property.CurrentValue;
|
||||
break;
|
||||
|
||||
case EntityState.Deleted:
|
||||
auditEntry.AuditType = AuditType.Delete;
|
||||
auditEntry.OldValues[propertyName] = property.OriginalValue;
|
||||
break;
|
||||
|
||||
case EntityState.Modified:
|
||||
if (property.IsModified && property.OriginalValue?.Equals(property.CurrentValue) == false)
|
||||
{
|
||||
auditEntry.ChangedColumns.Add(propertyName);
|
||||
auditEntry.AuditType = AuditType.Update;
|
||||
auditEntry.OldValues[propertyName] = property.OriginalValue;
|
||||
auditEntry.NewValues[propertyName] = property.CurrentValue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties))
|
||||
{
|
||||
AuditTrails.Add(auditEntry.ToAudit());
|
||||
}
|
||||
return [.. auditEntries.Where(_ => _.HasTemporaryProperties)];
|
||||
}
|
||||
|
||||
private Task OnAfterSaveChanges(List<AuditEntry> auditEntries, CancellationToken cancellationToken = new())
|
||||
{
|
||||
if (auditEntries == null || auditEntries.Count == 0)
|
||||
return Task.CompletedTask;
|
||||
|
||||
foreach (var auditEntry in auditEntries)
|
||||
{
|
||||
foreach (var prop in auditEntry.TemporaryProperties)
|
||||
{
|
||||
if (prop.Metadata.IsPrimaryKey())
|
||||
{
|
||||
auditEntry.KeyValues[prop.Metadata.Name] = prop.CurrentValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
auditEntry.NewValues[prop.Metadata.Name] = prop.CurrentValue;
|
||||
}
|
||||
}
|
||||
AuditTrails.Add(auditEntry.ToAudit());
|
||||
}
|
||||
return SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Models.Identity;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.Persistence.Contexts.EntityMaps
|
||||
{
|
||||
public class AppRoleMap : IEntityTypeConfiguration<AppRole>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<AppRole> builder)
|
||||
{
|
||||
builder.ToTable("Roles", "Identity");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Sufi.Demo.PeopleDirectory.Persistence.Models.Identity;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.Persistence.Contexts.EntityMaps
|
||||
{
|
||||
public class AppUserMap : IEntityTypeConfiguration<AppUser>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<AppUser> builder)
|
||||
{
|
||||
builder.ToTable("Users", "Identity");
|
||||
|
||||
builder.Property(e => e.Id).ValueGeneratedOnAdd();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user