diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Comments/EfCoreCommentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Comments/EfCoreCommentRepository.cs index 8c8e777185..8ca73426be 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Comments/EfCoreCommentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Comments/EfCoreCommentRepository.cs @@ -22,7 +22,8 @@ namespace Volo.CmsKit.Comments { } - public async Task GetWithAuthorAsync(Guid id, CancellationToken cancellationToken = default) + public async Task GetWithAuthorAsync(Guid id, + CancellationToken cancellationToken = default) { var query = from comment in (await GetDbSetAsync()) join user in (await GetDbContextAsync()).Set() on comment.CreatorId equals user.Id @@ -44,60 +45,64 @@ namespace Volo.CmsKit.Comments } public async Task> GetListAsync( - string filter = null, - string entityType = null, - string entityId = null, + string filter = null, + string entityType = null, + string entityId = 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, - entityType, - entityId, + filter, + entityType, + entityId, repliedCommentId, authorUsername, - creationStartDate, - creationEndDate); + creationStartDate, + creationEndDate, + token); if (sorting != null) { sorting = "comment." + sorting; } - + query = query.OrderBy(sorting ?? "comment.creationTime desc") - .PageBy(skipCount, maxResultCount); - - return await query.ToListAsync(GetCancellationToken(cancellationToken)); + .PageBy(skipCount, maxResultCount); + + return await query.ToListAsync(token); } public async Task GetCountAsync( - string text = null, - string entityType = null, + string text = null, + string entityType = null, string entityId = null, - Guid? repliedCommentId = null, + Guid? repliedCommentId = null, string authorUsername = null, DateTime? creationStartDate = null, - DateTime? creationEndDate = null, + DateTime? creationEndDate = null, CancellationToken cancellationToken = default - ) + ) { + var token = GetCancellationToken(cancellationToken); var query = await GetListQueryAsync( - text, - entityType, - entityId, - repliedCommentId, + text, + entityType, + entityId, + repliedCommentId, authorUsername, - creationStartDate, - creationEndDate); + creationStartDate, + creationEndDate, + token); - return await query.LongCountAsync(GetCancellationToken(cancellationToken)); + return await query.LongCountAsync(token); } public async Task> GetListWithAuthorsAsync( @@ -141,39 +146,35 @@ namespace Volo.CmsKit.Comments } protected virtual async Task> GetListQueryAsync( - string filter = null, - string entityType = null, + string filter = null, + string entityType = null, string entityId = null, - Guid? repliedCommentId = null, + Guid? repliedCommentId = null, string authorUsername = null, DateTime? creationStartDate = null, - DateTime? creationEndDate = null - ) + DateTime? creationEndDate = null, + CancellationToken cancellationToken = default + ) { - var query = from comment in (await GetDbSetAsync()) - join user in (await GetDbContextAsync()).Set() + var commentDbSet = await GetDbSetAsync(); + var cmsUserSet = (await GetDbContextAsync()).Set(); + + var query = from comment in commentDbSet + join user in cmsUserSet on comment.CreatorId equals user.Id select new CommentWithAuthorQueryResultItem { Comment = comment, Author = user }; - - if (!string.IsNullOrWhiteSpace(authorUsername)) - { - var author = await (await GetDbContextAsync()).Set().FirstOrDefaultAsync(x => x.UserName == authorUsername); - - var authorId = author?.Id ?? Guid.Empty; - query = query.Where(x => x.Comment.CreatorId == authorId); - } - return query.WhereIf(!filter.IsNullOrWhiteSpace(), c => c.Comment.Text.Contains(filter)) .WhereIf(!entityType.IsNullOrWhiteSpace(), c => c.Comment.EntityType.Contains(entityType)) .WhereIf(!entityId.IsNullOrWhiteSpace(), c => c.Comment.EntityId.Contains(entityId)) .WhereIf(repliedCommentId.HasValue, c => c.Comment.RepliedCommentId == repliedCommentId) + .WhereIf(!authorUsername.IsNullOrWhiteSpace(),c=>c.Author.UserName.Contains(authorUsername)) .WhereIf(creationStartDate.HasValue, c => c.Comment.CreationTime >= creationStartDate) .WhereIf(creationEndDate.HasValue, c => c.Comment.CreationTime <= creationEndDate); } } -} +} \ 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 c58b550305..6c3158b0e5 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 @@ -56,6 +56,7 @@ namespace Volo.CmsKit.MongoDB.Comments CancellationToken cancellationToken = default ) { + var token = GetCancellationToken(cancellationToken); var query = await GetListQueryAsync( filter, entityType, @@ -64,22 +65,22 @@ namespace Volo.CmsKit.MongoDB.Comments authorUsername, creationStartDate, creationEndDate, - cancellationToken); + token); var comments = await query.OrderBy(sorting ?? "creationTime desc") .As>() .PageBy>(skipCount, maxResultCount) - .ToListAsync(GetCancellationToken(cancellationToken)); + .ToListAsync(token); var commentIds = comments.Select(x => x.Id).ToList(); - var authorsQuery = from comment in (await GetMongoQueryableAsync(cancellationToken)) - join user in (await GetDbContextAsync(cancellationToken)).CmsUsers on comment.CreatorId equals user.Id + 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(cancellationToken); + var authors = await authorsQuery.ToListAsync(token); return comments .Select(