blog module refactoring

pull/441/head
Yunus Emre Kalkan 6 years ago
parent 3292847bde
commit adc61ab0b0

@ -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; }

@ -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<String> newTags, Post post)
{
await RemoveOldTags(newTags, post);
await AddNewTags(newTags, 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 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<string> 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<List<TagDto>> GetTagsOfPost(PostWithDetailsDto postDto)

@ -11,6 +11,8 @@ namespace Volo.Blogging.Comments
Guid postId
);
Task<int> GetCommentCountOfPostAsync(Guid postId);
Task<List<Comment>> GetRepliesOfComment(Guid id);
}
}

@ -24,6 +24,12 @@ namespace Volo.Blogging.Comments
.ToListAsync();
}
public async Task<int> GetCommentCountOfPostAsync(Guid postId)
{
return await DbSet
.CountAsync(a => a.PostId == postId);
}
public async Task<List<Comment>> GetRepliesOfComment(Guid id)
{
return await DbSet

@ -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;

Loading…
Cancel
Save