예제 #1
0
        public async Task <BasePageList <ArticleResult> > GetAsync(ArticleFilter filter)
        {
            var articlePageList = await _articleRepository.GetAsync(filter);

            var createdByIds = articlePageList.Collections.Select(x => x.CreatedById).ToArray();
            var updatedByIds = articlePageList.Collections.Select(x => x.UpdatedById).ToArray();

            var createdByUsers = await _userRepository.GetNameByIdsAsync(createdByIds);

            var updatedByUsers = await _userRepository.GetNameByIdsAsync(updatedByIds);

            var articleIds = articlePageList.Collections.Select(x => x.Id);
            var pictures   = await _articlePictureRepository.GetArticlePicturesByArticleIdsAsync(articleIds, new IdRequestFilter <long>
            {
                CanGetDeleted   = filter.CanGetDeleted,
                CanGetInactived = filter.CanGetInactived
            });

            var userAvatars = await _userPhotoRepository.GetUserPhotosByUserIdsAsync(createdByIds, UserPictureType.Avatar);

            foreach (var article in articlePageList.Collections)
            {
                var createdBy = createdByUsers.FirstOrDefault(x => x.Id == article.CreatedById);
                article.CreatedBy = createdBy.DisplayName;

                var updatedBy = updatedByUsers.FirstOrDefault(x => x.Id == article.UpdatedById);
                article.UpdatedBy   = updatedBy.DisplayName;
                article.Description = string.IsNullOrEmpty(article.Description) ? HtmlUtil.TrimHtml(article.Content) : article.Description;

                var picture = pictures.FirstOrDefault(x => x.ArticleId == article.Id);
                if (picture != null)
                {
                    article.Picture = new PictureResult {
                        Id = picture.PictureId
                    };
                }

                var avatar = userAvatars.FirstOrDefault(x => x.UserId == article.CreatedById);
                if (avatar != null)
                {
                    article.CreatedByPhotoCode = avatar.Code;
                }
            }

            return(articlePageList);
        }
예제 #2
0
        public async Task <BasePageList <FeedResult> > GetAsync(FeedFilter filter)
        {
            var feedsPageList = await _feedRepository.GetAsync(filter);

            // Get article pictures
            var articleIds      = feedsPageList.Collections.Where(x => x.FeedType == FeedType.Article).Select(x => long.Parse(x.Id));
            var articlePictures = await _articlePictureRepository
                                  .GetArticlePicturesByArticleIdsAsync(articleIds, new IdRequestFilter <long>(), ArticlePictureType.Thumbnail);

            // Get product pictures
            var productIds      = feedsPageList.Collections.Where(x => x.FeedType == FeedType.Product).Select(x => long.Parse(x.Id));
            var productPictures = await _productPictureRepository
                                  .GetProductPicturesByProductIdsAsync(productIds, new IdRequestFilter <long>(), ProductPictureType.Thumbnail);

            // Get farm pictures
            var farmIds      = feedsPageList.Collections.Where(x => x.FeedType == FeedType.Farm).Select(x => long.Parse(x.Id));
            var farmPictures = await _farmPictureRepository
                               .GetFarmPicturesByFarmIdsAsync(farmIds, new IdRequestFilter <long>(), FarmPictureType.Thumbnail);

            // Get user pictures
            var userIds      = feedsPageList.Collections.Where(x => x.FeedType == FeedType.User).Select(x => long.Parse(x.Id));
            var userPictures = await _userPhotoRepository.GetUserPhotoByUserIdsAsync(userIds, UserPictureType.Avatar);

            // Get created by user's photos
            var createdByIds          = feedsPageList.Collections.Select(x => x.CreatedById);
            var createdByUserPictures = await _userPhotoRepository.GetUserPhotoByUserIdsAsync(createdByIds, UserPictureType.Avatar);

            var createdByUsers = await _userRepository.GetNameByIdsAsync(createdByIds);

            foreach (var feed in feedsPageList.Collections)
            {
                var createdBy = createdByUsers.FirstOrDefault(x => x.Id == feed.CreatedById);
                feed.CreatedByName = createdBy.DisplayName;
                feed.Description   = HtmlUtil.TrimHtml(feed.Description);
                var createdByUserPicture = createdByUserPictures.FirstOrDefault(x => x.UserId == feed.CreatedById);
                if (createdByUserPicture != null)
                {
                    feed.CreatedByPhotoCode = createdByUserPicture.Code;
                }

                switch (feed.FeedType)
                {
                case FeedType.Article:
                    var articlePicture = articlePictures.FirstOrDefault(x => x.ArticleId == long.Parse(feed.Id));
                    if (articlePicture != null)
                    {
                        feed.PictureId = articlePicture.PictureId.ToString();
                    }
                    break;

                case FeedType.Product:
                    var productPicture = productPictures.FirstOrDefault(x => x.ProductId == long.Parse(feed.Id));
                    if (productPicture != null)
                    {
                        feed.PictureId = productPicture.PictureId.ToString();
                    }
                    break;

                case FeedType.Farm:
                    var farmePicture = farmPictures.FirstOrDefault(x => x.FarmId == long.Parse(feed.Id));
                    if (farmePicture != null)
                    {
                        feed.PictureId = farmePicture.PictureId.ToString();
                    }
                    break;

                default:
                    break;
                }
            }

            return(feedsPageList);
        }