[CHANGE] History -> Audit
This commit is contained in:
@@ -67,7 +67,7 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
|
||||
LogEvent("Failed to save client: " + e.Message, LogSeverity.Warning);
|
||||
return ResultError.Error(e);
|
||||
}
|
||||
|
||||
|
||||
var saveResult = await _libraryService.SaveChannelAsync(channel, cancellationToken);
|
||||
return saveResult;
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
{
|
||||
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
|
||||
{
|
||||
AddHistory(eventData.Context);
|
||||
AddAudit(eventData.Context);
|
||||
return base.SavingChanges(eventData, result);
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
InterceptionResult<int> result,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
AddHistory(eventData.Context);
|
||||
AddAudit(eventData.Context);
|
||||
return base.SavingChangesAsync(eventData, result, cancellationToken);
|
||||
}
|
||||
|
||||
private void AddHistory(DbContext? context)
|
||||
private void AddAudit(DbContext? context)
|
||||
{
|
||||
if (context == null) return;
|
||||
|
||||
@@ -32,7 +32,7 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
.Where(e => e.State is EntityState.Modified or EntityState.Deleted or EntityState.Added && Attribute.IsDefined(e.Entity.GetType(),
|
||||
typeof(AuditableAttribute)));
|
||||
|
||||
var histories = new List<EntityHistory>();
|
||||
var audits = new List<EntityAudit>();
|
||||
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
@@ -49,34 +49,34 @@ public class AuditInterceptor : SaveChangesInterceptor
|
||||
switch (entry.State)
|
||||
{
|
||||
case EntityState.Added:
|
||||
histories.AddRange(allowedProperties
|
||||
audits.AddRange(allowedProperties
|
||||
.Where(p => p.CurrentValue != null)
|
||||
.Select(p => CreateHistory(entry, p, entry.State, primaryKey))
|
||||
.Select(p => CreateAudit(entry, p, entry.State, primaryKey))
|
||||
);
|
||||
break;
|
||||
case EntityState.Modified:
|
||||
histories.AddRange(allowedProperties
|
||||
audits.AddRange(allowedProperties
|
||||
.Where(p => p.IsModified)
|
||||
.Select(p => CreateHistory(entry, p, entry.State, primaryKey))
|
||||
.Select(p => CreateAudit(entry, p, entry.State, primaryKey))
|
||||
);
|
||||
break;
|
||||
case EntityState.Deleted:
|
||||
histories.AddRange(allowedProperties
|
||||
.Select(p => CreateHistory(entry, p, entry.State, primaryKey))
|
||||
audits.AddRange(allowedProperties
|
||||
.Select(p => CreateAudit(entry, p, entry.State, primaryKey))
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (histories.Count != 0)
|
||||
if (audits.Count != 0)
|
||||
{
|
||||
context.Set<EntityHistory>().AddRange(histories);
|
||||
context.Set<EntityAudit>().AddRange(audits);
|
||||
}
|
||||
}
|
||||
|
||||
private EntityHistory CreateHistory(EntityEntry entry, PropertyEntry prop, EntityState changeType, string? primaryKey)
|
||||
private EntityAudit CreateAudit(EntityEntry entry, PropertyEntry prop, EntityState changeType, string? primaryKey)
|
||||
{
|
||||
return new EntityHistory
|
||||
return new EntityAudit
|
||||
{
|
||||
EntityName = entry.Entity.GetType().Name,
|
||||
EntityId = primaryKey ?? "Unknown",
|
||||
|
@@ -14,7 +14,7 @@ public sealed class LibraryDbContext : DbContext
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
||||
public DbSet<EntityHistory> Histories { get; set; }
|
||||
public DbSet<EntityAudit> Histories { get; set; }
|
||||
|
||||
public DbSet<CaptionEntity> Captions { get; set; }
|
||||
public DbSet<ChannelEntity> Channels { get; set; }
|
||||
@@ -32,9 +32,9 @@ public sealed class LibraryDbContext : DbContext
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<EntityHistory>(eh =>
|
||||
modelBuilder.Entity<EntityAudit>(eh =>
|
||||
{
|
||||
eh.ToTable("entity_history");
|
||||
eh.ToTable("audits");
|
||||
});
|
||||
|
||||
modelBuilder.Entity<CaptionEntity>(ce =>
|
||||
|
@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Manager.Data.Entities.Audit;
|
||||
|
||||
public class EntityHistory
|
||||
public class EntityAudit
|
||||
{
|
||||
[MaxLength(200)]
|
||||
public required string EntityName { get; set; }
|
Reference in New Issue
Block a user