From 6fa7c0952ede74d89210248f1e05113b46c1a533 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Mon, 9 Nov 2020 10:27:44 +0300 Subject: [PATCH] Update MongoAuditLogRepository.cs Co-Authored-By: maliming <6908465+maliming@users.noreply.github.com> --- .../MongoDB/MongoAuditLogRepository.cs | 71 ++++--------------- 1 file changed, 12 insertions(+), 59 deletions(-) diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs index 03f61ca378..44ae98fcbf 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs @@ -171,16 +171,11 @@ namespace Volo.Abp.AuditLogging.MongoDB { var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); - var auditLogs = await query.As>() - .PageBy>(skipCount, maxResultCount) + var auditLogs = await query.As>() + .PageBy>(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)); - // TODO: Improve this specification - - return auditLogs - .SelectMany(x => x.EntityChanges.Where(y => - IsSatisfiedEntityChange(y, auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName))) - .AsQueryable().OrderBy(sorting ?? "changeTime desc").ToList(); + return auditLogs.AsQueryable().OrderBy(sorting ?? "changeTime desc").ToList(); } public virtual async Task GetEntityChangeCountAsync( @@ -193,8 +188,8 @@ namespace Volo.Abp.AuditLogging.MongoDB CancellationToken cancellationToken = default) { var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); - var count = await query.As>() - .LongCountAsync(GetCancellationToken(cancellationToken)); + + var count = await query.As>().LongCountAsync(GetCancellationToken(cancellationToken)); return count; } @@ -228,7 +223,7 @@ namespace Volo.Abp.AuditLogging.MongoDB {EntityChange = x, UserName = auditLogs.First(y => y.Id == x.AuditLogId).UserName}).ToList(); } - protected virtual IQueryable GetEntityChangeListQuery( + protected virtual IQueryable GetEntityChangeListQuery( Guid? auditLogId = null, DateTime? startTime = null, DateTime? endTime = null, @@ -237,56 +232,14 @@ namespace Volo.Abp.AuditLogging.MongoDB string entityTypeFullName = null) { return GetMongoQueryable() - .Where(x => x.EntityChanges != null) + .SelectMany(x => x.EntityChanges) .WhereIf(auditLogId.HasValue, e => e.Id == auditLogId) - .WhereIf(startTime.HasValue, e => e.EntityChanges.Any(ec => ec.ChangeTime >= startTime)) - .WhereIf(endTime.HasValue, e => e.EntityChanges.Any(ec => ec.ChangeTime >= endTime)) - .WhereIf(changeType.HasValue, e => e.EntityChanges.Any(ec => ec.ChangeType == changeType)) - .WhereIf(!string.IsNullOrWhiteSpace(entityId), e => e.EntityChanges.Any(ec => ec.EntityId == entityId)) + .WhereIf(startTime.HasValue, e => e.ChangeTime >= startTime) + .WhereIf(endTime.HasValue, e => e.ChangeTime <= endTime) + .WhereIf(changeType.HasValue, e => e.ChangeType == changeType) + .WhereIf(!string.IsNullOrWhiteSpace(entityId), e => e.EntityId == entityId) .WhereIf(!string.IsNullOrWhiteSpace(entityTypeFullName), - e => e.EntityChanges.Any(ec => ec.EntityTypeFullName.Contains(entityTypeFullName))); - } - - protected virtual bool IsSatisfiedEntityChange( - EntityChange entityChange, - Guid? auditLogId = null, - DateTime? startTime = null, - DateTime? endTime = null, - EntityChangeType? changeType = null, - string entityId = null, - string entityTypeFullName = null) - { - if (auditLogId != null && auditLogId != entityChange.AuditLogId) - { - return false; - } - - if (startTime != null && startTime.Value >= entityChange.ChangeTime) - { - return false; - } - - if (endTime != null && endTime.Value <= entityChange.ChangeTime) - { - return false; - } - - if (changeType != null && changeType != entityChange.ChangeType) - { - return false; - } - - if (entityId != null && entityId != entityChange.EntityId) - { - return false; - } - - if (entityTypeFullName != null && entityChange.EntityTypeFullName.Contains(entityTypeFullName)) - { - return false; - } - - return true; + e => e.EntityTypeFullName.Contains(entityTypeFullName)); } } }