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 6677f2d4a2..edb1f3f599 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 @@ -39,7 +39,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore bool includeDetails = false, CancellationToken cancellationToken = default) { - var query = GetListQuery( + var query = await GetListQueryAsync( startTime, endTime, httpMethod, @@ -75,7 +75,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore HttpStatusCode? httpStatusCode = null, CancellationToken cancellationToken = default) { - var query = GetListQuery( + var query = await GetListQueryAsync( startTime, endTime, httpMethod, @@ -94,7 +94,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore return totalCount; } - protected virtual IQueryable GetListQuery( + protected virtual async Task> GetListQueryAsync( DateTime? startTime = null, DateTime? endTime = null, string httpMethod = null, @@ -109,7 +109,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore bool includeDetails = false) { var nHttpStatusCode = (int?) httpStatusCode; - return DbSet.AsNoTracking() + return (await GetDbSetAsync()).AsNoTracking() .IncludeDetails(includeDetails) .WhereIf(startTime.HasValue, auditLog => auditLog.ExecutionTime >= startTime) .WhereIf(endTime.HasValue, auditLog => auditLog.ExecutionTime <= endTime) @@ -127,7 +127,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore public virtual async Task> GetAverageExecutionDurationPerDayAsync(DateTime startDate, DateTime endDate) { - var result = await DbSet.AsNoTracking() + var result = await (await GetDbSetAsync()).AsNoTracking() .Where(a => a.ExecutionTime < endDate.AddDays(1) && a.ExecutionTime > startDate) .OrderBy(t => t.ExecutionTime) .GroupBy(t => new { t.ExecutionTime.Date }) @@ -137,14 +137,20 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore return result.ToDictionary(element => element.Day.ClearTime(), element => element.avgExecutionTime); } + [Obsolete("Use WithDetailsAsync method.")] public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); } + public override async Task> WithDetailsAsync() + { + return (await GetQueryableAsync()).IncludeDetails(); + } + public virtual async Task GetEntityChange(Guid entityChangeId) { - var entityChange = await DbContext.Set() + var entityChange = await (await GetDbContextAsync()).Set() .AsNoTracking() .IncludeDetails() .Where(x => x.Id == entityChangeId) @@ -172,7 +178,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore bool includeDetails = false, CancellationToken cancellationToken = default) { - var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName, includeDetails); + var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName, includeDetails); return await query.OrderBy(sorting ?? "changeTime desc") .PageBy(skipCount, maxResultCount) @@ -188,7 +194,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore string entityTypeFullName = null, CancellationToken cancellationToken = default) { - var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); + var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); var totalCount = await query.LongCountAsync(GetCancellationToken(cancellationToken)); @@ -197,7 +203,7 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore public virtual async Task GetEntityChangeWithUsernameAsync(Guid entityChangeId) { - var auditLog = await DbSet.AsNoTracking().IncludeDetails() + var auditLog = await (await GetDbSetAsync()).AsNoTracking().IncludeDetails() .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)).FirstAsync(); return new EntityChangeWithUsername() @@ -209,18 +215,20 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { - var query = DbContext.Set() + var dbContext = await GetDbContextAsync(); + + 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}) + join auditLog in dbContext.AuditLogs on e.AuditLogId equals auditLog.Id + select new EntityChangeWithUsername {EntityChange = e, UserName = auditLog.UserName}) .OrderByDescending(x => x.EntityChange.ChangeTime).ToListAsync(); } - protected virtual IQueryable GetEntityChangeListQuery( + protected virtual async Task> GetEntityChangeListQueryAsync( Guid? auditLogId = null, DateTime? startTime = null, DateTime? endTime = null, @@ -229,14 +237,16 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore string entityTypeFullName = null, bool includeDetails = false) { - return DbContext.Set().AsNoTracking().IncludeDetails(includeDetails) - .WhereIf(auditLogId.HasValue, e => e.AuditLogId == auditLogId) - .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.EntityTypeFullName.Contains(entityTypeFullName)); + return (await GetDbContextAsync()) + .Set() + .AsNoTracking() + .IncludeDetails(includeDetails) + .WhereIf(auditLogId.HasValue, e => e.AuditLogId == auditLogId) + .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.EntityTypeFullName.Contains(entityTypeFullName)); } } } 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 14e4dd39c7..95981d6355 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 @@ -40,7 +40,7 @@ namespace Volo.Abp.AuditLogging.MongoDB bool includeDetails = false, CancellationToken cancellationToken = default) { - var query = GetListQuery( + var query = await GetListQueryAsync( startTime, endTime, httpMethod, @@ -74,7 +74,7 @@ namespace Volo.Abp.AuditLogging.MongoDB HttpStatusCode? httpStatusCode = null, CancellationToken cancellationToken = default) { - var query = GetListQuery( + var query = await GetListQueryAsync( startTime, endTime, httpMethod, @@ -94,7 +94,7 @@ namespace Volo.Abp.AuditLogging.MongoDB return count; } - protected virtual IQueryable GetListQuery( + protected virtual async Task> GetListQueryAsync( DateTime? startTime = null, DateTime? endTime = null, string httpMethod = null, @@ -108,7 +108,7 @@ namespace Volo.Abp.AuditLogging.MongoDB HttpStatusCode? httpStatusCode = null, bool includeDetails = false) { - return GetMongoQueryable() + return (await GetMongoQueryableAsync()) .WhereIf(startTime.HasValue, auditLog => auditLog.ExecutionTime >= startTime) .WhereIf(endTime.HasValue, auditLog => auditLog.ExecutionTime <= endTime) .WhereIf(hasException.HasValue && hasException.Value, auditLog => auditLog.Exceptions != null && auditLog.Exceptions != "") @@ -126,7 +126,7 @@ namespace Volo.Abp.AuditLogging.MongoDB public virtual async Task> GetAverageExecutionDurationPerDayAsync(DateTime startDate, DateTime endDate) { - var result = await GetMongoQueryable() + var result = await (await GetMongoQueryableAsync()) .Where(a => a.ExecutionTime < endDate.AddDays(1) && a.ExecutionTime > startDate) .OrderBy(t => t.ExecutionTime) .GroupBy(t => new @@ -143,12 +143,11 @@ namespace Volo.Abp.AuditLogging.MongoDB public virtual async Task GetEntityChange(Guid entityChangeId) { - var entityChange = (await GetMongoQueryable() + var entityChange = (await (await GetMongoQueryableAsync()) .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)) .OrderBy(x => x.Id) .FirstAsync()).EntityChanges.FirstOrDefault(x => x.Id == entityChangeId); - if (entityChange == null) { throw new EntityNotFoundException(typeof(EntityChange)); @@ -170,7 +169,7 @@ namespace Volo.Abp.AuditLogging.MongoDB bool includeDetails = false, CancellationToken cancellationToken = default) { - var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); + var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); var auditLogs = await query.As>() .PageBy>(skipCount, maxResultCount) @@ -188,7 +187,7 @@ namespace Volo.Abp.AuditLogging.MongoDB string entityTypeFullName = null, CancellationToken cancellationToken = default) { - var query = GetEntityChangeListQuery(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); + var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); var count = await query.As>().LongCountAsync(GetCancellationToken(cancellationToken)); @@ -197,7 +196,7 @@ namespace Volo.Abp.AuditLogging.MongoDB public virtual async Task GetEntityChangeWithUsernameAsync(Guid entityChangeId) { - var auditLog = (await GetMongoQueryable() + var auditLog = (await (await GetMongoQueryableAsync()) .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)) .FirstAsync()); @@ -210,7 +209,7 @@ namespace Volo.Abp.AuditLogging.MongoDB public virtual async Task> GetEntityChangesWithUsernameAsync(string entityId, string entityTypeFullName) { - var auditLogs = await GetMongoQueryable() + var auditLogs = await (await GetMongoQueryableAsync()) .Where(x => x.EntityChanges.Any(y => y.EntityId == entityId && y.EntityTypeFullName == entityTypeFullName)) .As>() .OrderByDescending(x => x.ExecutionTime) @@ -224,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 async Task> GetEntityChangeListQueryAsync( Guid? auditLogId = null, DateTime? startTime = null, DateTime? endTime = null, @@ -232,7 +231,7 @@ namespace Volo.Abp.AuditLogging.MongoDB string entityId = null, string entityTypeFullName = null) { - return GetMongoQueryable() + return (await GetMongoQueryableAsync()) .SelectMany(x => x.EntityChanges) .WhereIf(auditLogId.HasValue, e => e.Id == auditLogId) .WhereIf(startTime.HasValue, e => e.ChangeTime >= startTime)