diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs index 5595a43b6e..699a42b72a 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs @@ -68,16 +68,16 @@ namespace Volo.Abp.Domain.Repositories { return ApplyDataFilters(query); } - - protected virtual TQueryable ApplyDataFilters(TQueryable query) - where TQueryable : IQueryable + + protected virtual TQueryable ApplyDataFilters(TQueryable query) + where TQueryable : IQueryable { - if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEnt))) + if (typeof(ISoftDelete).IsAssignableFrom(typeof(TOtherEntity))) { query = (TQueryable)query.WhereIf(DataFilter.IsEnabled(), e => ((ISoftDelete)e).IsDeleted == false); } - if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEnt))) + if (typeof(IMultiTenant).IsAssignableFrom(typeof(TOtherEntity))) { var tenantId = CurrentTenant.Id; query = (TQueryable)query.WhereIf(DataFilter.IsEnabled(), e => ((IMultiTenant)e).TenantId == tenantId); diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index e96f35cbb7..d45bd83f3f 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -522,15 +522,15 @@ namespace Volo.Abp.Domain.Repositories.MongoDB { return GetMongoQueryableAsync(cancellationToken); } - - protected virtual async Task> GetMongoQueryableAsync(CancellationToken cancellationToken = default) + + protected virtual async Task> GetMongoQueryableAsync(CancellationToken cancellationToken = default) { cancellationToken = GetCancellationToken(cancellationToken); var dbContext = await GetDbContextAsync(cancellationToken); - var collection = dbContext.Collection(); + var collection = dbContext.Collection(); - return ApplyDataFilters, TEnt>( + return ApplyDataFilters, TOtherEntity>( dbContext.SessionHandle != null ? collection.AsQueryable(dbContext.SessionHandle) : collection.AsQueryable() diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogPostRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogPostRepository.cs index 2d633714ce..48f23e7ebd 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogPostRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogPostRepository.cs @@ -28,26 +28,24 @@ namespace Volo.CmsKit.MongoDB.Blogs Check.NotNullOrEmpty(slug, nameof(slug)); var token = GetCancellationToken(cancellationToken); - + var blogPost = await GetAsync(x => x.BlogId == blogId && x.Slug.ToLower() == slug, cancellationToken: token); - var dbContext = await GetDbContextAsync(token); - - blogPost.Author = await dbContext.Collection().AsQueryable().FirstOrDefaultAsync(x => x.Id == blogPost.AuthorId, token); + blogPost.Author = await (await GetMongoQueryableAsync(token)).FirstOrDefaultAsync(x => x.Id == blogPost.AuthorId, token); return blogPost; } public virtual async Task GetCountAsync( - string filter = null, - Guid? blogId = null, + string filter = null, + Guid? blogId = null, CancellationToken cancellationToken = default) { var token = GetCancellationToken(cancellationToken); - + return await (await GetMongoQueryableAsync(token)) .WhereIf>(!string.IsNullOrWhiteSpace(filter), x => x.Title.Contains(filter) || x.Slug.Contains(filter)) .WhereIf>(blogId.HasValue, x => x.BlogId == blogId) @@ -65,7 +63,7 @@ namespace Volo.CmsKit.MongoDB.Blogs var token = GetCancellationToken(cancellationToken); var dbContext = await GetDbContextAsync(token); var blogPostQueryable = await GetQueryableAsync(); - + var usersQueryable = dbContext.Collection().AsQueryable(); var queryable = blogPostQueryable @@ -102,4 +100,4 @@ namespace Volo.CmsKit.MongoDB.Blogs return await queryable.AnyAsync(x => x.BlogId == blogId && x.Slug.ToLower() == slug, token); } } -} \ No newline at end of file +} diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs index 4d542ca9c8..c15bb31a7f 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs @@ -47,26 +47,26 @@ namespace Volo.CmsKit.MongoDB.Comments } public virtual async Task> GetListAsync( - string filter = null, + string filter = null, string entityType = null, Guid? repliedCommentId = null, string authorUsername = null, - DateTime? creationStartDate = null, - DateTime? creationEndDate = null, + DateTime? creationStartDate = null, + DateTime? creationEndDate = null, string sorting = null, - int maxResultCount = int.MaxValue, - int skipCount = 0, + int maxResultCount = int.MaxValue, + int skipCount = 0, CancellationToken cancellationToken = default ) { var token = GetCancellationToken(cancellationToken); var query = await GetListQueryAsync( - filter, + filter, entityType, - repliedCommentId, - authorUsername, - creationStartDate, - creationEndDate, + repliedCommentId, + authorUsername, + creationStartDate, + creationEndDate, token); var comments = await query.OrderBy(sorting.IsNullOrEmpty() ? "creationTime desc" : sorting) @@ -75,15 +75,15 @@ namespace Volo.CmsKit.MongoDB.Comments .ToListAsync(token); var commentIds = comments.Select(x => x.Id).ToList(); - + var authorsQuery = from comment in (await GetMongoQueryableAsync(token)) join user in (await GetDbContextAsync(token)).CmsUsers on comment.CreatorId equals user.Id where commentIds.Contains(comment.Id) orderby comment.CreationTime select user; - var authors = await authorsQuery.ToListAsync(token); - + var authors = await ApplyDataFilters, CmsUser>(authorsQuery).ToListAsync(token); + return comments .Select( comment => @@ -95,22 +95,22 @@ namespace Volo.CmsKit.MongoDB.Comments } public virtual async Task GetCountAsync( - string text = null, + string text = null, string entityType = null, - Guid? repliedCommentId = null, + Guid? repliedCommentId = null, string authorUsername = null, DateTime? creationStartDate = null, - DateTime? creationEndDate = null, + DateTime? creationEndDate = null, CancellationToken cancellationToken = default ) { var query = await GetListQueryAsync( - text, + text, entityType, - repliedCommentId, - authorUsername, - creationStartDate, - creationEndDate, + repliedCommentId, + authorUsername, + creationStartDate, + creationEndDate, cancellationToken); return await query.As>() @@ -131,7 +131,7 @@ namespace Volo.CmsKit.MongoDB.Comments orderby comment.CreationTime select user; - var authors = await authorsQuery.ToListAsync(GetCancellationToken(cancellationToken)); + var authors = await ApplyDataFilters, CmsUser>(authorsQuery).ToListAsync(GetCancellationToken(cancellationToken)); var comments = await (await GetMongoQueryableAsync(cancellationToken)) .Where(c => c.EntityId == entityId && c.EntityType == entityType) @@ -169,11 +169,11 @@ namespace Volo.CmsKit.MongoDB.Comments ); } } - + protected virtual async Task> GetListQueryAsync( - string filter = null, + string filter = null, string entityType = null, - Guid? repliedCommentId = null, + Guid? repliedCommentId = null, string authorUsername = null, DateTime? creationStartDate = null, DateTime? creationEndDate = null, @@ -181,18 +181,16 @@ namespace Volo.CmsKit.MongoDB.Comments ) { var queryable = await GetMongoQueryableAsync(cancellationToken); - + if (!string.IsNullOrEmpty(authorUsername)) { - var authorQueryable = (await GetDbContextAsync(cancellationToken)).Collection().AsQueryable(); - - var author = await authorQueryable.FirstOrDefaultAsync(x => x.UserName == authorUsername, cancellationToken: cancellationToken); + var author = await (await GetMongoQueryableAsync(cancellationToken)).FirstOrDefaultAsync(x => x.UserName == authorUsername, cancellationToken: cancellationToken); var authorId = author?.Id ?? Guid.Empty; queryable = queryable.Where(x => x.CreatorId == authorId); } - + return queryable.WhereIf(!filter.IsNullOrWhiteSpace(), c => c.Text.Contains(filter)) .WhereIf(!entityType.IsNullOrWhiteSpace(), c => c.EntityType == entityType) .WhereIf(repliedCommentId.HasValue, c => c.RepliedCommentId == repliedCommentId) diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs index 7452709bfd..167c91c4e9 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs @@ -67,7 +67,7 @@ namespace Volo.CmsKit.MongoDB.Tags Check.NotNullOrEmpty(entityType, nameof(entityType)); Check.NotNullOrEmpty(entityId, nameof(entityId)); - var entityTagIds = await (await GetDbContextAsync(cancellationToken)).EntityTags.AsQueryable() + var entityTagIds = await (await GetMongoQueryableAsync(cancellationToken)) .Where(q => q.EntityId == entityId) .Select(q => q.TagId) .ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs index 990c375f9a..42a9065103 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs @@ -41,16 +41,13 @@ namespace Volo.Abp.Identity.MongoDB .Select(r => r.OrganizationUnitId) .ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - - var organizationUnits = dbContext.OrganizationUnits - .AsQueryable() + var organizationUnits = await (await GetMongoQueryableAsync(cancellationToken)) .Where(ou => organizationUnitIds.Contains(ou.Id)) - .ToArray(); + .ToListAsync(cancellationToken: cancellationToken); var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); var roleIds = user.Roles.Select(r => r.RoleId).ToArray(); var allRoleIds = orgUnitRoleIds.Union(roleIds); - return await dbContext.Roles.AsQueryable().Where(r => allRoleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken)); + return await (await GetMongoQueryableAsync(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task> GetRoleNamesInOrganizationUnitAsync( @@ -63,16 +60,13 @@ namespace Volo.Abp.Identity.MongoDB .Select(r => r.OrganizationUnitId) .ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - - var organizationUnits = dbContext.OrganizationUnits - .AsQueryable() + var organizationUnits = await (await GetMongoQueryableAsync(cancellationToken)) .Where(ou => organizationUnitIds.Contains(ou.Id)) - .ToArray(); + .ToListAsync(cancellationToken: cancellationToken); var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); - var queryable = await GetMongoQueryableAsync(cancellationToken); + var queryable = await GetMongoQueryableAsync(cancellationToken); return await queryable .Where(r => roleIds.Contains(r.Id)) @@ -119,7 +113,7 @@ namespace Volo.Abp.Identity.MongoDB cancellationToken = GetCancellationToken(cancellationToken); var queryable = await GetMongoQueryableAsync(cancellationToken); - + var role = await queryable .Where(x => x.NormalizedName == normalizedRoleName) .OrderBy(x => x.Id) @@ -183,16 +177,13 @@ namespace Volo.Abp.Identity.MongoDB .Select(r => r.OrganizationUnitId) .ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - - var organizationUnits = dbContext.OrganizationUnits - .AsQueryable() + var organizationUnits = await (await GetMongoQueryableAsync(cancellationToken)) .Where(ou => organizationUnitIds.Contains(ou.Id)) - .ToArray(); + .ToListAsync(cancellationToken: cancellationToken); var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); var roleIds = user.Roles.Select(r => r.RoleId).ToArray(); var allRoleIds = orgUnitRoleIds.Union(roleIds); - return await dbContext.Roles.AsQueryable().Where(r => allRoleIds.Contains(r.Id)).ToListAsync(GetCancellationToken(cancellationToken)); + return await (await GetMongoQueryableAsync(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task> GetOrganizationUnitsAsync( @@ -203,9 +194,7 @@ namespace Volo.Abp.Identity.MongoDB var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)); var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId); - var dbContext = await GetDbContextAsync(cancellationToken); - - return await dbContext.OrganizationUnits.AsQueryable() + return await (await GetMongoQueryableAsync(cancellationToken)) .Where(ou => organizationUnitIds.Contains(ou.Id)) .ToListAsync(GetCancellationToken(cancellationToken)); } @@ -267,7 +256,7 @@ namespace Volo.Abp.Identity.MongoDB { cancellationToken = GetCancellationToken(cancellationToken); - var organizationUnitIds = await (await GetDbContextAsync(cancellationToken)).OrganizationUnits.AsQueryable() + var organizationUnitIds = await (await GetMongoQueryableAsync(cancellationToken)) .Where(ou => ou.Code.StartsWith(code)) .Select(ou => ou.Id) .ToListAsync(cancellationToken); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index 60c30721cb..b57b3ec388 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -89,10 +89,9 @@ namespace Volo.Abp.Identity.MongoDB CancellationToken cancellationToken = default) { var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - return await ApplyDataFilters, IdentityRole>( - dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)) - ) + + return await (await GetMongoQueryableAsync(cancellationToken)) + .Where(r => roleIds.Contains(r.Id)) .OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting) .As>() .PageBy>(skipCount, maxResultCount) @@ -104,12 +103,8 @@ namespace Volo.Abp.Identity.MongoDB CancellationToken cancellationToken = default) { var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - return await ApplyDataFilters, IdentityRole>( - dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)) - ) - .As>() - .CountAsync(cancellationToken); + + return await (await GetMongoQueryableAsync(cancellationToken)).Where(r => roleIds.Contains(r.Id)).CountAsync(cancellationToken); } public virtual async Task> GetUnaddedRolesAsync( @@ -122,8 +117,8 @@ namespace Volo.Abp.Identity.MongoDB CancellationToken cancellationToken = default) { var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - return await ApplyDataFilters, IdentityRole>(dbContext.Roles.AsQueryable()) + + return await (await GetMongoQueryableAsync(cancellationToken)) .Where(r => !roleIds.Contains(r.Id)) .WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter)) .OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting) @@ -138,8 +133,8 @@ namespace Volo.Abp.Identity.MongoDB CancellationToken cancellationToken = default) { var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); - var dbContext = await GetDbContextAsync(cancellationToken); - return await ApplyDataFilters, IdentityRole>(dbContext.Roles.AsQueryable()) + + return await (await GetMongoQueryableAsync(cancellationToken)) .Where(r => !roleIds.Contains(r.Id)) .WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter)) .As>() @@ -183,8 +178,8 @@ namespace Volo.Abp.Identity.MongoDB bool includeDetails = false, CancellationToken cancellationToken = default) { - var dbContext = await GetDbContextAsync(cancellationToken); - return await ApplyDataFilters, IdentityUser>(dbContext.Users.AsQueryable()) + return await + (await GetMongoQueryableAsync(cancellationToken)) .Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .WhereIf>( !filter.IsNullOrWhiteSpace(), @@ -202,8 +197,7 @@ namespace Volo.Abp.Identity.MongoDB public virtual async Task GetUnaddedUsersCountAsync(OrganizationUnit organizationUnit, string filter = null, CancellationToken cancellationToken = default) { - var dbContext = await GetDbContextAsync(cancellationToken); - return await ApplyDataFilters, IdentityUser>(dbContext.Users.AsQueryable()) + return await (await GetMongoQueryableAsync(cancellationToken)) .Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .WhereIf>( !filter.IsNullOrWhiteSpace(), @@ -224,8 +218,9 @@ namespace Volo.Abp.Identity.MongoDB public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) { + var userQueryable = await GetMongoQueryableAsync(cancellationToken); var dbContext = await GetDbContextAsync(cancellationToken); - var users = await ApplyDataFilters, IdentityUser>(dbContext.Users.AsQueryable()) + var users = await userQueryable .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .As>() .ToListAsync(GetCancellationToken(cancellationToken)); @@ -242,8 +237,7 @@ namespace Volo.Abp.Identity.MongoDB string filter = null, CancellationToken cancellationToken = default) { - var dbContext = await GetDbContextAsync(cancellationToken); - return ApplyDataFilters, IdentityUser>(dbContext.Users.AsQueryable()) + return (await GetMongoQueryableAsync(cancellationToken)) .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .WhereIf>( !filter.IsNullOrWhiteSpace(), @@ -253,23 +247,5 @@ namespace Volo.Abp.Identity.MongoDB (u.PhoneNumber != null && u.PhoneNumber.Contains(filter)) ); } - - //TODO: Should improve! - private TQueryable ApplyDataFilters(TQueryable query) - where TQueryable : IQueryable - { - if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) - { - query = (TQueryable)query.WhereIf(DataFilter.IsEnabled(), e => ((ISoftDelete)e).IsDeleted == false); - } - - if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) - { - var tenantId = CurrentTenant.Id; - query = (TQueryable)query.WhereIf(DataFilter.IsEnabled(), e => ((IMultiTenant)e).TenantId == tenantId); - } - - return query; - } } }