Esempio n. 1
0
        public async Task <HomePageBlogItemsModel> PrepareHomePageBlogItems()
        {
            var cacheKey    = string.Format(ModelCacheEventConsumer.BLOG_HOMEPAGE_MODEL_KEY, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id);
            var cachedModel = await _cacheManager.GetAsync(cacheKey, async() =>
            {
                var model = new HomePageBlogItemsModel();

                var blogPosts = await _blogService.GetAllBlogPosts(_storeContext.CurrentStore.Id,
                                                                   null, null, 0, _blogSettings.HomePageBlogCount);

                foreach (var post in blogPosts)
                {
                    var item               = new HomePageBlogItemsModel.BlogItemModel();
                    var description        = post.GetLocalized(x => x.BodyOverview, _workContext.WorkingLanguage.Id);
                    item.SeName            = post.GetSeName(_workContext.WorkingLanguage.Id);
                    item.Title             = post.GetLocalized(x => x.Title, _workContext.WorkingLanguage.Id);
                    item.Short             = description?.Length > _blogSettings.MaxTextSizeHomePage ? description.Substring(0, _blogSettings.MaxTextSizeHomePage) : description;
                    item.CreatedOn         = _dateTimeHelper.ConvertToUserTime(post.StartDateUtc ?? post.CreatedOnUtc, DateTimeKind.Utc);
                    item.GenericAttributes = post.GenericAttributes;

                    //prepare picture model
                    if (!string.IsNullOrEmpty(post.PictureId))
                    {
                        int pictureSize             = _mediaSettings.BlogThumbPictureSize;
                        var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.BLOG_PICTURE_MODEL_KEY, post.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id);
                        item.PictureModel           = await _cacheManager.GetAsync(categoryPictureCacheKey, async() =>
                        {
                            var picture      = await _pictureService.GetPictureById(post.PictureId);
                            var pictureModel = new PictureModel {
                                Id = post.PictureId,
                                FullSizeImageUrl = await _pictureService.GetPictureUrl(picture),
                                ImageUrl         = await _pictureService.GetPictureUrl(picture, pictureSize),
                                Title            = string.Format(_localizationService.GetResource("Media.Blog.ImageLinkTitleFormat"), post.Title),
                                AlternateText    = string.Format(_localizationService.GetResource("Media.Blog.ImageAlternateTextFormat"), post.Title)
                            };
                            return(pictureModel);
                        });
                    }
                    model.Items.Add(item);
                }
                return(model);
            });

            return(cachedModel);
        }
        public async Task <HomePageBlogItemsModel> Handle(GetHomePageBlog request, CancellationToken cancellationToken)
        {
            var cacheKey = string.Format(CacheKeyConst.BLOG_HOMEPAGE_MODEL_KEY,
                                         _workContext.WorkingLanguage.Id,
                                         _workContext.CurrentStore.Id);
            var cachedModel = await _cacheBase.GetAsync(cacheKey, async() =>
            {
                var model = new HomePageBlogItemsModel();

                var blogPosts = await _blogService.GetAllBlogPosts(_workContext.CurrentStore.Id,
                                                                   null, null, 0, _blogSettings.HomePageBlogCount);

                foreach (var post in blogPosts)
                {
                    var item        = new HomePageBlogItemsModel.BlogItemModel();
                    var description = post.GetTranslation(x => x.BodyOverview, _workContext.WorkingLanguage.Id);
                    item.SeName     = post.GetSeName(_workContext.WorkingLanguage.Id);
                    item.Title      = post.GetTranslation(x => x.Title, _workContext.WorkingLanguage.Id);
                    item.Short      = description?.Length > _blogSettings.MaxTextSizeHomePage ? description.Substring(0, _blogSettings.MaxTextSizeHomePage) : description;
                    item.CreatedOn  = _dateTimeService.ConvertToUserTime(post.StartDateUtc ?? post.CreatedOnUtc, DateTimeKind.Utc);
                    item.UserFields = post.UserFields;
                    item.Category   = (await _blogService.GetBlogCategoryByPostId(post.Id)).FirstOrDefault()?.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);

                    //prepare picture model
                    if (!string.IsNullOrEmpty(post.PictureId))
                    {
                        var pictureModel = new PictureModel
                        {
                            Id = post.PictureId,
                            FullSizeImageUrl = await _pictureService.GetPictureUrl(post.PictureId),
                            ImageUrl         = await _pictureService.GetPictureUrl(post.PictureId, _mediaSettings.BlogThumbPictureSize),
                            Title            = string.Format(_translationService.GetResource("Media.Blog.ImageLinkTitleFormat"), post.Title),
                            AlternateText    = string.Format(_translationService.GetResource("Media.Blog.ImageAlternateTextFormat"), post.Title)
                        };
                        item.PictureModel = pictureModel;
                    }
                    model.Items.Add(item);
                }
                return(model);
            });

            return(cachedModel);
        }