From 5fa097400e72eef685573ec7e9dfa4b396df4fed Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Sep 2018 13:51:49 +0300 Subject: [PATCH] Blogging module url duplication prevented --- .../Volo/Blogging/Posts/PostAppService.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs index c2a5a403eb..e479cfd0d7 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs @@ -39,7 +39,7 @@ namespace Volo.Blogging.Posts public async Task> GetListByBlogIdAndTagName(Guid id, string tagName) { var posts = _postRepository.GetPostsByBlogId(id); - var tag = tagName.IsNullOrWhiteSpace()? null: await _tagRepository.GetByNameAsync(tagName); + var tag = tagName.IsNullOrWhiteSpace() ? null : await _tagRepository.GetByNameAsync(tagName); var userDictionary = new Dictionary(); var postDtos = new List(ObjectMapper.Map, List>(posts)); @@ -68,7 +68,7 @@ namespace Volo.Blogging.Posts foreach (var postDto in postDtos) { - if (postDto.CreatorId.HasValue && userDictionary.ContainsKey((Guid) postDto.CreatorId)) + if (postDto.CreatorId.HasValue && userDictionary.ContainsKey((Guid)postDto.CreatorId)) { postDto.Writer = userDictionary[(Guid)postDto.CreatorId]; } @@ -139,7 +139,7 @@ namespace Volo.Blogging.Posts await AuthorizationService.CheckAsync(post, CommonOperations.Delete); var tags = await GetTagsOfPost(id); - _tagRepository.DecreaseUsageCountOfTags(tags.Select(t=>t.Id).ToList()); + _tagRepository.DecreaseUsageCountOfTags(tags.Select(t => t.Id).ToList()); _postTagRepository.DeleteOfPost(id); _commentRepository.DeleteOfPost(id); @@ -151,6 +151,8 @@ namespace Volo.Blogging.Posts { var post = await _postRepository.GetAsync(id); + input.Url = await RenameUrlIfItAlreadyExistAsync(input.BlogId, input.Url, post); + await AuthorizationService.CheckAsync(post, CommonOperations.Update); post.SetTitle(input.Title); @@ -169,6 +171,8 @@ namespace Volo.Blogging.Posts [Authorize(BloggingPermissions.Posts.Create)] public async Task CreateAsync(CreatePostDto input) { + input.Url = await RenameUrlIfItAlreadyExistAsync(input.BlogId, input.Url); + var post = new Post( id: GuidGenerator.Create(), blogId: input.BlogId, @@ -176,7 +180,8 @@ namespace Volo.Blogging.Posts title: input.Title, coverImage: input.CoverImage, url: input.Url - ) {Content = input.Content}; + ) + { Content = input.Content }; await _postRepository.InsertAsync(post); @@ -186,6 +191,18 @@ namespace Volo.Blogging.Posts return ObjectMapper.Map(post); } + private async Task RenameUrlIfItAlreadyExistAsync(Guid blogId, string url, Post existingPost = null) + { + var postList = await _postRepository.GetListAsync(); + + if (postList.Where(p => p.Url == url).WhereIf(existingPost != null, p => existingPost.Id != p.Id).Any()) + { + return url + "-" + Guid.NewGuid().ToString().Substring(0, 5); + } + + return url; + } + private async Task SaveTags(List newTags, Post post) { @@ -255,7 +272,7 @@ namespace Volo.Blogging.Posts { return new List(); } - return new List(tags.Split(",").Select(t=>t.Trim())); + return new List(tags.Split(",").Select(t => t.Trim())); } } }