示例#1
0
        public PagedResultDto <ArticleListDto> GetSubscribeArticle(PageDto pageDto)
        {
            long        userId           = _currentUser.Id ?? 0;
            List <long> subscribeUserIds = _userSubscribeService.GetSubscribeUserId(userId);

            var articles = _articleRepository
                           .Select
                           .Include(r => r.Classify)
                           .Include(r => r.UserInfo)
                           .IncludeMany(r => r.Tags, r => r.Where(u => u.Status))
                           .IncludeMany(r => r.UserLikes, r => r.Where(u => u.CreateUserId == userId))
                           .Where(r => r.IsAudit)
                           .IncludeMany(r => r.Tags, r => r.Where(u => u.Status))
                           .WhereIf(subscribeUserIds.Count > 0, r => subscribeUserIds.Contains(r.CreateUserId))
                           .WhereIf(subscribeUserIds.Count == 0, r => false)
                           .OrderByDescending(r => r.CreateTime).ToPagerList(pageDto, out long totalCount);

            List <ArticleListDto> articleDtos = articles
                                                .Select(r =>
            {
                ArticleListDto articleDto   = _mapper.Map <ArticleListDto>(r);
                articleDto.IsLiked          = r.UserLikes.Any();
                articleDto.ThumbnailDisplay = _currentUser.GetFileUrl(articleDto.Thumbnail);
                return(articleDto);
            })
                                                .ToList();

            return(new PagedResultDto <ArticleListDto>(articleDtos, totalCount));
        }
        public async Task TestHandle_WithAuthor_ShouldFilterCorrectly()
        {
            // Arrange
            var author      = new UserProfile(Guid.NewGuid().ToString(), "email", "username");
            var otherAuthor = new UserProfile(Guid.NewGuid().ToString(), "otherAuthorEmail", "otherAuthorUsername");

            var articles = new List <Article>
            {
                new Article("first title", "first description", "first body", new DateTime(1, 2, 3), author),
                new Article("second title", "second description", "second body", new DateTime(2, 2, 3), otherAuthor),
            };

            Context.Articles.AddRange(articles);
            await Context.SaveChangesAsync();

            var expected = new ArticleListDto {
                Articles = new List <ArticleDto> {
                    Mapper.Map <ArticleDto>(articles[0])
                }
            };

            var query = new ListArticlesQuery {
                Author = author.Username
            };

            var currentUserMock = new Mock <ICurrentUserService>();

            var sut = new ListArticlesQuery.Handler(Context, Mapper, currentUserMock.Object);

            // Act
            var result = await sut.Handle(query, CancellationToken.None);

            // Assert
            result.Should().BeEquivalentTo(expected);
        }
示例#3
0
        public PageData <ArticleDto> List(ArticleListDto dto)
        {
            var data = _despository.List(dto);
            //PageData<ArticleDto> res = new PageData<ArticleDto>
            //{
            //    TotalNum = data.TotalNum,
            //    TotalPageCount = data.TotalPageCount,
            //    Items = Mapper.Map<List<ArticleDto>>(data.Items)
            //};
            var result = Mapper.Map <PageData <ArticleDto> >(data);

            return(result);
        }
        public async Task TestHandle_ShouldSkipAndTakeCorrectNumberOfItems()
        {
            // Arrange
            var user   = new UserProfile(Guid.NewGuid().ToString(), "currentUser", "currentUser");
            var author = new UserProfile(Guid.NewGuid().ToString(), "author", "author");

            var articles = new List <Article>
            {
                new Article("first title", "first description", "first body", new DateTime(1, 2, 3), author),
                new Article("second title", "second description", "second body", new DateTime(2, 2, 3), author),
                new Article("third title", "third description", "third body", new DateTime(3, 3, 3), user),
            };

            Context.Articles.AddRange(articles);
            Context.UserFollowers.Add(new UserFollower(author, user));
            await Context.SaveChangesAsync();

            var expected = new ArticleListDto {
                Articles = articles
                           .Take(2)
                           .OrderByDescending(a => a.UpdatedAt)
                           .Skip(1)
                           .Take(1)
                           .Select(a => {
                    var dto = Mapper.Map <ArticleDto>(a);
                    dto.Author.Following = true;
                    return(dto);
                })
            };

            var query = new FeedArticlesQuery {
                Limit  = 1,
                Offset = 1
            };

            var currentUser = Mock.Of <ICurrentUserService>(s => s.UserId == user.Id);

            var sut = new FeedArticlesQuery.Handler(Context, Mapper, currentUser);

            // Act
            var result = await sut.Handle(query, CancellationToken.None);

            // Assert
            result.Should().BeEquivalentTo(expected);
        }
