From 75143e9d2f40e426d43df7b42189e297bf88af36 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 19 Jul 2018 10:01:11 +0300 Subject: [PATCH 1/3] Audit Log repository GetListAsync refactoring --- .../Volo/Abp/Paging/PagedResult.cs | 73 ------------------- .../Abp/AuditLogging/IAuditLogRepository.cs | 11 ++- .../EfCoreAuditLogRepository.cs | 46 +++++++++--- .../MongoDB/MongoAuditLogRepository.cs | 34 ++++++--- 4 files changed, 67 insertions(+), 97 deletions(-) delete mode 100644 modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs deleted file mode 100644 index 49e51eb078..0000000000 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Volo.Abp.Paging -{ - public class PagedResult : ListResultDto, IPagedResult - { - /// - public long TotalCount { get; set; } - - /// - /// Creates a new object. - /// - public PagedResult() - { - - } - - /// - /// Creates a new object. - /// - /// Total count of Items - /// List of items in current page - public PagedResult(long totalCount, IReadOnlyList items) - : base(items) - { - TotalCount = totalCount; - } - } - - public class ListResultDto : IListResult - { - /// - public IReadOnlyList Items - { - get { return _items ?? (_items = new List()); } - set { _items = value; } - } - private IReadOnlyList _items; - - /// - /// Creates a new object. - /// - public ListResultDto() - { - - } - - /// - /// Creates a new object. - /// - /// List of items - public ListResultDto(IReadOnlyList items) - { - Items = items; - } - } - - public interface IListResult - { - IReadOnlyList Items { get; set; } - } - - public interface IPagedResult : IListResult, IHasTotalCount - { - } - - public interface IHasTotalCount - { - long TotalCount { 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 53b278ae5d..f02d84102e 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 @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -7,15 +8,21 @@ namespace Volo.Abp.AuditLogging { public interface IAuditLogRepository : IBasicRepository { - Task> GetListAsync( + Task> GetListAsync( string sorting = null, int maxResultCount = 50, int skipCount = 0, - string filter = null, string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false); + + Task GetCountAsync( + string httpMethod = null, + string url = null, + string userName = null, + HttpStatusCode? httpStatusCode = null, + bool includeDetails = true); } } 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 c0bb68d835..5e5dbe6d9b 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 @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Net; @@ -17,31 +18,52 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore } - public async Task> GetListAsync( + public async Task> GetListAsync( string sorting = null, int maxResultCount = 50, int skipCount = 0, - string filter = null, string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, - bool includeDetails = false) + bool includeDetails = true) { - var query = DbSet.AsNoTracking() - .IncludeDetails(includeDetails) - .WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant()) - .WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant())) - .WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName) - .WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode); - - var totalCount = await query.LongCountAsync(); + var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); var auditLogs = await query.OrderBy(sorting) .PageBy(skipCount, maxResultCount) .ToListAsync(); - return new Paging.PagedResult(totalCount, auditLogs); + return auditLogs; + } + + public async Task GetCountAsync( + string httpMethod = null, + string url = null, + string userName = null, + HttpStatusCode? httpStatusCode = null, + bool includeDetails = true) + { + var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); + + var totalCount = await query.LongCountAsync(); + + return totalCount; + } + + private IQueryable GetListQuery( + string httpMethod = null, + string url = null, + string userName = null, + HttpStatusCode? httpStatusCode = null, + bool includeDetails = true) + { + return DbSet.AsNoTracking() + .IncludeDetails(includeDetails) + .WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant()) + .WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant())) + .WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName) + .WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode); } public override IQueryable WithDetails() 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 8c2f831e73..992da23369 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 @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Net; @@ -18,22 +19,35 @@ namespace Volo.Abp.AuditLogging.MongoDB } - public async Task> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, string filter = null, + public async Task> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false) { - var query = GetMongoQueryable() - .WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant()) - .WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant())) - .WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName) - .WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode); + var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); - var totalCount = await query.As>().LongCountAsync(); - - var questions = await query.OrderBy(sorting).As>() + return await query.OrderBy(sorting).As>() .PageBy>(skipCount, maxResultCount) .ToListAsync(); + } + + public async Task GetCountAsync(string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false) + { + var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); - return new Paging.PagedResult(totalCount, questions); + return await query.As>().LongCountAsync(); + } + + private IQueryable GetListQuery( + string httpMethod = null, + string url = null, + string userName = null, + HttpStatusCode? httpStatusCode = null, + bool includeDetails = true) + { + return GetMongoQueryable() + .WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant()) + .WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant())) + .WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName) + .WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode); } } } From bd7177ca36c1f9c75af2d1c770ad4cbede383fef Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 19 Jul 2018 10:38:37 +0300 Subject: [PATCH 2/3] Added Should_Get_List_Of_Audit_Logs Test --- .../AuditLogging/AuditStore_Basic_Tests.cs | 121 +++++++++++++++++- 1 file changed, 118 insertions(+), 3 deletions(-) diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditStore_Basic_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditStore_Basic_Tests.cs index 7b77349b85..b21df0b025 100644 --- a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditStore_Basic_Tests.cs +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditStore_Basic_Tests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Threading.Tasks; using Shouldly; using Volo.Abp.Auditing; @@ -9,7 +10,7 @@ using Xunit; namespace Volo.Abp.AuditLogging { - public abstract class AuditStore_Basic_Tests : AuditLoggingTestBase + public abstract class AuditStore_Basic_Tests : AuditLoggingTestBase where TStartupModule : IAbpModule { private readonly IAuditingStore _auditingStore; @@ -22,7 +23,7 @@ namespace Volo.Abp.AuditLogging } [Fact] - public async Task Should_Save_A_Audit_Log() + public async Task Should_Save_An_Audit_Log() { //Arrange var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); @@ -39,7 +40,7 @@ namespace Volo.Abp.AuditLogging ClientIpAddress = ipAddress, ClientName = "MyDesktop", BrowserInfo = "Chrome", - Comments = new List { firstComment, "Second Comment"}, + Comments = new List { firstComment, "Second Comment" }, EntityChanges = { new EntityChangeInfo @@ -76,5 +77,119 @@ namespace Volo.Abp.AuditLogging insertedLog.EntityChanges.Count.ShouldBeGreaterThan(0); insertedLog.EntityChanges.First().PropertyChanges.Count.ShouldBeGreaterThan(0); } + + + [Fact] + public async Task Should_Get_List_Of_Audit_Logs() + { + var userId = 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", + 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 = userId, + 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, + EntityChanges = { + 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 + } + } + } + + } + }; + + await _auditingStore.SaveAsync(log1); + await _auditingStore.SaveAsync(log2); + + var allLogsCount = await _auditLogRepository.GetCountAsync(); + + var onlyLog1QueryResult = await _auditLogRepository.GetListAsync(includeDetails: true, userName: "Douglas"); + + var onlyLog2QueryResult = await _auditLogRepository.GetListAsync(includeDetails: true, httpStatusCode: HttpStatusCode.BadGateway); + + + allLogsCount.ShouldBe(2); + + onlyLog1QueryResult.Count.ShouldBe(1); + onlyLog1QueryResult.FirstOrDefault().UserName.ShouldBe("Douglas"); + onlyLog1QueryResult.FirstOrDefault().EntityChanges.Count.ShouldBe(2); + + onlyLog2QueryResult.Count.ShouldBe(1); + onlyLog2QueryResult.FirstOrDefault().HttpStatusCode.ShouldBe((int?)HttpStatusCode.BadGateway); + onlyLog2QueryResult.FirstOrDefault().EntityChanges.Count.ShouldBe(1); + } } } From c31c5d1d34b89a64c047c824561a15c8dcfff6e7 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 19 Jul 2018 10:39:04 +0300 Subject: [PATCH 3/3] Audit Logging getalllistasync fixes --- .../Volo/Abp/AuditLogging/IAuditLogRepository.cs | 2 +- .../EntityFrameworkCore/EfCoreAuditLogRepository.cs | 2 +- .../AuditLogging/MongoDB/MongoAuditLogRepository.cs | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) 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 f02d84102e..e746063b49 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 @@ -16,7 +16,7 @@ namespace Volo.Abp.AuditLogging string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, - bool includeDetails = false); + bool includeDetails = true); Task GetCountAsync( string httpMethod = null, 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 5e5dbe6d9b..1fb7fcad7f 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 @@ -30,7 +30,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore { var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); - var auditLogs = await query.OrderBy(sorting) + var auditLogs = await query.OrderBy(sorting ?? "executionTime desc") .PageBy(skipCount, maxResultCount) .ToListAsync(); 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 992da23369..662cfdbc2e 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 @@ -20,20 +20,22 @@ namespace Volo.Abp.AuditLogging.MongoDB } public async Task> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, - string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false) + string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = true) { var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); - return await query.OrderBy(sorting).As>() + return await query.OrderBy(sorting ?? "executionTime desc").As>() .PageBy>(skipCount, maxResultCount) .ToListAsync(); } - public async Task GetCountAsync(string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false) + public async Task GetCountAsync(string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = true) { var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails); - return await query.As>().LongCountAsync(); + var count = await query.As>().LongCountAsync(); + + return count; } private IQueryable GetListQuery(