Beispiel #1
0
        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);
        }
Beispiel #2
0
        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));
        }
Beispiel #3
0
        public IDomainRepositoryQueryContext <IDictionary <int, CustomEntityRenderSummary> > AsRenderSummaries(PublishStatusQuery?publishStatus = null)
        {
            var query = new GetCustomEntityRenderSummariesByIdRangeQuery(_customEntityIds);

            if (publishStatus.HasValue)
            {
                query.PublishStatus = publishStatus.Value;
            }

            return(DomainRepositoryQueryContextFactory.Create(query, ExtendableContentRepository));
        }
        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 _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));
        }
        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());
        }
        private async Task <List <CustomEntityVersion> > QueryAsync(GetCustomEntityRenderSummariesByIdRangeQuery query, IExecutionContext executionContext)
        {
            if (query.PublishStatus == PublishStatusQuery.SpecificVersion)
            {
                throw new InvalidOperationException("PublishStatusQuery.SpecificVersion not supported in GetCustomEntityRenderSummariesByDefinitionCodeQuery");
            }

            var dbQuery = await _dbContext
                          .CustomEntityPublishStatusQueries
                          .AsNoTracking()
                          .Include(e => e.CustomEntityVersion)
                          .ThenInclude(e => e.CustomEntity)
                          .FilterActive()
                          .FilterByStatus(query.PublishStatus, executionContext.ExecutionDate)
                          .Where(v => query.CustomEntityIds.Contains(v.CustomEntityId))
                          .ToListAsync();

            return(dbQuery
                   .Select(v => v.CustomEntityVersion)
                   .ToList());
        }
        public async Task <IDictionary <int, CustomEntityRenderSummary> > ExecuteAsync(GetCustomEntityRenderSummariesByIdRangeQuery query, IExecutionContext executionContext)
        {
            var dbResults = await QueryAsync(query, executionContext);

            EnforcePermissions(dbResults, executionContext);
            var results = await _customEntityRenderSummaryMapper.MapAsync(dbResults, executionContext);

            return(results.ToDictionary(r => r.CustomEntityId));
        }