From e6550bbb726ae899b65dd103d56db9e237b8ff78 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 12 Sep 2018 13:57:37 +0300 Subject: [PATCH] Blogging module post delete in app services and repositories --- .../Volo/Blogging/Posts/IPostAppService.cs | 2 ++ .../Volo/Blogging/Posts/PostAppService.cs | 20 ++++++++++++++----- .../Blogging/Comments/ICommentRepository.cs | 2 ++ .../Volo/Blogging/Posts/IPostTagRepository.cs | 5 ++++- .../Volo/Blogging/Tagging/ITagRepository.cs | 2 ++ .../Comments/EfCoreCommentRepository.cs | 6 ++++++ .../Blogging/Posts/EfCorePostTagRepository.cs | 11 +++++++++- .../Blogging/Tagging/EfCoreTagRepository.cs | 10 ++++++++++ 8 files changed, 51 insertions(+), 7 deletions(-) diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs index fc1e536b45..aee0a26a23 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs @@ -13,6 +13,8 @@ namespace Volo.Blogging.Posts Task GetAsync(Guid id); + Task DeleteAsync(Guid id); + Task CreateAsync(CreatePostDto input); Task UpdateAsync(Guid id, UpdatePostDto input); 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 3085a56b90..1629c6e351 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 @@ -41,7 +41,7 @@ namespace Volo.Blogging.Posts foreach (var postDto in postDtos) { - postDto.Tags = await GetTagsOfPost(postDto); + postDto.Tags = await GetTagsOfPost(postDto.Id); postDto.CommentCount = await _commentRepository.GetCommentCountOfPostAsync(postDto.Id); } @@ -63,7 +63,7 @@ namespace Volo.Blogging.Posts var postDto = ObjectMapper.Map(post); - postDto.Tags = await GetTagsOfPost(postDto); + postDto.Tags = await GetTagsOfPost(postDto.Id); return postDto; } @@ -74,11 +74,21 @@ namespace Volo.Blogging.Posts var postDto = ObjectMapper.Map(post); - postDto.Tags = await GetTagsOfPost(postDto); + postDto.Tags = await GetTagsOfPost(postDto.Id); return postDto; } + public async Task DeleteAsync(Guid id) + { + var tags = await GetTagsOfPost(id); + _tagRepository.DecreaseUsageCountOfTags(tags.Select(t=>t.Id).ToList()); + _postTagRepository.DeleteOfPost(id); + _commentRepository.DeleteOfPost(id); + + await _postRepository.DeleteAsync(id); + } + [Authorize(BloggingPermissions.Posts.Update)] public async Task UpdateAsync(Guid id, UpdatePostDto input) { @@ -171,9 +181,9 @@ namespace Volo.Blogging.Posts } } - private async Task> GetTagsOfPost(PostWithDetailsDto postDto) + private async Task> GetTagsOfPost(Guid id) { - var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == postDto.Id); + var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == id); var tags = await _tagRepository.GetListAsync(tagIds.Select(t => t.TagId)); 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 41ee3e0c05..7122e1da0a 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 @@ -14,5 +14,7 @@ namespace Volo.Blogging.Comments Task GetCommentCountOfPostAsync(Guid postId); Task> GetRepliesOfComment(Guid id); + + void DeleteOfPost(Guid id); } } diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostTagRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostTagRepository.cs index d376b5af44..7caa7ef917 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostTagRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostTagRepository.cs @@ -1,8 +1,11 @@ -using Volo.Abp.Domain.Repositories; +using System; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; namespace Volo.Blogging.Posts { public interface IPostTagRepository : IBasicRepository { + void DeleteOfPost(Guid id); } } diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs index 773af4c3d8..538212b37d 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs @@ -12,5 +12,7 @@ namespace Volo.Blogging.Tagging Task GetByNameAsync(string name); Task> GetListAsync(IEnumerable ids); + + void DecreaseUsageCountOfTags(List 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 c463a4fd08..35795ee85f 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 @@ -35,5 +35,11 @@ namespace Volo.Blogging.Comments return await DbSet .Where(a => a.RepliedCommentId == id).ToListAsync(); } + + public void DeleteOfPost(Guid id) + { + var recordsToDelete = DbSet.Where(pt => pt.PostId == id); + DbSet.RemoveRange(recordsToDelete); + } } } diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostTagRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostTagRepository.cs index 66825f55c0..ff6c434d5a 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostTagRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostTagRepository.cs @@ -1,4 +1,7 @@ -using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using System; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; using Volo.Blogging.EntityFrameworkCore; @@ -9,5 +12,11 @@ namespace Volo.Blogging.Posts public EfCorePostTagRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) { } + + public void DeleteOfPost(Guid id) + { + var recordsToDelete = DbSet.Where(pt=>pt.PostId == id); + DbSet.RemoveRange(recordsToDelete); + } } } diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs index eefc721106..5ec949dc51 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs @@ -30,5 +30,15 @@ namespace Volo.Blogging.Tagging { return await DbSet.Where(c => ids.Contains(c.Id)).ToListAsync(); } + + public void DecreaseUsageCountOfTags(List ids) + { + var tags = DbSet.Where(t => ids.Any(id => id == t.Id)); + + foreach (var tag in tags) + { + tag.DecreaseUsageCount(); + } + } } }