public async Task <PostWithDetailsDto> GetByUrlAsync(GetPostInput input) { var post = await _postRepository.GetPostByUrl(input.BlogId, input.Url); var postDto = ObjectMapper.Map <Post, PostWithDetailsDto>(post); var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == postDto.Id); var tags = await _tagRepository.GetListAsync(tagIds.Select(t => t.TagId)); postDto.Tags = ObjectMapper.Map <List <Tag>, List <TagDto> >(tags); return(postDto); }
public async Task <List <PopularTagDto> > GetPopularTags(GetPopularTagsInput input) { var postTags = await _postTagRepository.GetListAsync(); var postTagsGrouped = postTags.GroupBy(x => x.PostId) .OrderByDescending(g => g.Count()) .SelectMany(g => g).Select(t => t.TagId).Distinct().ToList(); var popularTagDtos = new List <PopularTagDto>(); for (var i = 0; i < input.ResultCount && i < postTagsGrouped.Count; i++) { var tagId = postTagsGrouped[i]; var count = postTags.Count(t => t.TagId == tagId); if (count < input.MinimumPostCount) { break; } var tag = await _tagRepository.GetAsync(tagId); popularTagDtos.Add(new PopularTagDto { Tag = ObjectMapper.Map <Tag, TagDto>(tag), Count = count }); } return(popularTagDtos); }
public async Task <ListResultDto <PostWithDetailsDto> > GetListByBlogId(Guid id) { var posts = _postRepository.GetPostsByBlogId(id); var postDtos = new List <PostWithDetailsDto>( ObjectMapper.Map <List <Post>, List <PostWithDetailsDto> >(posts)); foreach (var postDto in postDtos) { var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == postDto.Id); var tags = await _tagRepository.GetListAsync(tagIds.Select(t => t.TagId)); postDto.Tags = ObjectMapper.Map <List <Tag>, List <TagDto> >(tags); postDto.CommentCount = (await _commentRepository.GetListAsync()).Count(p => p.PostId == postDto.Id); } return(new ListResultDto <PostWithDetailsDto>(postDtos)); }
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) { var tag = await _tagRepository.GetAsync(oldTag.TagId); 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); } } }
public List <PostTag> GetPostTagList(Expression <Func <PostTag, bool> > predicate) { return(_postTag.GetListAsync(predicate).Result); }