-
Notifications
You must be signed in to change notification settings - Fork 8
/
ApplicationDbContext.cs
249 lines (201 loc) · 8.03 KB
/
ApplicationDbContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using Headway.Core.Model;
using Headway.RemediatR.Core.Model;
using Headway.Repository.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Headway.Repository.Data
{
public class ApplicationDbContext : DbContext
{
private string user;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Audit> Audits { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Permission> Permissions { get; set; }
public DbSet<Module> Modules { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<MenuItem> MenuItems { get; set; }
public DbSet<Config> Configs { get; set; }
public DbSet<ConfigItem> ConfigItems { get; set; }
public DbSet<ConfigContainer> ConfigContainers { get; set; }
public DbSet<ConfigSearchItem> ConfigSearchItems { get; set; }
public DbSet<DemoModel> DemoModels { get; set; }
public DbSet<DemoModelItem> DemoModelItems { get; set; }
public DbSet<DemoModelTreeItem> DemoModelTreeItems { get; set; }
public DbSet<DemoModelComplexProperty> DemoModelComplexProperties { get; set; }
// RemediatR
public DbSet<Country> Countries { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Program> Programs { get; set; }
public DbSet<Redress> Redresses { get; set; }
public void SetUser(string user)
{
this.user = user;
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<User>()
.HasIndex(u => u.UserName)
.IsUnique();
builder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
builder.Entity<Role>()
.HasIndex(r => r.Name)
.IsUnique();
builder.Entity<Permission>()
.HasIndex(p => p.Name)
.IsUnique();
builder.Entity<Module>()
.HasIndex(p => p.Name)
.IsUnique();
builder.Entity<Category>()
.HasIndex(c => c.Name)
.IsUnique();
builder.Entity<MenuItem>()
.HasIndex(m => m.Name)
.IsUnique();
builder.Entity<Config>()
.HasIndex(c => c.Name)
.IsUnique();
builder.Entity<ConfigContainer>()
.HasIndex(cc => cc.Name)
.IsUnique();
builder.Entity<Country>()
.HasIndex(c => c.Name)
.IsUnique();
builder.Entity<Program>()
.HasIndex(p => p.Name)
.IsUnique();
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
var audits = OnBeforeSaveChanges();
var result = base.SaveChanges(acceptAllChangesOnSuccess);
if (audits.Any())
{
OnAfterSaveChanges(audits);
base.SaveChanges(acceptAllChangesOnSuccess);
}
return result;
}
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
var audits = OnBeforeSaveChanges();
var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
if(audits.Any())
{
OnAfterSaveChanges(audits);
await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
return result;
}
private List<Audit> OnBeforeSaveChanges()
{
ChangeTracker.DetectChanges();
var audits = new List<Audit>();
foreach (var entry in ChangeTracker.Entries())
{
if (entry.Entity is not ModelBase
|| entry.State == EntityState.Detached
|| entry.State == EntityState.Unchanged)
{
continue;
}
var now = DateTime.Now;
if (entry.State.Equals(EntityState.Added))
{
((ModelBase)entry.Entity).CreatedDate = now;
((ModelBase)entry.Entity).CreatedBy = user ?? null;
((ModelBase)entry.Entity).ModifiedDate = now;
((ModelBase)entry.Entity).ModifiedBy = user ?? null;
}
else if (entry.State.Equals(EntityState.Modified))
{
((ModelBase)entry.Entity).ModifiedDate = now;
((ModelBase)entry.Entity).ModifiedBy = user ?? null;
}
var audit = new Audit
{
TableName = entry.Metadata.GetTableName(),
ClrType = entry.Metadata.ClrType.Name,
Action = entry.State == EntityState.Added ? "ADD" : entry.State == EntityState.Modified ? "UPDATE" : "DELETE",
User = user,
DateTime = now
};
foreach (var property in entry.Properties)
{
if (property.IsTemporary)
{
audit.TemporaryProperties.Add(property);
}
if (property.Metadata.IsPrimaryKey())
{
audit.EntityId = property.CurrentValue.ToString();
}
string propertyName = property.Metadata.Name;
switch (entry.State)
{
case EntityState.Added:
audit.NewValuesDictionary[propertyName] = property.CurrentValue;
break;
case EntityState.Deleted:
audit.OldValuesDictionary[propertyName] = property.OriginalValue;
break;
case EntityState.Modified:
if (property.IsModified)
{
audit.OldValuesDictionary[propertyName] = property.OriginalValue;
audit.NewValuesDictionary[propertyName] = property.CurrentValue;
}
break;
}
}
audits.Add(audit);
}
foreach(var audit in audits.Where(e => !e.TemporaryProperties.Any()))
{
Audits.Add(SerializeValues(audit));
}
return audits.Where(e => e.TemporaryProperties.Any()).ToList();
}
private void OnAfterSaveChanges(List<Audit> audits)
{
foreach (var audit in audits)
{
foreach (var prop in audit.TemporaryProperties)
{
if (prop.Metadata.IsPrimaryKey())
{
audit.EntityId = prop.CurrentValue.ToString();
}
audit.NewValuesDictionary[prop.Metadata.Name] = prop.CurrentValue;
}
Audits.Add(SerializeValues(audit));
}
}
private static Audit SerializeValues(Audit audit)
{
if(audit.OldValuesDictionary.Any())
{
audit.OldValues = JsonSerializer.Serialize(audit.OldValuesDictionary);
}
if (audit.NewValuesDictionary.Any())
{
audit.NewValues = JsonSerializer.Serialize(audit.NewValuesDictionary);
}
return audit;
}
}
}