From fac0ec3efdeb0da9ae1e8b7988e127788b074b75 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 7 Feb 2019 16:23:29 +0300 Subject: [PATCH] blogging module remove PostTagRepository --- .../Volo/Blogging/Posts/PostAppService.cs | 20 +++++-------- .../Volo/Blogging/Posts/IPostTagRepository.cs | 13 --------- .../Volo/Blogging/Posts/Post.cs | 11 ++++++++ .../Blogging/Posts/EfCorePostTagRepository.cs | 28 ------------------- .../MongoDB/BloggingMongoDbContext.cs | 2 -- .../BloggingMongoDbContextExtensions.cs | 5 ---- .../Blogging/MongoDB/BloggingMongoDbModule.cs | 1 - .../MongoDB/IBloggingMongoDbContext.cs | 2 -- 8 files changed, 18 insertions(+), 64 deletions(-) delete mode 100644 modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostTagRepository.cs delete mode 100644 modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostTagRepository.cs 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 5bf5c6decb..a88fd46679 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 @@ -24,15 +24,13 @@ namespace Volo.Blogging.Posts private readonly IPostRepository _postRepository; private readonly ITagRepository _tagRepository; - private readonly IPostTagRepository _postTagRepository; private readonly ICommentRepository _commentRepository; - public PostAppService(IPostRepository postRepository, ITagRepository tagRepository, IPostTagRepository postTagRepository, ICommentRepository commentRepository, IBlogUserLookupService userLookupService) + public PostAppService(IPostRepository postRepository, ITagRepository tagRepository, ICommentRepository commentRepository, IBlogUserLookupService userLookupService) { UserLookupService = userLookupService; _postRepository = postRepository; _tagRepository = tagRepository; - _postTagRepository = postTagRepository; _commentRepository = commentRepository; } @@ -85,10 +83,11 @@ namespace Volo.Blogging.Posts private async Task> FilterPostsByTag(List allPostDtos, Tag tag) { var filteredPostDtos = new List(); + var posts = await _postRepository.GetListAsync(); foreach (var postDto in allPostDtos) { - if (await _postTagRepository.FindByTagIdAndPostIdAsync(postDto.Id, tag.Id) == null) + if (!postDto.Tags.Any(p=> p.Id == tag.Id)) { continue; } @@ -145,7 +144,6 @@ namespace Volo.Blogging.Posts var tags = await GetTagsOfPost(id); _tagRepository.DecreaseUsageCountOfTags(tags.Select(t => t.Id).ToList()); - _postTagRepository.DeleteOfPost(id); await _commentRepository.DeleteOfPost(id); await _postRepository.DeleteAsync(id); @@ -210,7 +208,6 @@ namespace Volo.Blogging.Posts private async Task SaveTags(List newTags, Post post) { - await RemoveOldTags(newTags, post); await AddNewTags(newTags, post); @@ -218,9 +215,7 @@ namespace Volo.Blogging.Posts 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) + foreach (var oldTag in post.Tags) { var tag = await _tagRepository.GetAsync(oldTag.TagId); @@ -228,7 +223,7 @@ namespace Volo.Blogging.Posts if (oldTagNameInNewTags == null) { - await _postTagRepository.DeleteAsync(oldTag); + post.RemoveTag(oldTag.TagId); tag.DecreaseUsageCount(); await _tagRepository.UpdateAsync(tag); @@ -256,15 +251,14 @@ namespace Volo.Blogging.Posts { tag.IncreaseUsageCount(); tag = await _tagRepository.UpdateAsync(tag); + post.AddTag(tag.Id); } - - await _postTagRepository.InsertAsync(new PostTag(post.Id, tag.Id)); } } private async Task> GetTagsOfPost(Guid id) { - var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == id); + var tagIds = (await _postRepository.GetAsync(id)).Tags; var tags = await _tagRepository.GetListAsync(tagIds.Select(t => t.TagId)); 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 deleted file mode 100644 index f552424e93..0000000000 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostTagRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Threading.Tasks; -using Volo.Abp.Domain.Repositories; - -namespace Volo.Blogging.Posts -{ - public interface IPostTagRepository : IBasicRepository - { - void DeleteOfPost(Guid id); - - Task FindByTagIdAndPostIdAsync(Guid postId, Guid tagId); - } -} diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/Post.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/Post.cs index baabb876cb..8e4a898ce4 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/Post.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/Post.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using JetBrains.Annotations; using Volo.Abp; @@ -61,5 +62,15 @@ namespace Volo.Blogging.Posts Url = Check.NotNullOrWhiteSpace(url, nameof(url)); return this; } + + public virtual void AddTag(Guid tagId) + { + Tags.Add(new PostTag(Id,tagId)); + } + + public virtual void RemoveTag(Guid tagId) + { + Tags.RemoveAll(t => t.TagId == tagId); + } } } 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 deleted file mode 100644 index e95fdeb6ca..0000000000 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostTagRepository.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Volo.Abp.Domain.Repositories.EntityFrameworkCore; -using Volo.Abp.EntityFrameworkCore; -using Volo.Blogging.EntityFrameworkCore; - -namespace Volo.Blogging.Posts -{ - public class EfCorePostTagRepository : EfCoreRepository, IPostTagRepository - { - public EfCorePostTagRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) - { - } - - public void DeleteOfPost(Guid id) - { - var recordsToDelete = DbSet.Where(pt=>pt.PostId == id); - DbSet.RemoveRange(recordsToDelete); - } - - public async Task FindByTagIdAndPostIdAsync(Guid postId, Guid tagId) - { - return await DbSet.FirstOrDefaultAsync(pt=> pt.PostId == postId && pt.TagId == tagId); - } - } -} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs index cf57e77b57..2394097cfa 100644 --- a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs @@ -24,8 +24,6 @@ namespace Volo.Blogging.MongoDB public IMongoCollection Tags => Collection(); - public IMongoCollection PostTags => Collection(); - public IMongoCollection Comments => Collection(); protected override void CreateModel(IMongoModelBuilder modelBuilder) diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs index 8afbdf12f8..bb6bf8775e 100644 --- a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs @@ -42,11 +42,6 @@ namespace Volo.Blogging.MongoDB b.CollectionName = options.CollectionPrefix + "Tags"; }); - builder.Entity(b => - { - b.CollectionName = options.CollectionPrefix + "PostTags"; - }); - builder.Entity(b => { b.CollectionName = options.CollectionPrefix + "Comments"; diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs index 1991c4d958..a78ad43559 100644 --- a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs @@ -23,7 +23,6 @@ namespace Volo.Blogging.MongoDB options.AddRepository(); options.AddRepository(); options.AddRepository(); - options.AddRepository(); options.AddRepository(); }); } diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs index 4e5c782c98..61d1af21ca 100644 --- a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs @@ -19,8 +19,6 @@ namespace Volo.Blogging.MongoDB IMongoCollection Tags { get; } - IMongoCollection PostTags { get; } - IMongoCollection Comments { get; } }