From ce3b8178e50058bdaa255527b192eeed109deafb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 15 Aug 2020 14:50:30 +0300 Subject: [PATCH] Refactor and add notes. --- .../Controllers/CmsKitControllerBase.cs | 4 +-- .../Comments/CommentPublicAppService.cs | 33 +++++++++++-------- .../Public/CmsKitPublicControllerBase.cs | 1 + .../Comments/CommentPublicController.cs | 1 + 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/CmsKitControllerBase.cs b/modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/CmsKitControllerBase.cs index 8938109b3b..21a546195a 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/CmsKitControllerBase.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Common.HttpApi/Volo/CmsKit/Controllers/CmsKitControllerBase.cs @@ -3,9 +3,9 @@ using Volo.CmsKit.Localization; namespace Volo.CmsKit.Controllers { - public class CmsKitControllerBase : AbpController + public abstract class CmsKitControllerBase : AbpController { - public CmsKitControllerBase() + protected CmsKitControllerBase() { LocalizationResource = typeof(CmsKitResource); } 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 576ff14b27..07a6a22979 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 @@ -31,11 +31,12 @@ namespace Volo.CmsKit.Public.Comments public virtual async Task> GetListAsync(string entityType, string entityId) { - var commentsWithAuthor = await CommentRepository.GetListWithAuthorsAsync(entityType, entityId); + var commentsWithAuthor = await CommentRepository + .GetListWithAuthorsAsync(entityType, entityId); return new ListResultDto( ConvertCommentsToNestedStructure(commentsWithAuthor) - ); + ); } [Authorize] @@ -45,18 +46,21 @@ namespace Volo.CmsKit.Public.Comments if (user == null) { + //TODO: Localize throw new BusinessException(message: "User Not found!"); } - var comment = await CommentRepository.InsertAsync(new Comment( - GuidGenerator.Create(), - input.EntityType, - input.EntityId, - input.Text, - input.RepliedCommentId, - user.Id, - CurrentTenant.Id - )); + var comment = await CommentRepository.InsertAsync( + new Comment( + GuidGenerator.Create(), + input.EntityType, + input.EntityId, + input.Text, + input.RepliedCommentId, + user.Id, + CurrentTenant.Id + ) + ); return ObjectMapper.Map(comment); } @@ -68,7 +72,7 @@ namespace Volo.CmsKit.Public.Comments if (comment.CreatorId != CurrentUser.GetId()) { - throw new BusinessException(); + throw new BusinessException(); //TODO: AbpAuthorizationException! } comment.SetText(input.Text); @@ -82,9 +86,10 @@ namespace Volo.CmsKit.Public.Comments public virtual async Task DeleteAsync(Guid id) { var comment = await CommentRepository.GetAsync(id); + if (comment.CreatorId != CurrentUser.GetId()) { - throw new BusinessException(); + throw new BusinessException(); //TODO: AbpAuthorizationException! } await CommentRepository.DeleteWithRepliesAsync(comment); @@ -92,6 +97,8 @@ namespace Volo.CmsKit.Public.Comments private List ConvertCommentsToNestedStructure(List comments) { + //TODO: I think this method can be optimized if you use dictionaries instead of straight search + var parentComments = comments .Where(c=> c.Comment.RepliedCommentId == null) .Select(c=> ObjectMapper.Map(c.Comment)) diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/CmsKitPublicControllerBase.cs b/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/CmsKitPublicControllerBase.cs index 79bb1b63ba..49aa98c134 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/CmsKitPublicControllerBase.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/CmsKitPublicControllerBase.cs @@ -4,5 +4,6 @@ namespace Volo.CmsKit.Public { public abstract class CmsKitPublicControllerBase : CmsKitControllerBase { + } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Comments/CommentPublicController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Comments/CommentPublicController.cs index 7611c3bc7f..d2dfbc83f4 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Comments/CommentPublicController.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Comments/CommentPublicController.cs @@ -38,6 +38,7 @@ namespace Volo.CmsKit.Public.Comments return CommentPublicAppService.UpdateAsync(id, input); } + //TODO: Route seems incorrect! Should be "{id}" ? [HttpDelete] [Route("update")] public Task DeleteAsync(Guid id)