-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAuditLoggingRepository.cs
55 lines (45 loc) · 2.16 KB
/
AuditLoggingRepository.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
using Microsoft.EntityFrameworkCore;
using Skoruba.AuditLogging.EntityFramework.DbContexts;
using Skoruba.AuditLogging.EntityFramework.Entities;
using Skoruba.AuditLogging.EntityFramework.Helpers;
using Skoruba.AuditLogging.EntityFramework.Helpers.Common;
using System.Threading.Tasks;
namespace Skoruba.AuditLogging.EntityFramework.Repositories
{
public class AuditLoggingRepository<TDbContext, TAuditLog>(TDbContext dbContext) : IAuditLoggingRepository<TAuditLog>
where TDbContext : IAuditLoggingDbContext<TAuditLog>
where TAuditLog : AuditLog
{
protected TDbContext DbContext = dbContext;
public virtual async Task<PagedList<TAuditLog>> GetAsync(int page = 1, int pageSize = 10)
{
var pagedList = new PagedList<TAuditLog>();
var auditLogs = await DbContext.AuditLog
.PageBy(x => x.Id, page, pageSize)
.ToListAsync();
pagedList.Data.AddRange(auditLogs);
pagedList.PageSize = pageSize;
pagedList.TotalCount = await DbContext.AuditLog.CountAsync();
return pagedList;
}
public virtual async Task<PagedList<TAuditLog>> GetAsync(string subjectIdentifier, string subjectName, string category, int page = 1, int pageSize = 10)
{
var pagedList = new PagedList<TAuditLog>();
var auditLogs = await DbContext.AuditLog
.WhereIf(!string.IsNullOrWhiteSpace(subjectIdentifier), x => x.SubjectIdentifier == subjectIdentifier)
.WhereIf(!string.IsNullOrWhiteSpace(subjectName), x => x.SubjectName == subjectName)
.WhereIf(!string.IsNullOrWhiteSpace(category), x => x.Category == category)
.PageBy(x => x.Id, page, pageSize)
.ToListAsync();
pagedList.Data.AddRange(auditLogs);
pagedList.PageSize = pageSize;
pagedList.TotalCount = await DbContext.AuditLog.CountAsync();
return pagedList;
}
public virtual async Task SaveAsync(TAuditLog auditLog)
{
await DbContext.AuditLog.AddAsync(auditLog);
await DbContext.SaveChangesAsync();
}
}
}