| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | | 5 | | using ProjectTemplate.Infrastructure.Data.Entities; |
| | | 6 | | |
| | | 7 | | namespace ProjectTemplate.Infrastructure.Data; |
| | | 8 | | |
| | 24 | 9 | | internal sealed class AuditEntry(EntityEntry entry) |
| | | 10 | | { |
| | 24 | 11 | | internal EntityEntry Entry { get; } = entry; |
| | | 12 | | [DataType(DataType.Text)] |
| | 72 | 13 | | internal string TableName { get; set; } = string.Empty; |
| | | 14 | | [DataType(DataType.Text)] |
| | 72 | 15 | | internal string State { get; set; } = string.Empty; |
| | | 16 | | [DataType(DataType.Text)] |
| | 48 | 17 | | internal string Application { get; set; } = string.Empty; |
| | | 18 | | [DataType(DataType.Text)] |
| | 72 | 19 | | internal string ModifiedBy { get; set; } = string.Empty; |
| | | 20 | | [DataType(DataType.DateTime)] |
| | 48 | 21 | | internal DateTime ModifiedOnUtc { get; set; } |
| | 72 | 22 | | internal Dictionary<string, object> KeyValues { get; } = []; |
| | 80 | 23 | | internal Dictionary<string, object> OriginalValues { get; } = []; |
| | 256 | 24 | | internal Dictionary<string, object> CurrentValues { get; } = []; |
| | 72 | 25 | | internal List<PropertyEntry> TemporaryProperties { get; } = []; |
| | | 26 | | |
| | 48 | 27 | | internal bool HasTemporaryProperties => TemporaryProperties.Count != 0; |
| | | 28 | | |
| | | 29 | | internal AuditRecord ToAuditRecord() |
| | | 30 | | { |
| | 24 | 31 | | string applicationAssembly = |
| | 24 | 32 | | Assembly.GetEntryAssembly()?.GetName().Name |
| | 24 | 33 | | ?? GetType().Assembly.GetName().Name |
| | 24 | 34 | | ?? "Unknown Assembly"; |
| | | 35 | | |
| | 24 | 36 | | AuditRecord auditRecord = new() |
| | 24 | 37 | | { |
| | 24 | 38 | | Entity = TableName, |
| | 24 | 39 | | State = State, |
| | 24 | 40 | | Application = string.IsNullOrWhiteSpace(Application) |
| | 24 | 41 | | ? applicationAssembly |
| | 24 | 42 | | : Application, |
| | 24 | 43 | | ModifiedBy = ModifiedBy, |
| | 24 | 44 | | ModifiedOnUtc = ModifiedOnUtc, |
| | 24 | 45 | | KeyValues = SerializeAuditValues(KeyValues), |
| | 24 | 46 | | OriginalValues = SerializeAuditValues(OriginalValues), |
| | 24 | 47 | | CurrentValues = SerializeAuditValues(CurrentValues) |
| | 24 | 48 | | }; |
| | 24 | 49 | | return auditRecord; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | private static string SerializeAuditValues(Dictionary<string, object> values) |
| | | 53 | | { |
| | 72 | 54 | | return values.Count == 0 |
| | 72 | 55 | | ? string.Empty |
| | 72 | 56 | | : JsonSerializer.Serialize(values); |
| | | 57 | | } |
| | | 58 | | } |