Files
monorepo/LCU.API.Internal.MemberPreferencesApi/LCU.API.Internal.MemberPreferencesApiLib/Persistence/ApplicationDbContext.cs
T
2026-04-07 13:11:39 -05:00

112 lines
4.6 KiB
C#

using LCU.Library.Internal.MemberPreferencesApiModels.Entities;
using LCU.Library.Internal.MemberPreferencesApiModels.Enums;
using LCU.Library.Internal.MemberPreferencesApiModels.Models;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
namespace LCU.API.Internal.MemberPreferencesApiLib.Persistence;
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
private readonly IDictionary<Auditable, string> _systemModifiedAuditables = new Dictionary<Auditable, string>();
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<MemberPreference> MemberPreferences { get; set; }
public DbSet<MemberOptInSettings> MemberOptInSettings { get; set; }
public DbSet<DnaAuditProcess> DnaAuditProcesses { get; set; }
public DbSet<XdiBatchItem> XdiBatchItems { get; set; }
public DbSet<XdiBatchProcessInfo> XdiBatchProcessInfos { get; set; }
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
foreach (var entry in ChangeTracker.Entries<Auditable>())
{
var auditableUser = _systemModifiedAuditables.TryGetValue(entry.Entity, out var systemUser);
var applicationUser = new ApplicationUser
{
UserName = systemUser,
UserType = UserType.System
};
var otherUser = new ApplicationUser
{
UserName = "Unknown",
UserType = UserType.System,
};
var currentUser = auditableUser ? applicationUser : otherUser;
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedByType = currentUser.UserType;
entry.Entity.CreatedBy = currentUser.UserName;
entry.Entity.Created = DateTime.Now;
entry.Entity.LastUpdatedByType = currentUser.UserType;
entry.Entity.LastUpdatedBy = currentUser.UserName;
entry.Entity.LastUpdated = DateTime.Now;
break;
case EntityState.Modified:
entry.Entity.LastUpdatedByType = currentUser.UserType;
entry.Entity.LastUpdatedBy = currentUser.UserName;
entry.Entity.LastUpdated = DateTime.Now;
break;
}
}
return base.SaveChangesAsync(cancellationToken);
}
public Task<int> SaveChangesWithOperatorAsync(string operatorId, DateTime dateAdded, UserType userType, CancellationToken cancellationToken = new CancellationToken())
{
foreach (var entry in ChangeTracker.Entries<Auditable>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedByType = userType;
entry.Entity.CreatedBy = operatorId;
entry.Entity.Created = dateAdded;
entry.Entity.LastUpdatedByType = userType;
entry.Entity.LastUpdatedBy = operatorId;
entry.Entity.LastUpdated = dateAdded;
break;
case EntityState.Modified:
entry.Entity.LastUpdatedByType = userType;
entry.Entity.LastUpdatedBy = operatorId;
entry.Entity.LastUpdated = dateAdded;
break;
}
}
return base.SaveChangesAsync(cancellationToken);
}
public void SystemModified(Auditable entity, string modifiedBy) => _systemModifiedAuditables[entity] = modifiedBy;
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(builder);
builder.Entity<MemberPreference>().ToTable("MemberPreference");
builder.Entity<MemberPreference>().HasKey(p => p.Id);
builder.Entity<XdiBatchItem>().ToTable("XdiBatchItems");
builder.Entity<XdiBatchItem>().HasKey(p => new { p.UserId, p.BatchState });
builder.Entity<MemberOptInSettings>().ToTable("MemberOptInSettings");
builder.Entity<MemberOptInSettings>().HasKey(p => p.Id);
builder.Entity<MemberOptInSettings>().HasIndex(p => new { p.UserId })
.IsUnique(true);
builder.Entity<DnaAuditProcess>().ToTable("DnaAuditProcess");
builder.Entity<DnaAuditProcess>().HasKey(p => p.ProcessId);
builder.Entity<XdiBatchProcessInfo>().ToTable("XdiBatchProcessInfo");
builder.Entity<XdiBatchProcessInfo>().HasKey(p => p.XdiBatchProcessInfoId);
}
}