-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreTracker.cs
66 lines (59 loc) · 2.02 KB
/
CoreTracker.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
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using AuditLog.Common;
using AuditLog.Models;
namespace AuditLog
{
public class CoreTracker
{
private readonly ITrackerContext _context;
public CoreTracker(ITrackerContext context)
{
_context = context;
}
public void AuditChanges(object userName)
{
foreach (
DbEntityEntry ent in
_context.ChangeTracker.Entries()
.Where(p => p.State == EntityState.Deleted || p.State == EntityState.Modified))
{
using (var auditer = new LogAuditor(ent))
{
var eventType = GetEventType(ent);
Models.AuditLog record = auditer.CreateLogRecord(userName, eventType, _context);
if (record != null)
{
_context.AuditLog.Add(record);
}
}
}
}
public IEnumerable<DbEntityEntry> GetAdditions()
{
return _context.ChangeTracker.Entries().Where(p => p.State == EntityState.Added).ToList();
}
public void AuditAdditions(object userName, IEnumerable<DbEntityEntry> addedEntries)
{
// Get all Added entities
foreach (DbEntityEntry ent in addedEntries)
{
using (var auditer = new LogAuditor(ent))
{
Models.AuditLog record = auditer.CreateLogRecord(userName, EventType.Added, _context);
if (record != null)
{
_context.AuditLog.Add(record);
}
}
}
}
private EventType GetEventType(DbEntityEntry entry)
{
var eventType = entry.State == EntityState.Modified ? EventType.Modified : EventType.Deleted;
return eventType;
}
}
}