示例#5
0
        public async Task <PagedResultDto <ArticleListDto> > GetArticleAsync(ArticleSearchDto searchDto)
        {
            DateTime monthDays  = DateTime.Now.AddDays(-30);
            DateTime weeklyDays = DateTime.Now.AddDays(-7);
            DateTime threeDays  = DateTime.Now.AddDays(-3);

            long?          userId   = _currentUser.Id;
            List <Article> articles = await _articleRepository
                                      .Select
                                      .Include(r => r.Classify)
                                      .Include(r => r.UserInfo)
                                      .IncludeMany(r => r.Tags, r => r.Where(u => u.Status == true))
                                      .IncludeMany(r => r.UserLikes, r => r.Where(u => u.CreateUserId == userId))
                                      .Where(r => r.IsAudit == true)
                                      .WhereCascade(r => r.IsDeleted == false)
                                      .WhereIf(searchDto.UserId != null, r => r.CreateUserId == searchDto.UserId)
                                      .WhereIf(searchDto.TagId.HasValue, r => r.Tags.AsSelect().Any(u => u.Id == searchDto.TagId))
                                      .WhereIf(searchDto.ClassifyId.HasValue, r => r.ClassifyId == searchDto.ClassifyId)
                                      .WhereIf(searchDto.ChannelId.HasValue, r => r.ChannelId == searchDto.ChannelId)
                                      .WhereIf(searchDto.Title.IsNotNullOrEmpty(), r => r.Title.Contains(searchDto.Title))
                                      .WhereIf(searchDto.Sort == "THREE_DAYS_HOTTEST", r => r.CreateTime > threeDays)
                                      .WhereIf(searchDto.Sort == "WEEKLY_HOTTEST", r => r.CreateTime > weeklyDays)
                                      .WhereIf(searchDto.Sort == "MONTHLY_HOTTEST", r => r.CreateTime > monthDays)
                                      .OrderByDescending(
                searchDto.Sort == "THREE_DAYS_HOTTEST" || searchDto.Sort == "WEEKLY_HOTTEST" || searchDto.Sort == "MONTHLY_HOTTEST" ||
                searchDto.Sort == "HOTTEST",
                r => (r.ViewHits + r.LikesQuantity * 20 + r.CommentQuantity * 30))
                                      .OrderByDescending(r => r.CreateTime).ToPagerListAsync(searchDto, out long totalCount);

            List <ArticleListDto> articleDtos = articles
                                                .Select(a =>
            {
                ArticleListDto articleDto = _mapper.Map <ArticleListDto>(a);

                articleDto.IsLiked          = userId != null && a.UserLikes.Any();
                articleDto.ThumbnailDisplay = _currentUser.GetFileUrl(articleDto.Thumbnail);

                return(articleDto);
            })
                                                .ToList();

            return(new PagedResultDto <ArticleListDto>(articleDtos, totalCount));
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public PageData <TArticle> List(ArticleListDto dto)
        {
            StringBuilder condition = new StringBuilder();

            if (!String.IsNullOrEmpty(dto.GuideName))
            {
                dto.GuideName = $"%{dto.GuideName}%";
                condition.Append(" and b.name like @GuideName");
            }
            if (!String.IsNullOrEmpty(dto.Content))
            {
                dto.Content = $"%{dto.Content}%";
                condition.Append(" and a.Content like @Content");
            }
            if (!String.IsNullOrEmpty(dto.Title))
            {
                dto.Title = $"%{dto.Title}%";
                condition.Append(" and a.title like @Title");
            }

            string modelQuery   = $@"select  a.* from g_Article as a left join g_guide as b on a.Guideid=b.id  where 1=1 {condition} limit @startindex,@count";
            string countQuery   = "select count(1) from g_Article";
            var    mixCondition = new
            {
                startindex = (dto.Page - 1) * dto.Size,
                count      = dto.Size
            };

            using (IDbConnection db = new MySqlConnection(constr))
            {
                List <TArticle>     list      = db.Query <TArticle>(modelQuery, mixCondition).ToList();
                int                 totalNum  = db.Query <int>(countQuery, mixCondition).SingleOrDefault();
                float               totlapage = (float)totalNum / dto.Size;
                PageData <TArticle> result    = new PageData <TArticle>
                {
                    Items          = list,
                    TotalNum       = totalNum,
                    TotalPageCount = (int)Math.Ceiling(totlapage)
                };
                return(result);
            }
        }