|
|
|
@ -19,11 +19,11 @@ namespace Volo.Blogging.Posts |
|
|
|
{ |
|
|
|
protected IBlogUserLookupService UserLookupService { get; } |
|
|
|
|
|
|
|
private readonly IPostRepository _postRepository; |
|
|
|
private readonly ITagRepository _tagRepository; |
|
|
|
private readonly ICommentRepository _commentRepository; |
|
|
|
private readonly IDistributedCache<List<PostCacheItem>> _postsCache; |
|
|
|
private readonly ILocalEventBus _localEventBus; |
|
|
|
protected IPostRepository PostRepository { get; } |
|
|
|
protected ITagRepository TagRepository { get; } |
|
|
|
protected ICommentRepository CommentRepository { get; } |
|
|
|
protected IDistributedCache<List<PostCacheItem>> PostsCache { get; } |
|
|
|
protected ILocalEventBus LocalEventBus { get; } |
|
|
|
|
|
|
|
public PostAppService( |
|
|
|
IPostRepository postRepository, |
|
|
|
@ -35,17 +35,17 @@ namespace Volo.Blogging.Posts |
|
|
|
) |
|
|
|
{ |
|
|
|
UserLookupService = userLookupService; |
|
|
|
_postRepository = postRepository; |
|
|
|
_tagRepository = tagRepository; |
|
|
|
_commentRepository = commentRepository; |
|
|
|
_postsCache = postsCache; |
|
|
|
_localEventBus = localEventBus; |
|
|
|
PostRepository = postRepository; |
|
|
|
TagRepository = tagRepository; |
|
|
|
CommentRepository = commentRepository; |
|
|
|
PostsCache = postsCache; |
|
|
|
LocalEventBus = localEventBus; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<ListResultDto<PostWithDetailsDto>> GetListByBlogIdAndTagNameAsync(Guid id, string tagName) |
|
|
|
{ |
|
|
|
var posts = await _postRepository.GetPostsByBlogId(id); |
|
|
|
var tag = tagName.IsNullOrWhiteSpace() ? null : await _tagRepository.FindByNameAsync(id, tagName); |
|
|
|
var posts = await PostRepository.GetPostsByBlogId(id); |
|
|
|
var tag = tagName.IsNullOrWhiteSpace() ? null : await TagRepository.FindByNameAsync(id, tagName); |
|
|
|
var userDictionary = new Dictionary<Guid, BlogUserDto>(); |
|
|
|
var postDtos = new List<PostWithDetailsDto>(ObjectMapper.Map<List<Post>, List<PostWithDetailsDto>>(posts)); |
|
|
|
|
|
|
|
@ -84,7 +84,7 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
public async Task<ListResultDto<PostWithDetailsDto>> GetTimeOrderedListAsync(Guid blogId) |
|
|
|
{ |
|
|
|
var postCacheItems = await _postsCache.GetOrAddAsync( |
|
|
|
var postCacheItems = await PostsCache.GetOrAddAsync( |
|
|
|
blogId.ToString(), |
|
|
|
async () => await GetTimeOrderedPostsAsync(blogId), |
|
|
|
() => new DistributedCacheEntryOptions |
|
|
|
@ -112,7 +112,7 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
public async Task<PostWithDetailsDto> GetForReadingAsync(GetPostInput input) |
|
|
|
{ |
|
|
|
var post = await _postRepository.GetPostByUrl(input.BlogId, input.Url); |
|
|
|
var post = await PostRepository.GetPostByUrl(input.BlogId, input.Url); |
|
|
|
|
|
|
|
post.IncreaseReadCount(); |
|
|
|
|
|
|
|
@ -132,7 +132,7 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
public async Task<PostWithDetailsDto> GetAsync(Guid id) |
|
|
|
{ |
|
|
|
var post = await _postRepository.GetAsync(id); |
|
|
|
var post = await PostRepository.GetAsync(id); |
|
|
|
|
|
|
|
var postDto = ObjectMapper.Map<Post, PostWithDetailsDto>(post); |
|
|
|
|
|
|
|
@ -151,22 +151,22 @@ namespace Volo.Blogging.Posts |
|
|
|
[Authorize(BloggingPermissions.Posts.Delete)] |
|
|
|
public async Task DeleteAsync(Guid id) |
|
|
|
{ |
|
|
|
var post = await _postRepository.GetAsync(id); |
|
|
|
var post = await PostRepository.GetAsync(id); |
|
|
|
|
|
|
|
await AuthorizationService.CheckAsync(post, CommonOperations.Delete); |
|
|
|
|
|
|
|
var tags = await GetTagsOfPostAsync(id); |
|
|
|
await _tagRepository.DecreaseUsageCountOfTagsAsync(tags.Select(t => t.Id).ToList()); |
|
|
|
await _commentRepository.DeleteOfPost(id); |
|
|
|
await TagRepository.DecreaseUsageCountOfTagsAsync(tags.Select(t => t.Id).ToList()); |
|
|
|
await CommentRepository.DeleteOfPost(id); |
|
|
|
|
|
|
|
await _postRepository.DeleteAsync(id); |
|
|
|
await PostRepository.DeleteAsync(id); |
|
|
|
await PublishPostChangedEventAsync(post.BlogId); |
|
|
|
} |
|
|
|
|
|
|
|
[Authorize(BloggingPermissions.Posts.Update)] |
|
|
|
public async Task<PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input) |
|
|
|
{ |
|
|
|
var post = await _postRepository.GetAsync(id); |
|
|
|
var post = await PostRepository.GetAsync(id); |
|
|
|
|
|
|
|
input.Url = await RenameUrlIfItAlreadyExistAsync(input.BlogId, input.Url, post); |
|
|
|
|
|
|
|
@ -179,7 +179,7 @@ namespace Volo.Blogging.Posts |
|
|
|
post.Description = input.Description; |
|
|
|
post.CoverImage = input.CoverImage; |
|
|
|
|
|
|
|
post = await _postRepository.UpdateAsync(post); |
|
|
|
post = await PostRepository.UpdateAsync(post); |
|
|
|
|
|
|
|
var tagList = SplitTags(input.Tags); |
|
|
|
await SaveTags(tagList, post); |
|
|
|
@ -205,7 +205,7 @@ namespace Volo.Blogging.Posts |
|
|
|
Description = input.Description |
|
|
|
}; |
|
|
|
|
|
|
|
await _postRepository.InsertAsync(post); |
|
|
|
await PostRepository.InsertAsync(post); |
|
|
|
|
|
|
|
var tagList = SplitTags(input.Tags); |
|
|
|
await SaveTags(tagList, post); |
|
|
|
@ -216,14 +216,14 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
private async Task<List<PostCacheItem>> GetTimeOrderedPostsAsync(Guid blogId) |
|
|
|
{ |
|
|
|
var posts = await _postRepository.GetOrderedList(blogId); |
|
|
|
var posts = await PostRepository.GetOrderedList(blogId); |
|
|
|
|
|
|
|
return ObjectMapper.Map<List<Post>, List<PostCacheItem>>(posts); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<string> RenameUrlIfItAlreadyExistAsync(Guid blogId, string url, Post existingPost = null) |
|
|
|
{ |
|
|
|
if (await _postRepository.IsPostUrlInUseAsync(blogId, url, existingPost?.Id)) |
|
|
|
if (await PostRepository.IsPostUrlInUseAsync(blogId, url, existingPost?.Id)) |
|
|
|
{ |
|
|
|
return url + "-" + Guid.NewGuid().ToString().Substring(0, 5); |
|
|
|
} |
|
|
|
@ -242,7 +242,7 @@ namespace Volo.Blogging.Posts |
|
|
|
{ |
|
|
|
foreach (var oldTag in post.Tags.ToList()) |
|
|
|
{ |
|
|
|
var tag = await _tagRepository.GetAsync(oldTag.TagId); |
|
|
|
var tag = await TagRepository.GetAsync(oldTag.TagId); |
|
|
|
|
|
|
|
var oldTagNameInNewTags = newTags.FirstOrDefault(t => t == tag.Name); |
|
|
|
|
|
|
|
@ -251,7 +251,7 @@ namespace Volo.Blogging.Posts |
|
|
|
post.RemoveTag(oldTag.TagId); |
|
|
|
|
|
|
|
tag.DecreaseUsageCount(); |
|
|
|
await _tagRepository.UpdateAsync(tag); |
|
|
|
await TagRepository.UpdateAsync(tag); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -262,7 +262,7 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
private async Task AddNewTags(IEnumerable<string> newTags, Post post) |
|
|
|
{ |
|
|
|
var tags = await _tagRepository.GetListAsync(post.BlogId); |
|
|
|
var tags = await TagRepository.GetListAsync(post.BlogId); |
|
|
|
|
|
|
|
foreach (var newTag in newTags) |
|
|
|
{ |
|
|
|
@ -270,12 +270,12 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
if (tag == null) |
|
|
|
{ |
|
|
|
tag = await _tagRepository.InsertAsync(new Tag(GuidGenerator.Create(), post.BlogId, newTag, 1)); |
|
|
|
tag = await TagRepository.InsertAsync(new Tag(GuidGenerator.Create(), post.BlogId, newTag, 1)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
tag.IncreaseUsageCount(); |
|
|
|
tag = await _tagRepository.UpdateAsync(tag); |
|
|
|
tag = await TagRepository.UpdateAsync(tag); |
|
|
|
} |
|
|
|
|
|
|
|
post.AddTag(tag.Id); |
|
|
|
@ -284,9 +284,9 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
private async Task<List<TagDto>> GetTagsOfPostAsync(Guid id) |
|
|
|
{ |
|
|
|
var tagIds = (await _postRepository.GetAsync(id)).Tags; |
|
|
|
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)); |
|
|
|
|
|
|
|
return ObjectMapper.Map<List<Tag>, List<TagDto>>(tags); |
|
|
|
} |
|
|
|
@ -309,7 +309,7 @@ namespace Volo.Blogging.Posts |
|
|
|
|
|
|
|
private async Task PublishPostChangedEventAsync(Guid blogId) |
|
|
|
{ |
|
|
|
await _localEventBus.PublishAsync( |
|
|
|
await LocalEventBus.PublishAsync( |
|
|
|
new PostChangedEvent |
|
|
|
{ |
|
|
|
BlogId = blogId |
|
|
|
|