IW4M-Admin/SharedLibraryCore/Repositories/AuditInformationRepository.cs

55 lines
2.3 KiB
C#
Raw Normal View History

2022-01-26 11:32:16 -05:00
using System.Collections.Generic;
2020-04-28 17:48:06 -04:00
using System.Linq;
using System.Threading.Tasks;
using Data.Abstractions;
2022-01-26 11:32:16 -05:00
using Data.Models;
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Interfaces;
2020-04-28 17:48:06 -04:00
namespace SharedLibraryCore.Repositories
{
/// <summary>
2022-01-26 11:32:16 -05:00
/// implementation if IAuditInformationRepository
2020-04-28 17:48:06 -04:00
/// </summary>
public class AuditInformationRepository : IAuditInformationRepository
{
private readonly IDatabaseContextFactory _contextFactory;
public AuditInformationRepository(IDatabaseContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
2022-01-26 11:32:16 -05:00
/// <inheritdoc />
public async Task<IList<AuditInfo>> ListAuditInformation(PaginationRequest paginationInfo)
2020-04-28 17:48:06 -04:00
{
2022-01-26 11:32:16 -05:00
await using var ctx = _contextFactory.CreateContext(false);
var iqItems = (from change in ctx.EFChangeHistory
2022-01-26 11:32:16 -05:00
where change.TypeOfChange != EFChangeHistory.ChangeType.Ban
orderby change.TimeChanged descending
join originClient in ctx.Clients
on change.ImpersonationEntityId ?? change.OriginEntityId equals originClient.ClientId
join targetClient in ctx.Clients
on change.TargetEntityId equals targetClient.ClientId
into targetChange
from targetClient in targetChange.DefaultIfEmpty()
select new AuditInfo
{
Action = change.TypeOfChange.ToString(),
OriginName = originClient.CurrentAlias.Name,
OriginId = originClient.ClientId,
TargetName = targetClient == null ? "" : targetClient.CurrentAlias.Name,
TargetId = targetClient == null ? new int?() : targetClient.ClientId,
When = change.TimeChanged,
Data = change.Comment,
OldValue = change.PreviousValue,
NewValue = change.CurrentValue
})
.Skip(paginationInfo.Offset)
.Take(paginationInfo.Count);
2020-04-28 17:48:06 -04:00
return await iqItems.ToListAsync();
2020-04-28 17:48:06 -04:00
}
}
2022-01-26 11:32:16 -05:00
}