From 55158bf1ada731be90827e7bff62ad059319ed85 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Wed, 8 Apr 2020 12:02:34 +0300 Subject: [PATCH 1/4] entityhistory implemented to auditlog repository --- .../Volo/Abp/AuditLogging/EntityHistory.cs | 9 ++ .../Abp/AuditLogging/IAuditLogRepository.cs | 2 + .../EfCoreAuditLogRepository.cs | 9 ++ .../MongoDB/MongoAuditLogRepository.cs | 13 ++ .../AuditLogging/AuditLogRepository_Tests.cs | 120 ++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs new file mode 100644 index 0000000000..981eba161b --- /dev/null +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.AuditLogging +{ + public class EntityHistory + { + public EntityChange EntityChange { get; set; } + + public string UserName { get; set; } + } +} \ No newline at end of file diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs index 0e907b2a8e..ed0666f2e2 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs @@ -69,5 +69,7 @@ namespace Volo.Abp.AuditLogging string entityId = null, string entityTypeFullName = null, CancellationToken cancellationToken = default); + + Task> GetEntityHistoriesAsync(string entityId, string entityTypeFullName); } } diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs index 80e381e02b..690273ae1f 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs @@ -182,6 +182,15 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore return totalCount; } + public virtual async Task> GetEntityHistoriesAsync(string entityId, string entityTypeFullName) + { + var query = DbContext.Set().AsNoTracking().IncludeDetails().Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); + + return await (from e in query + join auditLog in DbSet on e.AuditLogId equals auditLog.Id + select new EntityHistory() {EntityChange = e, UserName = auditLog.UserName}).ToListAsync(); + } + protected virtual IQueryable GetEntityChangeListQuery( Guid? auditLogId = null, DateTime? startTime = null, 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 3541dfc88d..cc7248c9f2 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 @@ -191,6 +191,19 @@ namespace Volo.Abp.AuditLogging.MongoDB return count; } + public virtual async Task> GetEntityHistoriesAsync(string entityId, string entityTypeFullName) + { + var auditLogs = await GetMongoQueryable().Where(x => + x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)).As>().ToListAsync(); + + var entityChanges = auditLogs.SelectMany(x => x.EntityChanges).ToList(); + + entityChanges.RemoveAll(x => x.EntityId != entityId || x.EntityTypeFullName != entityTypeFullName); + + return entityChanges.Select(x => new EntityHistory() + {EntityChange = x, UserName = auditLogs.First(y => y.Id == x.AuditLogId).UserName}).ToList(); + } + protected virtual IQueryable GetEntityChangeListQuery( Guid? auditLogId = null, DateTime? startTime = null, diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs index 6de00a0f49..25720e83aa 100644 --- a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using System.Net; using System.Threading.Tasks; +using Microsoft.VisualBasic; using Shouldly; using Volo.Abp.Auditing; using Volo.Abp.Guids; @@ -761,5 +762,124 @@ namespace Volo.Abp.AuditLogging entityChanges.ShouldNotBeNull(); entityChanges.Count.ShouldBe(1); } + + [Fact] + public async Task GetEntityHistoryAsync() + { + // Arrange + var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); + var userId2 = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); + var ipAddress = "153.1.7.61"; + var firstComment = "first Comment"; + + var firstUser = "Douglas"; + var secondUser = "John Doe"; + + var entityId = Guid.NewGuid().ToString(); + var entityType = "Volo.Abp.AuditLogging.TestEntity"; + + var log1 = new AuditLogInfo + { + UserId = userId, + ImpersonatorUserId = Guid.NewGuid(), + ImpersonatorTenantId = Guid.NewGuid(), + ExecutionTime = DateTime.Today, + ExecutionDuration = 42, + ClientIpAddress = ipAddress, + ClientName = "MyDesktop", + BrowserInfo = "Chrome", + Comments = new List { firstComment, "Second Comment" }, + UserName = firstUser, + EntityChanges = { + new EntityChangeInfo + { + EntityId = entityId, + EntityTypeFullName = entityType, + ChangeType = EntityChangeType.Created, + ChangeTime = DateTime.Now, + PropertyChanges = new List + { + new EntityPropertyChangeInfo + { + PropertyTypeFullName = typeof(string).FullName, + PropertyName = "Name", + NewValue = "New value", + OriginalValue = null + } + } + }, + new EntityChangeInfo + { + EntityId = Guid.NewGuid().ToString(), + EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity", + ChangeType = EntityChangeType.Created, + ChangeTime = DateTime.Now, + PropertyChanges = new List + { + new EntityPropertyChangeInfo + { + PropertyTypeFullName = typeof(string).FullName, + PropertyName = "Name", + NewValue = "New value", + OriginalValue = null + } + } + } + + } + }; + + var log2 = new AuditLogInfo + { + UserId = userId2, + ImpersonatorUserId = Guid.NewGuid(), + ImpersonatorTenantId = Guid.NewGuid(), + ExecutionTime = DateTime.Today, + ExecutionDuration = 42, + ClientIpAddress = ipAddress, + ClientName = "MyDesktop", + BrowserInfo = "Chrome", + Comments = new List { firstComment, "Second Comment" }, + HttpStatusCode = (int?)HttpStatusCode.Accepted, + UserName = secondUser, + EntityChanges = { + new EntityChangeInfo + { + EntityId = entityId, + EntityTypeFullName = entityType, + ChangeType = EntityChangeType.Updated, + ChangeTime = DateTime.Now, + PropertyChanges = new List + { + new EntityPropertyChangeInfo + { + PropertyTypeFullName = typeof(string).FullName, + PropertyName = "Name", + NewValue = "New value", + OriginalValue = null + } + } + } + + } + }; + + await AuditLogRepository.InsertAsync(new AuditLog(GuidGenerator, log1)); + await AuditLogRepository.InsertAsync(new AuditLog(GuidGenerator, log2)); + + //Assert + var entityHistory = await AuditLogRepository.GetEntityHistoriesAsync(entityId, entityType); + + entityHistory.Count.ShouldBe(2); + var firstUserChange = entityHistory.First(x => x.UserName == firstUser); + firstUserChange.ShouldNotBeNull(); + firstUserChange.EntityChange.ShouldNotBeNull(); + firstUserChange.EntityChange.ChangeType.ShouldBe(EntityChangeType.Created); + + var secondUserChange = entityHistory.First(x => x.UserName == secondUser); + secondUserChange.ShouldNotBeNull(); + secondUserChange.EntityChange.ShouldNotBeNull(); + secondUserChange.EntityChange.ChangeType.ShouldBe(EntityChangeType.Updated); + } } } From e978ab328b6caae06e62efda9eda6adbcfb14c3c Mon Sep 17 00:00:00 2001 From: Ahmet Date: Wed, 8 Apr 2020 12:46:33 +0300 Subject: [PATCH 2/4] renamed --- ...History.cs => EntityChangeWithUsername.cs} | 2 +- .../Abp/AuditLogging/IAuditLogRepository.cs | 4 +- .../EfCoreAuditLogRepository.cs | 16 ++- .../MongoDB/MongoAuditLogRepository.cs | 17 ++- .../AuditLogging/AuditLogRepository_Tests.cs | 109 +++++++++++++++++- 5 files changed, 140 insertions(+), 8 deletions(-) rename modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/{EntityHistory.cs => EntityChangeWithUsername.cs} (78%) diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChangeWithUsername.cs similarity index 78% rename from modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs rename to modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChangeWithUsername.cs index 981eba161b..8f59b2900e 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityHistory.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChangeWithUsername.cs @@ -1,6 +1,6 @@ namespace Volo.Abp.AuditLogging { - public class EntityHistory + public class EntityChangeWithUsername { public EntityChange EntityChange { get; set; } diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs index ed0666f2e2..ad0e0d20c2 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs @@ -70,6 +70,8 @@ namespace Volo.Abp.AuditLogging string entityTypeFullName = null, CancellationToken cancellationToken = default); - Task> GetEntityHistoriesAsync(string entityId, string entityTypeFullName); + Task GetEntityChangeWithUsernameAsync(Guid entityChangeId); + + Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName); } } diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs index 690273ae1f..1ae28a8478 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs @@ -182,13 +182,25 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore return totalCount; } - public virtual async Task> GetEntityHistoriesAsync(string entityId, string entityTypeFullName) + public virtual async Task GetEntityChangeWithUsernameAsync(Guid entityChangeId) + { + var auditLog = await DbSet.AsNoTracking().IncludeDetails() + .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)).FirstAsync(); + + return new EntityChangeWithUsername() + { + EntityChange = auditLog.EntityChanges.First(x => x.Id == entityChangeId), + UserName = auditLog.UserName + }; + } + + public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { var query = DbContext.Set().AsNoTracking().IncludeDetails().Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); return await (from e in query join auditLog in DbSet on e.AuditLogId equals auditLog.Id - select new EntityHistory() {EntityChange = e, UserName = auditLog.UserName}).ToListAsync(); + select new EntityChangeWithUsername() {EntityChange = e, UserName = auditLog.UserName}).ToListAsync(); } protected virtual IQueryable GetEntityChangeListQuery( 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 cc7248c9f2..2e4d9895df 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 @@ -191,7 +191,20 @@ namespace Volo.Abp.AuditLogging.MongoDB return count; } - public virtual async Task> GetEntityHistoriesAsync(string entityId, string entityTypeFullName) + public virtual async Task GetEntityChangeWithUsernameAsync(Guid entityChangeId) + { + var auditLog = (await GetMongoQueryable() + .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)) + .FirstAsync()); + + return new EntityChangeWithUsername() + { + EntityChange = auditLog.EntityChanges.First(x => x.Id == entityChangeId), + UserName = auditLog.UserName + }; + } + + public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { var auditLogs = await GetMongoQueryable().Where(x => x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)).As>().ToListAsync(); @@ -200,7 +213,7 @@ namespace Volo.Abp.AuditLogging.MongoDB entityChanges.RemoveAll(x => x.EntityId != entityId || x.EntityTypeFullName != entityTypeFullName); - return entityChanges.Select(x => new EntityHistory() + return entityChanges.Select(x => new EntityChangeWithUsername() {EntityChange = x, UserName = auditLogs.First(y => y.Id == x.AuditLogId).UserName}).ToList(); } diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs index 25720e83aa..68c56bf5a7 100644 --- a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs @@ -764,7 +764,7 @@ namespace Volo.Abp.AuditLogging } [Fact] - public async Task GetEntityHistoryAsync() + public async Task GetEntityChangesWithUsernameAsync() { // Arrange var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); @@ -868,7 +868,7 @@ namespace Volo.Abp.AuditLogging await AuditLogRepository.InsertAsync(new AuditLog(GuidGenerator, log2)); //Assert - var entityHistory = await AuditLogRepository.GetEntityHistoriesAsync(entityId, entityType); + var entityHistory = await AuditLogRepository.GetEntityChangesWithUsernameAsync(entityId, entityType); entityHistory.Count.ShouldBe(2); var firstUserChange = entityHistory.First(x => x.UserName == firstUser); @@ -881,5 +881,110 @@ namespace Volo.Abp.AuditLogging secondUserChange.EntityChange.ShouldNotBeNull(); secondUserChange.EntityChange.ChangeType.ShouldBe(EntityChangeType.Updated); } + + [Fact] + public async Task GetEntityChangeWithUsernameAsync() + { + // Arrange + var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); + var userId2 = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); + var ipAddress = "153.1.7.61"; + var firstComment = "first Comment"; + + var log1 = new AuditLogInfo + { + UserId = userId, + ImpersonatorUserId = Guid.NewGuid(), + ImpersonatorTenantId = Guid.NewGuid(), + ExecutionTime = DateTime.Today, + ExecutionDuration = 42, + ClientIpAddress = ipAddress, + ClientName = "MyDesktop", + BrowserInfo = "Chrome", + Comments = new List { firstComment, "Second Comment" }, + UserName = "Douglas", + EntityChanges = { + new EntityChangeInfo + { + EntityId = Guid.NewGuid().ToString(), + EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity_Deleted", + ChangeType = EntityChangeType.Deleted, + ChangeTime = new DateTime(1995, 3, 27), + PropertyChanges = new List + { + new EntityPropertyChangeInfo + { + PropertyTypeFullName = typeof(string).FullName, + PropertyName = "Name", + NewValue = "New value", + OriginalValue = null + } + } + }, + new EntityChangeInfo + { + EntityId = Guid.NewGuid().ToString(), + EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity_Created", + ChangeType = EntityChangeType.Created, + ChangeTime = DateTime.Now, + PropertyChanges = new List + { + new EntityPropertyChangeInfo + { + PropertyTypeFullName = typeof(string).FullName, + PropertyName = "Name", + NewValue = "New value", + OriginalValue = null + } + } + } + + } + }; + + var log2 = new AuditLogInfo + { + UserId = userId2, + ImpersonatorUserId = Guid.NewGuid(), + ImpersonatorTenantId = Guid.NewGuid(), + ExecutionTime = DateTime.Today, + ExecutionDuration = 42, + ClientIpAddress = ipAddress, + ClientName = "MyDesktop", + BrowserInfo = "Chrome", + Comments = new List { firstComment, "Second Comment" }, + HttpStatusCode = (int?)HttpStatusCode.BadGateway, + UserName = "John Doe", + EntityChanges = { + new EntityChangeInfo + { + EntityId = Guid.NewGuid().ToString(), + EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity_Updated", + ChangeType = EntityChangeType.Updated, + ChangeTime = DateTime.Now, + PropertyChanges = new List + { + new EntityPropertyChangeInfo + { + PropertyTypeFullName = typeof(string).FullName, + PropertyName = "Name", + NewValue = "New value", + OriginalValue = null + } + } + } + } + }; + + await AuditLogRepository.InsertAsync(new AuditLog(GuidGenerator, log1)); + await AuditLogRepository.InsertAsync(new AuditLog(GuidGenerator, log2)); + + var entityChanges = await AuditLogRepository.GetEntityChangeListAsync(); + var entityHistory = + await AuditLogRepository.GetEntityChangeWithUsernameAsync(entityChanges.First().Id); + + entityHistory.EntityChange.ChangeTime.ShouldBe(entityChanges.First().ChangeTime); + entityHistory.UserName.ShouldNotBeNull();; + } } } From 05df59b2a7a73b48503f23fec6ba7776c5332887 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Wed, 8 Apr 2020 13:35:08 +0300 Subject: [PATCH 3/4] query order implemented --- .../EntityFrameworkCore/EfCoreAuditLogRepository.cs | 2 +- .../Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs index 1ae28a8478..0e127ba671 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs @@ -196,7 +196,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { - var query = DbContext.Set().AsNoTracking().IncludeDetails().Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); + var query = DbContext.Set().AsNoTracking().IncludeDetails().Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName).OrderBy(x => x.ChangeTime); return await (from e in query join auditLog in DbSet on e.AuditLogId equals auditLog.Id 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 2e4d9895df..eaa5146d24 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 @@ -207,7 +207,7 @@ namespace Volo.Abp.AuditLogging.MongoDB public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { var auditLogs = await GetMongoQueryable().Where(x => - x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)).As>().ToListAsync(); + x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)).As>().OrderBy(x => x.ExecutionTime).ToListAsync(); var entityChanges = auditLogs.SelectMany(x => x.EntityChanges).ToList(); From 3fe584f8f58483bc14eb14edb191fbe483d00103 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Wed, 8 Apr 2020 13:47:26 +0300 Subject: [PATCH 4/4] queryOrder updated --- .../EntityFrameworkCore/EfCoreAuditLogRepository.cs | 8 ++++++-- .../Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs | 7 +++++-- .../Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs index 0e127ba671..06dcf8ffed 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs @@ -196,11 +196,15 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { - var query = DbContext.Set().AsNoTracking().IncludeDetails().Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName).OrderBy(x => x.ChangeTime); + var query = DbContext.Set() + .AsNoTracking() + .IncludeDetails() + .Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); return await (from e in query join auditLog in DbSet on e.AuditLogId equals auditLog.Id - select new EntityChangeWithUsername() {EntityChange = e, UserName = auditLog.UserName}).ToListAsync(); + select new EntityChangeWithUsername() {EntityChange = e, UserName = auditLog.UserName}) + .OrderByDescending(x => x.EntityChange.ChangeTime).ToListAsync(); } protected virtual IQueryable GetEntityChangeListQuery( 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 eaa5146d24..e2f21bfe24 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 @@ -206,8 +206,11 @@ namespace Volo.Abp.AuditLogging.MongoDB public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { - var auditLogs = await GetMongoQueryable().Where(x => - x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)).As>().OrderBy(x => x.ExecutionTime).ToListAsync(); + var auditLogs = await GetMongoQueryable() + .Where(x => x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)) + .As>() + .OrderByDescending(x => x.ExecutionTime) + .ToListAsync(); var entityChanges = auditLogs.SelectMany(x => x.EntityChanges).ToList(); diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs index 68c56bf5a7..3214ee75af 100644 --- a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogRepository_Tests.cs @@ -984,7 +984,7 @@ namespace Volo.Abp.AuditLogging await AuditLogRepository.GetEntityChangeWithUsernameAsync(entityChanges.First().Id); entityHistory.EntityChange.ChangeTime.ShouldBe(entityChanges.First().ChangeTime); - entityHistory.UserName.ShouldNotBeNull();; + entityHistory.UserName.ShouldNotBeNull(); } } }