-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
122 lines (100 loc) · 3.33 KB
/
Program.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
using System.Text.Json;
using System.Text.Json.Serialization;
using Audit.Core;
using Audit.EntityFramework;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MiniDemo.Model;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("AppDb");
builder.Services.AddTransient<DataSeeder>();
//Add Repository Pattern
builder.Services.AddScoped<IDataRepository, DataRepository>();
builder.Services.AddDbContext<EmployeeDbContext>(x => x.UseSqlServer(connectionString));
builder.Services.AddDbContext<AuditableDbContext>(x => x.UseSqlServer(connectionString));
//Add Swagger Support
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
SetupAudit();
var app = builder.Build();
app.UseSwaggerUI();
if (args.Length == 1 && args[0].ToLower() == "seeddata")
SeedData(app);
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger(x => x.SerializeAsV2 = true);
app.MapGet("/employee/{id}", ([FromServices] IDataRepository db, string id) =>
{
return db.GetEmployeeById(id);
});
app.MapGet("/employees", ([FromServices] IDataRepository db) =>
{
return db.GetEmployees();
}
);
app.MapPut("/employee/{id}", ([FromServices] IDataRepository db, Employee employee) =>
{
return db.PutEmployee(employee);
});
app.MapPost("/employee", ([FromServices] IDataRepository db, Employee employee) =>
{
return db.AddEmployee(employee);
});
app.Run();
void SetupAudit()
{
Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var efEvent = scope.GetEntityFrameworkEvent();
foreach (var entry in efEvent.Entries)
{
if (entry.GetEntry().Entity is not AuditEntity auditEntity)
continue;
var now = DateTime.UtcNow;
if (entry.Action == "Insert")
{
auditEntity.CreatedDate = now;
}
else
{
entry.GetEntry().Property("CreatedDate").IsModified = false;
auditEntity.ModifiedDate = now;
}
}
});
Audit.EntityFramework.Configuration.Setup()
.ForContext<EmployeeDbContext>(config => config
.AuditEventType("EF:{context}")
)
.UseOptIn()
;
Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.UseDbContext<AuditableDbContext>()
.AuditTypeMapper(t => typeof(AuditTrail))
.AuditEntityAction<AuditTrail>((ev, entry, entity) =>
{
entity.JsonData = entry.ToJson();
entity.Action = entry.Action.ToString();
entity.ModifiedDate = DateTime.UtcNow;
entity.TableName = entry.Table;
entity.EventType = ev.EventType.ToString();
entity.HostUserName = ev.Environment.UserName;
var efe = ev.GetEntityFrameworkEvent();
return efe.Success;
})
.IgnoreMatchedProperties()
);
}
//Seed Data
void SeedData(IHost app)
{
var scopedFactory = app.Services.GetService<IServiceScopeFactory>();
using (var scope = scopedFactory.CreateScope())
{
var service = scope.ServiceProvider.GetService<DataSeeder>();
service.Seed();
}
}