From e6a224db73cbf4d23ec0ac1b9ad994e30092d44c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Aug 2020 16:42:57 +0300 Subject: [PATCH] Refactor the domain layer of the CMS Kit. --- .../Volo/CmsKit/Comments/Comment.cs | 7 ++++++- ...r.cs => CommentWithAuthorQueryResultItem.cs} | 2 +- .../Volo/CmsKit/Comments/ICommentRepository.cs | 5 +++-- .../Reactions/DefaultReactionDefinitionStore.cs | 2 +- .../CmsKit/Reactions/IUserReactionRepository.cs | 9 ++++++--- .../Volo/CmsKit/Users/CmsUserLookupService.cs | 1 - .../CmsKit/Comments/EfCoreCommentRepository.cs | 4 ++-- .../MongoDB/Comments/MongoCommentRepository.cs | 4 ++-- .../Public/Comments/CommentPublicAppService.cs | 4 ++-- .../Reactions/ReactionPublicAppService.cs | 17 +++++++++-------- .../Reactions/ReactionPublicAppService_Tests.cs | 1 + 11 files changed, 33 insertions(+), 23 deletions(-) rename modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/{CommentWithAuthor.cs => CommentWithAuthorQueryResultItem.cs} (76%) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/Comment.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/Comment.cs index 06e22aa919..dc821eb3d3 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/Comment.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/Comment.cs @@ -44,10 +44,15 @@ namespace Volo.CmsKit.Comments CreatorId = creatorId; TenantId = tenantId; - SetText(text); + SetTextInternal(text); } public virtual void SetText(string text) + { + SetTextInternal(text); + } + + protected virtual void SetTextInternal(string text) { Text = Check.NotNullOrWhiteSpace(text, nameof(text), CommentConsts.MaxTextLength); } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/CommentWithAuthor.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/CommentWithAuthorQueryResultItem.cs similarity index 76% rename from modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/CommentWithAuthor.cs rename to modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/CommentWithAuthorQueryResultItem.cs index 3a04a31ee8..a572ec1017 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/CommentWithAuthor.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/CommentWithAuthorQueryResultItem.cs @@ -2,7 +2,7 @@ namespace Volo.CmsKit.Comments { - public class CommentWithAuthor + public class CommentWithAuthorQueryResultItem { public Comment Comment { get; set; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/ICommentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/ICommentRepository.cs index 8e5bba3427..8eb1f55f6b 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/ICommentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Comments/ICommentRepository.cs @@ -8,8 +8,9 @@ namespace Volo.CmsKit.Comments { public interface ICommentRepository : IBasicRepository { - Task> GetListWithAuthorsAsync( + Task> GetListWithAuthorsAsync( [NotNull] string entityType, - [NotNull] string entityId); + [NotNull] string entityId + ); } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/DefaultReactionDefinitionStore.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/DefaultReactionDefinitionStore.cs index 4c7d4e095c..84cb15471d 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/DefaultReactionDefinitionStore.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/DefaultReactionDefinitionStore.cs @@ -20,7 +20,7 @@ namespace Volo.CmsKit.Reactions return Task.FromResult(Options.Reactions.Values.ToList()); } - public Task GetReactionOrNullAsync(string reactionName, string entityType = null) + public virtual Task GetReactionOrNullAsync(string reactionName, string entityType = null) { return Task.FromResult(Options.Reactions.GetOrDefault(reactionName)); } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IUserReactionRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IUserReactionRepository.cs index 2c964c70f5..ae9398e2d0 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IUserReactionRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IUserReactionRepository.cs @@ -12,15 +12,18 @@ namespace Volo.CmsKit.Reactions Guid userId, [NotNull] string entityType, [NotNull] string entityId, - [NotNull] string reactionName); + [NotNull] string reactionName + ); Task> GetListForUserAsync( Guid userId, [NotNull] string entityType, - [NotNull] string entityId); + [NotNull] string entityId + ); Task> GetSummariesAsync( [NotNull] string entityType, - [NotNull] string entityId); + [NotNull] string entityId + ); } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserLookupService.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserLookupService.cs index 0771140a10..126d2b9c60 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserLookupService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserLookupService.cs @@ -19,6 +19,5 @@ namespace Volo.CmsKit.Users { return new CmsUser(externalUser); } - } } 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 3c39386f03..00b3150219 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 @@ -19,7 +19,7 @@ namespace Volo.CmsKit.Comments { } - public async Task> GetListWithAuthorsAsync( + public async Task> GetListWithAuthorsAsync( string entityType, string entityId) { @@ -30,7 +30,7 @@ namespace Volo.CmsKit.Comments join user in DbContext.CmsUsers on comment.CreatorId equals user.Id where entityType == comment.EntityType && entityId == comment.EntityId orderby comment.CreationTime - select new CommentWithAuthor + select new CommentWithAuthorQueryResultItem { Comment = comment, Author = user 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 26a7c09a8c..3711831a46 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 @@ -18,7 +18,7 @@ namespace Volo.CmsKit.MongoDB.Comments { } - public async Task> GetListWithAuthorsAsync( + public async Task> GetListWithAuthorsAsync( string entityType, string entityId) { @@ -40,7 +40,7 @@ namespace Volo.CmsKit.MongoDB.Comments return comments .Select( comment => - new CommentWithAuthor + new CommentWithAuthorQueryResultItem { Comment = comment, Author = authors.FirstOrDefault(a => a.Id == comment.CreatorId) diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs index be3e260c5b..01e172de7c 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs @@ -93,7 +93,7 @@ namespace Volo.CmsKit.Public.Comments await CommentRepository.DeleteAsync(id); } - private List ConvertCommentsToNestedStructure(List comments) + private List ConvertCommentsToNestedStructure(List comments) { var parentComments = comments .Where(c=> c.Comment.RepliedCommentId == null) @@ -118,7 +118,7 @@ namespace Volo.CmsKit.Public.Comments return parentComments; } - private CmsUserDto GetAuthorAsDtoFromCommentList(List comments, Guid commentId) + private CmsUserDto GetAuthorAsDtoFromCommentList(List comments, Guid commentId) { return ObjectMapper.Map(comments.Single(c => c.Comment.Id == commentId).Author); } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Reactions/ReactionPublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Reactions/ReactionPublicAppService.cs index 6a1431f065..27fdd170ab 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Reactions/ReactionPublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Reactions/ReactionPublicAppService.cs @@ -31,13 +31,14 @@ namespace Volo.CmsKit.Public.Reactions { var summaries = await ReactionManager.GetSummariesAsync(entityType, entityId); - var userReactions = CurrentUser.IsAuthenticated ? - (await UserReactionRepository - .GetListForUserAsync( - CurrentUser.GetId(), - entityType, - entityId - )).ToDictionary(x => x.ReactionName, x => x) : null; + var userReactionsOrNull = CurrentUser.IsAuthenticated + ? (await UserReactionRepository + .GetListForUserAsync( + CurrentUser.GetId(), + entityType, + entityId + )).ToDictionary(x => x.ReactionName, x => x) + : null; var reactionWithSelectionDtos = new List(); @@ -48,7 +49,7 @@ namespace Volo.CmsKit.Public.Reactions { Reaction = ConvertToReactionDto(summary.Reaction), Count = summary.Count, - IsSelectedByCurrentUser = userReactions?.ContainsKey(summary.Reaction.Name) ?? false + IsSelectedByCurrentUser = userReactionsOrNull?.ContainsKey(summary.Reaction.Name) ?? false } ); } diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs index d03712ea07..dd74a1f1e7 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs @@ -43,6 +43,7 @@ namespace Volo.CmsKit.Reactions reactions.Items. First(r=>r.Reaction.Name == StandardReactions.Rocket).IsSelectedByCurrentUser.ShouldBeTrue(); + reactions.Items. First(r=>r.Reaction.Name == StandardReactions.Rocket).Count.ShouldBe(1);