private async Task <PagedQueryResult <BlogPostSummary> > MapBlogPostsAsync(PagedQueryResult <CustomEntityRenderSummary> customEntityResult) { var blogPosts = new List <BlogPostSummary>(customEntityResult.Items.Count()); var imageAssetIds = customEntityResult .Items .Select(i => (BlogPostDataModel)i.Model) .Select(m => m.ThumbnailImageAssetId) .Distinct(); var images = await _imageAssetRepository.GetImageAssetRenderDetailsByIdRangeAsync(imageAssetIds); foreach (var customEntity in customEntityResult.Items) { var model = (BlogPostDataModel)customEntity.Model; var blogPost = new BlogPostSummary(); blogPost.Title = customEntity.Title; blogPost.ShortDescription = model.ShortDescription; blogPost.ThumbnailImageAsset = images.GetOrDefault(model.ThumbnailImageAssetId); blogPost.FullPath = customEntity.PageUrls.FirstOrDefault(); blogPost.PostDate = customEntity.PublishDate; blogPosts.Add(blogPost); } return(customEntityResult.ChangeType(blogPosts)); }
/// <summary> /// Here we map the raw custom entity data from Cofoundry into our /// own BlogPostSummary which will get sent to be rendered in the /// view. /// /// This code is repeated in HomepageBlogPostsViewComponent for /// simplicity, but typically you'd put the code into a shared /// mapper or break data access out into it's own shared layer. /// </summary> private async Task <PagedQueryResult <BlogPostSummary> > MapBlogPostsAsync( PagedQueryResult <CustomEntityRenderSummary> customEntityResult, PublishStatusQuery ambientEntityPublishStatusQuery ) { var blogPosts = new List <BlogPostSummary>(customEntityResult.Items.Count()); var imageAssetIds = customEntityResult .Items .Select(i => (BlogPostDataModel)i.Model) .Select(m => m.ThumbnailImageAssetId) .Distinct(); var authorIds = customEntityResult .Items .Select(i => (BlogPostDataModel)i.Model) .Select(m => m.AuthorId) .Distinct(); var imageLookup = await _contentRepository .ImageAssets() .GetByIdRange(imageAssetIds) .AsRenderDetails() .ExecuteAsync(); var authorQuery = new GetCustomEntityRenderSummariesByIdRangeQuery(authorIds, ambientEntityPublishStatusQuery); var authorLookup = await _contentRepository .CustomEntities() .GetByIdRange(authorIds) .AsRenderSummaries(ambientEntityPublishStatusQuery) .ExecuteAsync(); foreach (var customEntity in customEntityResult.Items) { var model = (BlogPostDataModel)customEntity.Model; var blogPost = new BlogPostSummary(); blogPost.Title = customEntity.Title; blogPost.ShortDescription = model.ShortDescription; blogPost.ThumbnailImageAsset = imageLookup.GetOrDefault(model.ThumbnailImageAssetId); blogPost.FullPath = customEntity.PageUrls.FirstOrDefault(); blogPost.PostDate = customEntity.PublishDate; var author = authorLookup.GetOrDefault(model.AuthorId); if (author != null) { blogPost.AuthorName = author.Title; } blogPosts.Add(blogPost); } return(customEntityResult.ChangeType(blogPosts)); }