public async Task <ICollection <CatSummary> > ExecuteAsync(GetCatSummariesByUserLikedQuery query, IExecutionContext executionContext) { var userCatIds = await _dbContext .CatLikes .AsNoTracking() .Where(c => c.UserId == query.UserId) .OrderByDescending(c => c.CreateDate) .Select(c => c.CatCustomEntityId) .ToListAsync(); var customEntityQuery = new GetCustomEntityRenderSummariesByIdRangeQuery(userCatIds); var catCustomEntities = await _customEntityRepository.GetCustomEntityRenderSummariesByIdRangeAsync(customEntityQuery); // GetByIdRange queries return a dictionary to make lookups easier, so we // have an extra step to do if we want to set the ordering var orderedCats = catCustomEntities .FilterAndOrderByKeys(userCatIds) .ToList(); var allMainImages = await GetMainImages(orderedCats); var allLikeCounts = await GetLikeCounts(orderedCats); return(MapCats(orderedCats, allMainImages, allLikeCounts)); }
public async Task <BlogPostDisplayModel> MapDisplayModelAsync( CustomEntityRenderDetails renderDetails, BlogPostDataModel dataModel, PublishStatusQuery publishStatusQuery ) { var vm = new BlogPostDisplayModel(); vm.MetaDescription = dataModel.ShortDescription; vm.PageTitle = renderDetails.Title; vm.Title = renderDetails.Title; vm.Date = renderDetails.CreateDate; vm.FullPath = renderDetails.PageUrls.FirstOrDefault(); if (!EnumerableHelper.IsNullOrEmpty(dataModel.CategoryIds)) { // We manually query and map relations which gives us maximum flexibility when mapping models // Fortunately the framework provides tools to make this fairly simple var categoriesQuery = new GetCustomEntityRenderSummariesByIdRangeQuery(dataModel.CategoryIds, publishStatusQuery); var customEntities = await _customEntityRepository.GetCustomEntityRenderSummariesByIdRangeAsync(categoriesQuery); vm.Categories = customEntities .FilterAndOrderByKeys(dataModel.CategoryIds) .Select(c => MapCategory(c)) .ToList(); } return(vm); }
public async Task <Dictionary <int, Feature> > ExecuteAsync(GetFeaturesByIdRangeQuery query, IExecutionContext executionContext) { var customEntityQuery = new GetCustomEntityRenderSummariesByIdRangeQuery(query.FeatureIds); var customEntities = await _customEntityRepository.GetCustomEntityRenderSummariesByIdRangeAsync(customEntityQuery);; var features = customEntities .Select(d => MapFeature(d.Value)) .ToDictionary(f => f.FeatureId); return(features); }
/// <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 _imageAssetRepository.GetImageAssetRenderDetailsByIdRangeAsync(imageAssetIds); var authorQuery = new GetCustomEntityRenderSummariesByIdRangeQuery(authorIds, ambientEntityPublishStatusQuery); var authorLookup = await _customEntityRepository.GetCustomEntityRenderSummariesByIdRangeAsync(authorQuery); 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)); }
private async Task <ICollection <CategorySummary> > MapCategories( BlogPostDataModel dataModel, PublishStatusQuery publishStatusQuery ) { if (EnumerableHelper.IsNullOrEmpty(dataModel.CategoryIds)) { return(Array.Empty <CategorySummary>()); } // We manually query and map relations which gives us maximum flexibility when // mapping models. Fortunately the framework provides tools to make this fairly simple var categoriesQuery = new GetCustomEntityRenderSummariesByIdRangeQuery(dataModel.CategoryIds, publishStatusQuery); var results = await _customEntityRepository.GetCustomEntityRenderSummariesByIdRangeAsync(categoriesQuery); return(results .FilterAndOrderByKeys(dataModel.CategoryIds) .Select(c => MapCategory(c)) .ToList()); }