Example #1
0
        /// <summary>
        /// 获取博客列表下的文章内容
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <PagedResultDto <PostDetailsDto> > GetPostsPagedForHome(GetPortalBlogsInput input)
        {
            var query = _postManager.QueryPostsAsNoTracking()
                        //模糊搜索地址
                        .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Url.Contains(input.FilterText))
                        //模糊搜索标题
                        .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Title.Contains(input.FilterText))
                        //模糊搜索内容
                        .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Content.Contains(input.FilterText));

            // TODO:根据传入的参数添加过滤条件

            var tag = input.TagName.IsNullOrWhiteSpace()
                ? null
                : await _tagManager.QueryTagsAsNoTracking().FirstOrDefaultAsync(a => a.Name == input.TagName);

            if (tag != null)
            {
                query = query.Where(a => a.Tags.Any(t => t.TagId == tag.Id));
            }



            var count = await query.CountAsync();

            var posts = await query
                        .OrderByDescending(d => d.CreationTime)
                        .PageBy(input)
                        .ToListAsync();



            var postDtos = ObjectMapper.Map <List <PostDetailsDto> >(posts);

            var userDictionary = new Dictionary <long, UserListDto>();
            var blogDictionary = new Dictionary <Guid, string>();

            foreach (var postDto in postDtos)
            {
                // 通过文章的创建者userid可以获取关于用户的更多信息


                if (postDto.CreatorUserId.HasValue)
                {
                    if (!userDictionary.ContainsKey(postDto.CreatorUserId.Value))
                    {
                        var tes = AbpSession.TenantId;
                        // AbpSession
                        var creatorUser = await UserManager.Users
                                          .IgnoreQueryFilters()
                                          .FirstOrDefaultAsync(a => a.Id == postDto.CreatorUserId.Value);

                        if (creatorUser != null)
                        {
                            userDictionary[creatorUser.Id] = ObjectMapper.Map <UserListDto>(creatorUser);
                        }
                    }

                    if (userDictionary.ContainsKey(postDto.CreatorUserId.Value))
                    {
                        postDto.Writer = userDictionary[(long)postDto.CreatorUserId];
                    }
                }


                // 获取文章的博客短名称

                if (!blogDictionary.ContainsKey(postDto.BlogId))
                {
                    var blogDto = await _blogManager.FindByIdAsync(postDto.BlogId);

                    blogDictionary[postDto.BlogId] = blogDto.ShortName;
                }


                if (blogDictionary.ContainsKey(postDto.BlogId))
                {
                    postDto.BlogShortName = blogDictionary[postDto.BlogId];
                }

                //获取标签
                var tags = await _tagManager.GetTagsOfPost(postDto.Id);

                postDto.Tags = ObjectMapper.Map <List <TagListDto> >(tags);
            }

            return(new PagedResultDto <PostDetailsDto>(count, postDtos));
        }