public virtual PostModel PreparePostModel(TblPosts post, TblUsers currentUser, UrlHelper url) { var result = post.Adapt <PostModel>(); result.Title = post.GetLocalized(p => p.Title); result.PageTitle = post.GetLocalized(p => p.PageTitle); result.MetaDescription = post.GetLocalized(p => p.MetaDescription); result.MetaKeyWords = post.GetLocalized(p => p.MetaKeyWords); var likesCount = _userLikesService.GetNumberOfLikes(post.Id); result.NumberOfLikes = likesCount; result.LastUpdate = post.LastUpDate ?? post.PublishDate; result.Categories = post.Categories .Select(p => new PostCategoriesModel() { Id = p.Id, CategoryName = p.GetLocalized(x => x.CategoryName), Slug = p.Slug, CategoryUrl = post.PostType == PostType.BlogPost ? url.Action("FilterByCategory", "Blog", new { slug = p.Slug }) : url.Action("FilterByCategory", "Product", new { slug = p.Slug }) }) .ToList(); result.TagsList = post.Tags .Select(p => new Tuple <int, string>(p.Id, p.GetLocalized(x => x.Tag))) .ToList(); result.LikeWishlistButtonsModel = new LikeWishlistButtonsModel() { PostId = post.Id, AlreadyAddedToWishlist = _userWishlistService.UserAddedThisPostToWishlist(post.Id, currentUser?.Id), AlreadyLiked = _userLikesService.UserLikedThisPost(post.Id, currentUser?.Id) }; result.Images.Clear(); foreach (var img in post.Images.OrderBy(p => p.DisplayOrder)) { result.Images.Add(new PostImagesModel() { Title = img.GetLocalized(p => p.Title) ?? result.PageTitle, Alt = img.GetLocalized(p => p.Alt) ?? result.Title, ImageUrl = img.GetLocalized(p => p.ImageUrl), DisplayOrder = img.DisplayOrder }); } result.Descriptions.Clear(); foreach (var desc in post.Descriptions.OrderBy(p => p.DisplayOrder)) { var description = desc.GetLocalized(x => x.HtmlDescription); result.Descriptions.Add(new PostDescriptionsModel() { Description = description, Title = desc.GetLocalized(x => x.Title), IsRtl = description.StripHtml().IsRtlLanguage() }); } result.Attributes.Clear(); foreach (var attr in post.Attributes) { result.Attributes.Add(new PostAttributesModel() { Type = attr.PostAttribute.AttributeType, Name = attr.PostAttribute.GetLocalized(p => p.Name), Value = attr.PostAttribute.AttributeType == PostAttributeType.Option ? attr.AttributeOption.GetLocalized(p => p.Name) : attr.GetLocalized(p => p.Value), DisplayOrder = attr.DisplayOrder }); } if (post.PostType == PostType.BlogPost) { result.PostUrl = new Uri(url.Action("Post", "Blog", new { slug = post.Slug }, _httpContext.Request.Url.Scheme)).ToString(); } else if (post.PostType == PostType.Product) { result.PostUrl = new Uri(url.Action("Index", "Product", new { slug = post.Slug }, _httpContext.Request.Url.Scheme)).ToString(); } else { result.PostUrl = new Uri(url.Action("Index", "Search", new SearchTermModel() { PostType = null, OrderBy = SearchResultSortType.Score, SearchPlace = SearchPlace.Title, Query = post.Title }, _httpContext.Request.Url.Scheme)).ToString(); } return(result); }
protected virtual Document MapPost(TblPosts post, TblLanguages language, PostType?postType) { var title = post.GetLocalized(p => p.Title, language.Id); var descriptions = post.Descriptions.Where(p => p.AddToSearchEngineIndexes).Aggregate("", (current, description) => current + description.GetLocalized(p => p.HtmlDescription, language.Id)?.ConvertHtmlToText() + Environment.NewLine + Environment.NewLine); var tags = post.Tags.Aggregate("", (current, tag) => current + tag.GetLocalized(p => p.Tag, language.Id) + ", "); var categories = post.Categories.Aggregate("", (current, cat) => current + (cat.Id + " , ")); var keyWords = post.GetLocalized(p => p.MetaKeyWords, language.Id); var document = new Document(); document.Add(new Field("ID", post.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("PostType", postType?.ToString() ?? "", Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("LanguageId", language.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("LanguageCode", language.IsoCode, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("PublishDate", DateTools.DateToString(post.PublishDate, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("LastUpDate", DateTools.DateToString(post.LastUpDate ?? post.PublishDate, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.ANALYZED)); var numberOfViewField = new NumericField("NumberOfVisit", Field.Store.YES, true); numberOfViewField.SetIntValue(post.NumberOfViews); document.Add(numberOfViewField); if (!string.IsNullOrEmpty(title)) { var titleField = new Field("Title", title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS) { Boost = 100 }; document.Add(titleField); } if (!string.IsNullOrWhiteSpace(descriptions.Replace(Environment.NewLine, ""))) { var descriptionField = new Field("Description", descriptions, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS) { Boost = 50 }; document.Add(descriptionField); } if (!string.IsNullOrWhiteSpace(tags.Replace(", ", ""))) { var tagField = new Field("Tags", tags, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS) { Boost = 50 }; document.Add(tagField); } var categoriesField = new Field("Categories", categories, Field.Store.YES, Field.Index.ANALYZED); document.Add(categoriesField); if (!string.IsNullOrEmpty(keyWords)) { var keywordsField = new Field("Keywords", keyWords, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS) { Boost = 50 }; document.Add(keywordsField); } return(document); }