Ejemplo n.º 1
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);
        }