blogging module remove PostTagRepository

pull/782/head
Yunus Emre Kalkan 6 years ago
parent 856907053a
commit fac0ec3efd

@ -24,15 +24,13 @@ namespace Volo.Blogging.Posts
private readonly IPostRepository _postRepository; private readonly IPostRepository _postRepository;
private readonly ITagRepository _tagRepository; private readonly ITagRepository _tagRepository;
private readonly IPostTagRepository _postTagRepository;
private readonly ICommentRepository _commentRepository; 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; UserLookupService = userLookupService;
_postRepository = postRepository; _postRepository = postRepository;
_tagRepository = tagRepository; _tagRepository = tagRepository;
_postTagRepository = postTagRepository;
_commentRepository = commentRepository; _commentRepository = commentRepository;
} }
@ -85,10 +83,11 @@ namespace Volo.Blogging.Posts
private async Task<List<PostWithDetailsDto>> FilterPostsByTag(List<PostWithDetailsDto> allPostDtos, Tag tag) private async Task<List<PostWithDetailsDto>> FilterPostsByTag(List<PostWithDetailsDto> allPostDtos, Tag tag)
{ {
var filteredPostDtos = new List<PostWithDetailsDto>(); var filteredPostDtos = new List<PostWithDetailsDto>();
var posts = await _postRepository.GetListAsync();
foreach (var postDto in allPostDtos) foreach (var postDto in allPostDtos)
{ {
if (await _postTagRepository.FindByTagIdAndPostIdAsync(postDto.Id, tag.Id) == null) if (!postDto.Tags.Any(p=> p.Id == tag.Id))
{ {
continue; continue;
} }
@ -145,7 +144,6 @@ namespace Volo.Blogging.Posts
var tags = await GetTagsOfPost(id); var tags = await GetTagsOfPost(id);
_tagRepository.DecreaseUsageCountOfTags(tags.Select(t => t.Id).ToList()); _tagRepository.DecreaseUsageCountOfTags(tags.Select(t => t.Id).ToList());
_postTagRepository.DeleteOfPost(id);
await _commentRepository.DeleteOfPost(id); await _commentRepository.DeleteOfPost(id);
await _postRepository.DeleteAsync(id); await _postRepository.DeleteAsync(id);
@ -210,7 +208,6 @@ namespace Volo.Blogging.Posts
private async Task SaveTags(List<String> newTags, Post post) private async Task SaveTags(List<String> newTags, Post post)
{ {
await RemoveOldTags(newTags, post); await RemoveOldTags(newTags, post);
await AddNewTags(newTags, post); await AddNewTags(newTags, post);
@ -218,9 +215,7 @@ namespace Volo.Blogging.Posts
private async Task RemoveOldTags(List<string> newTags, Post post) private async Task RemoveOldTags(List<string> newTags, Post post)
{ {
var oldTags = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == post.Id).ToList(); foreach (var oldTag in post.Tags)
foreach (var oldTag in oldTags)
{ {
var tag = await _tagRepository.GetAsync(oldTag.TagId); var tag = await _tagRepository.GetAsync(oldTag.TagId);
@ -228,7 +223,7 @@ namespace Volo.Blogging.Posts
if (oldTagNameInNewTags == null) if (oldTagNameInNewTags == null)
{ {
await _postTagRepository.DeleteAsync(oldTag); post.RemoveTag(oldTag.TagId);
tag.DecreaseUsageCount(); tag.DecreaseUsageCount();
await _tagRepository.UpdateAsync(tag); await _tagRepository.UpdateAsync(tag);
@ -256,15 +251,14 @@ namespace Volo.Blogging.Posts
{ {
tag.IncreaseUsageCount(); tag.IncreaseUsageCount();
tag = await _tagRepository.UpdateAsync(tag); tag = await _tagRepository.UpdateAsync(tag);
post.AddTag(tag.Id);
} }
await _postTagRepository.InsertAsync(new PostTag(post.Id, tag.Id));
} }
} }
private async Task<List<TagDto>> GetTagsOfPost(Guid id) private async Task<List<TagDto>> 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)); var tags = await _tagRepository.GetListAsync(tagIds.Select(t => t.TagId));

@ -1,13 +0,0 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
namespace Volo.Blogging.Posts
{
public interface IPostTagRepository : IBasicRepository<PostTag>
{
void DeleteOfPost(Guid id);
Task<PostTag> FindByTagIdAndPostIdAsync(Guid postId, Guid tagId);
}
}

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp; using Volo.Abp;
@ -61,5 +62,15 @@ namespace Volo.Blogging.Posts
Url = Check.NotNullOrWhiteSpace(url, nameof(url)); Url = Check.NotNullOrWhiteSpace(url, nameof(url));
return this; 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);
}
} }
} }

@ -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<IBloggingDbContext, PostTag>, IPostTagRepository
{
public EfCorePostTagRepository(IDbContextProvider<IBloggingDbContext> dbContextProvider) : base(dbContextProvider)
{
}
public void DeleteOfPost(Guid id)
{
var recordsToDelete = DbSet.Where(pt=>pt.PostId == id);
DbSet.RemoveRange(recordsToDelete);
}
public async Task<PostTag> FindByTagIdAndPostIdAsync(Guid postId, Guid tagId)
{
return await DbSet.FirstOrDefaultAsync(pt=> pt.PostId == postId && pt.TagId == tagId);
}
}
}

@ -24,8 +24,6 @@ namespace Volo.Blogging.MongoDB
public IMongoCollection<Tagging.Tag> Tags => Collection<Tagging.Tag>(); public IMongoCollection<Tagging.Tag> Tags => Collection<Tagging.Tag>();
public IMongoCollection<PostTag> PostTags => Collection<PostTag>();
public IMongoCollection<Comment> Comments => Collection<Comment>(); public IMongoCollection<Comment> Comments => Collection<Comment>();
protected override void CreateModel(IMongoModelBuilder modelBuilder) protected override void CreateModel(IMongoModelBuilder modelBuilder)

@ -42,11 +42,6 @@ namespace Volo.Blogging.MongoDB
b.CollectionName = options.CollectionPrefix + "Tags"; b.CollectionName = options.CollectionPrefix + "Tags";
}); });
builder.Entity<PostTag>(b =>
{
b.CollectionName = options.CollectionPrefix + "PostTags";
});
builder.Entity<Comment>(b => builder.Entity<Comment>(b =>
{ {
b.CollectionName = options.CollectionPrefix + "Comments"; b.CollectionName = options.CollectionPrefix + "Comments";

@ -23,7 +23,6 @@ namespace Volo.Blogging.MongoDB
options.AddRepository<BlogUser, MongoBlogUserRepository>(); options.AddRepository<BlogUser, MongoBlogUserRepository>();
options.AddRepository<Post, MongoPostRepository>(); options.AddRepository<Post, MongoPostRepository>();
options.AddRepository<Tag, MongoTagRepository>(); options.AddRepository<Tag, MongoTagRepository>();
options.AddRepository<PostTag, MongoPostTagRepository>();
options.AddRepository<Comment, MongoCommentRepository>(); options.AddRepository<Comment, MongoCommentRepository>();
}); });
} }

@ -19,8 +19,6 @@ namespace Volo.Blogging.MongoDB
IMongoCollection<Tagging.Tag> Tags { get; } IMongoCollection<Tagging.Tag> Tags { get; }
IMongoCollection<PostTag> PostTags { get; }
IMongoCollection<Comment> Comments { get; } IMongoCollection<Comment> Comments { get; }
} }

Loading…
Cancel
Save