public async Task <HomePageBlogItemsModel> Handle(GetHomePageBlog request, CancellationToken cancellationToken) { var cacheKey = string.Format(ModelCacheEventConst.BLOG_HOMEPAGE_MODEL_KEY, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id, _webHelper.GetMachineName()); 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)) { var pictureModel = new PictureModel { Id = post.PictureId, FullSizeImageUrl = await _pictureService.GetPictureUrl(post.PictureId), ImageUrl = await _pictureService.GetPictureUrl(post.PictureId, _mediaSettings.BlogThumbPictureSize), Title = string.Format(_localizationService.GetResource("Media.Blog.ImageLinkTitleFormat"), post.Title), AlternateText = string.Format(_localizationService.GetResource("Media.Blog.ImageAlternateTextFormat"), post.Title) }; item.PictureModel = pictureModel; } model.Items.Add(item); } return(model); }); return(cachedModel); }
private async Task <NewsItemListModel.NewsItemModel> PrepareNewsItemModel(NewsItem newsItem) { var model = new NewsItemListModel.NewsItemModel(); model.Id = newsItem.Id; model.SeName = newsItem.GetSeName(_workContext.WorkingLanguage.Id); model.Title = newsItem.GetLocalized(x => x.Title, _workContext.WorkingLanguage.Id); model.Short = newsItem.GetLocalized(x => x.Short, _workContext.WorkingLanguage.Id); model.Full = newsItem.GetLocalized(x => x.Full, _workContext.WorkingLanguage.Id); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(newsItem.StartDateUtc ?? newsItem.CreatedOnUtc, DateTimeKind.Utc); //prepare picture model if (!string.IsNullOrEmpty(newsItem.PictureId)) { int pictureSize = _mediaSettings.NewsListThumbPictureSize; var categoryPictureCacheKey = string.Format(ModelCacheEventConst.NEWS_PICTURE_MODEL_KEY, newsItem.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.GetMachineName(), _storeContext.CurrentStore.Id); model.PictureModel = await _cacheManager.GetAsync(categoryPictureCacheKey, async() => { var pictureModel = new PictureModel { Id = newsItem.PictureId, FullSizeImageUrl = await _pictureService.GetPictureUrl(newsItem.PictureId), ImageUrl = await _pictureService.GetPictureUrl(newsItem.PictureId, pictureSize), Title = string.Format(_localizationService.GetResource("Media.News.ImageLinkTitleFormat"), newsItem.Title), AlternateText = string.Format(_localizationService.GetResource("Media.News.ImageAlternateTextFormat"), newsItem.Title) }; return(pictureModel); }); } return(model); }
public virtual async Task PrepareNewsItemModel(NewsItemModel model, NewsItem newsItem, bool prepareComments) { if (newsItem == null) { throw new ArgumentNullException("newsItem"); } if (model == null) { throw new ArgumentNullException("model"); } model.Id = newsItem.Id; model.MetaTitle = newsItem.GetLocalized(x => x.MetaTitle, _workContext.WorkingLanguage.Id); model.MetaDescription = newsItem.GetLocalized(x => x.MetaDescription, _workContext.WorkingLanguage.Id); model.MetaKeywords = newsItem.GetLocalized(x => x.MetaKeywords, _workContext.WorkingLanguage.Id); model.SeName = newsItem.GetSeName(_workContext.WorkingLanguage.Id); model.Title = newsItem.GetLocalized(x => x.Title, _workContext.WorkingLanguage.Id); model.Short = newsItem.GetLocalized(x => x.Short, _workContext.WorkingLanguage.Id); model.Full = newsItem.GetLocalized(x => x.Full, _workContext.WorkingLanguage.Id); model.AllowComments = newsItem.AllowComments; model.CreatedOn = _dateTimeHelper.ConvertToUserTime(newsItem.StartDateUtc ?? newsItem.CreatedOnUtc, DateTimeKind.Utc); model.NumberOfComments = newsItem.CommentCount; model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnNewsCommentPage; if (prepareComments) { var newsComments = newsItem.NewsComments.OrderBy(pr => pr.CreatedOnUtc); foreach (var nc in newsComments) { var customer = await _serviceProvider.GetRequiredService <ICustomerService>().GetCustomerById(nc.CustomerId); var commentModel = new NewsCommentModel { Id = nc.Id, CustomerId = nc.CustomerId, CustomerName = customer.FormatUserName(_customerSettings.CustomerNameFormat), CommentTitle = nc.CommentTitle, CommentText = nc.CommentText, CreatedOn = _dateTimeHelper.ConvertToUserTime(nc.CreatedOnUtc, DateTimeKind.Utc), AllowViewingProfiles = _customerSettings.AllowViewingProfiles && customer != null && !customer.IsGuest(), }; if (_customerSettings.AllowCustomersToUploadAvatars) { commentModel.CustomerAvatarUrl = await _pictureService.GetPictureUrl( customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.AvatarPictureId), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType : PictureType.Avatar); } model.Comments.Add(commentModel); } } //prepare picture model if (!string.IsNullOrEmpty(newsItem.PictureId)) { int pictureSize = prepareComments ? _mediaSettings.NewsThumbPictureSize : _mediaSettings.NewsListThumbPictureSize; var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.NEWS_PICTURE_MODEL_KEY, newsItem.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.GetMachineName(), _storeContext.CurrentStore.Id); model.PictureModel = await _cacheManager.GetAsync(categoryPictureCacheKey, async() => { var pictureModel = new PictureModel { Id = newsItem.PictureId, FullSizeImageUrl = await _pictureService.GetPictureUrl(newsItem.PictureId), ImageUrl = await _pictureService.GetPictureUrl(newsItem.PictureId, pictureSize), Title = string.Format(_localizationService.GetResource("Media.News.ImageLinkTitleFormat"), newsItem.Title), AlternateText = string.Format(_localizationService.GetResource("Media.News.ImageAlternateTextFormat"), newsItem.Title) }; return(pictureModel); }); } }
private async Task PreparePicture(NewsItem newsItem, NewsItemModel model) { if (!string.IsNullOrEmpty(newsItem.PictureId)) { var categoryPictureCacheKey = string.Format(ModelCacheEventConst.NEWS_PICTURE_MODEL_KEY, newsItem.Id, _mediaSettings.NewsThumbPictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.GetMachineName(), _storeContext.CurrentStore.Id); model.PictureModel = await _cacheManager.GetAsync(categoryPictureCacheKey, async() => { var pictureModel = new PictureModel { Id = newsItem.PictureId, FullSizeImageUrl = await _pictureService.GetPictureUrl(newsItem.PictureId), ImageUrl = await _pictureService.GetPictureUrl(newsItem.PictureId, _mediaSettings.NewsThumbPictureSize), Title = string.Format(_localizationService.GetResource("Media.News.ImageLinkTitleFormat"), newsItem.Title), AlternateText = string.Format(_localizationService.GetResource("Media.News.ImageAlternateTextFormat"), newsItem.Title) }; return(pictureModel); }); } }