Exemplo n.º 1
0
        private int OnAfterSaveChanges(List <AuditEntryBO> auditEntries)
        {
            if (auditEntries == null || auditEntries.Count == 0)
            {
                return(1);
            }


            foreach (var auditEntry in auditEntries)
            {
                // Get the final value of the temporary properties
                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;
                    }
                }

                // Save the Audit entry
                TblAuditHistory.Add(auditEntry.ToAudit());
            }

            return(SaveChanges());
        }
Exemplo n.º 2
0
        public TblAuditHistory ToAudit()
        {
            var audit = new TblAuditHistory();

            audit.TableName   = TableName;
            audit.CreatedAt   = DateTime.UtcNow;
            audit.KeyValues   = JsonSerializer.Serialize(KeyValues);
            audit.QueryAction = QueryAction;
            audit.OldValues   = OldValues.Count == 0 ? null : JsonSerializer.Serialize(OldValues);
            audit.NewValues   = NewValues.Count == 0 ? null : JsonSerializer.Serialize(NewValues);
            return(audit);
        }
Exemplo n.º 3
0
    public TblAuditHistory ToAudit()
    {
        var jsonSerializerOptions = new JsonSerializerOptions();

        jsonSerializerOptions.Converters.Add(new DateOnlyConverter());

        var audit = new TblAuditHistory();

        audit.TableName   = TableName;
        audit.CreatedAt   = DateTime.UtcNow;
        audit.KeyValues   = JsonSerializer.Serialize(KeyValues, jsonSerializerOptions);
        audit.QueryAction = QueryAction;
        audit.OldValues   = OldValues.Count == 0 ? null : JsonSerializer.Serialize(OldValues, jsonSerializerOptions);
        audit.NewValues   = NewValues.Count == 0 ? null : JsonSerializer.Serialize(NewValues, jsonSerializerOptions);
        return(audit);
    }
Exemplo n.º 4
0
        public List <AuditEntryBO> OnBeforeSaveChanges()
        {
            ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditEntryBO>();

            foreach (var entry in ChangeTracker.Entries())
            {
                if (entry.Entity is TblAuditHistory || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }

                var auditEntry = new AuditEntryBO(entry);
                auditEntry.TableName = entry.Metadata.GetTableName();
                auditEntries.Add(auditEntry);

                foreach (var property in entry.Properties)
                {
                    switch (property.Metadata.Name)
                    {
                    case "IsEnabled":
                        property.CurrentValue = true;
                        break;

                    case "CreatedOn":
                        if (entry.State == EntityState.Added)
                        {
                            property.CurrentValue = DateTime.Now;
                        }
                        break;

                    case "ModifiedOn":
                        property.CurrentValue = DateTime.Now;
                        break;

                    case "IsDeleted":
                        if (property.CurrentValue == null)
                        {
                            property.CurrentValue = false;
                        }
                        break;

                    case "CreatedBy":
                        if (property.CurrentValue == null)
                        {
                            property.CurrentValue = (long?)0m;
                        }
                        break;

                    default:
                        break;
                    }

                    if (property.IsTemporary)
                    {
                        // value will be generated by the database, get the value after saving
                        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.NewValues[propertyName] = property.CurrentValue;
                        auditEntry.QueryAction             = entry.State.ToString();
                        break;

                    case EntityState.Deleted:
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        auditEntry.QueryAction             = entry.State.ToString();
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.OldValues[propertyName] = property.OriginalValue;
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                            auditEntry.QueryAction             = entry.State.ToString();
                        }
                        break;
                    }
                }
            }

            // Save audit entities that have all the modifications
            foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties))
            {
                TblAuditHistory.Add(auditEntry.ToAudit());
            }

            // keep a list of entries where the value of some properties are unknown at this step
            return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList());
        }