diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs index 9c55f9e265..21077a4912 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs @@ -11,7 +11,6 @@ namespace Volo.Blogging.Posts [StringLength(PostConsts.MaxTitleLength)] public string Title { get; set; } - [Required] [StringLength(PostConsts.MaxUrlLength)] public string Url { get; set; } 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 baf14fa8f7..3e3089b88d 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 @@ -43,7 +43,7 @@ namespace Volo.Blogging.Posts { postDto.Tags = await GetTagsOfPost(postDto); - postDto.CommentCount = (await _commentRepository.GetListAsync()).Count(p => p.PostId == postDto.Id); + postDto.CommentCount = await _commentRepository.GetCommentCountOfPostAsync(postDto.Id); } if (!tagName.IsNullOrWhiteSpace()) @@ -116,19 +116,39 @@ namespace Volo.Blogging.Posts } private async Task SaveTags(List newTags, Post post) + { + + await RemoveOldTags(newTags, post); + + await AddNewTags(newTags, post); + } + + private async Task RemoveOldTags(List newTags, Post post) { var oldTags = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == post.Id).ToList(); foreach (var oldTag in oldTags) { - await _postTagRepository.DeleteAsync(oldTag); - var tag = await _tagRepository.GetAsync(oldTag.TagId); - tag.DecreaseUsageCount(); - await _tagRepository.UpdateAsync(tag); + var oldTagNameInNewTags = newTags.FirstOrDefault(t => t == tag.Name); + + if (oldTagNameInNewTags == null) + { + await _postTagRepository.DeleteAsync(oldTag); + + tag.DecreaseUsageCount(); + await _tagRepository.UpdateAsync(tag); + } + else + { + newTags.Remove(oldTagNameInNewTags); + } } + } + private async Task AddNewTags(List newTags, Post post) + { var tags = await _tagRepository.GetListAsync(); foreach (var newTag in newTags) @@ -147,7 +167,6 @@ namespace Volo.Blogging.Posts await _postTagRepository.InsertAsync(new PostTag(post.Id, tag.Id)); } - } private async Task> GetTagsOfPost(PostWithDetailsDto postDto) diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs index fb91a7816e..41ee3e0c05 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs @@ -11,6 +11,8 @@ namespace Volo.Blogging.Comments Guid postId ); + Task GetCommentCountOfPostAsync(Guid postId); + Task> GetRepliesOfComment(Guid id); } } diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs index 3713d0464d..c463a4fd08 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs @@ -24,6 +24,12 @@ namespace Volo.Blogging.Comments .ToListAsync(); } + public async Task GetCommentCountOfPostAsync(Guid postId) + { + return await DbSet + .CountAsync(a => a.PostId == postId); + } + public async Task> GetRepliesOfComment(Guid id) { return await DbSet diff --git a/modules/blogging/src/Volo.Blogging.Web/Areas/Blog/Controllers/CommentsController.cs b/modules/blogging/src/Volo.Blogging.Web/Areas/Blog/Controllers/CommentsController.cs index 4c8176351d..89ed0f1968 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Areas/Blog/Controllers/CommentsController.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Areas/Blog/Controllers/CommentsController.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;