public virtual IPagedList <PostCardViewModel> PreparePostCardViewModel(IPagedList <TblPosts> posts, TblUsers currentUser, UrlHelper url) { var numberOfLikes = _userLikesService.GetNumberOfLikes(posts.Select(p => p.Id).ToArray()); var userAddedThisPostsToWishlist = _userWishlistService.UserAddedThisPostToWishlist(posts.Select(p => p.Id).ToArray(), currentUser?.Id); var userLikedThisPosts = _userLikesService.UserLikedThisPost(posts.Select(p => p.Id).ToArray(), currentUser?.Id); var result = new List <PostCardViewModel>(); foreach (var post in posts) { var model = post.Adapt <PostCardViewModel>(); model.LastUpDate = post.LastUpDate ?? post.PublishDate; model.MainImageUrl = post.Images?.OrderBy(p => p.DisplayOrder).FirstOrDefault() ?.ImageUrl ?? ""; model.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(); var desc = post.Descriptions?.OrderBy(p => p.DisplayOrder).FirstOrDefault()?.GetLocalized(x => x.HtmlDescription) ?? ""; model.DescriptionTruncated = desc.ConvertHtmlToText().TruncateText(350); if (post.PostType == PostType.BlogPost) { model.PostUrl = new Uri(url.Action("Post", "Blog", new { slug = post.Slug }, _httpContext.Request.Url.Scheme)).ToString(); } else if (post.PostType == PostType.Product) { model.PostUrl = new Uri(url.Action("Index", "Product", new { slug = post.Slug }, _httpContext.Request.Url.Scheme)).ToString(); } else { model.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(); } if (numberOfLikes.ContainsKey(model.Id)) { model.NumberOfLikes = numberOfLikes[model.Id]; } var likeWishlistButtonsModel = new LikeWishlistButtonsModel() { PostId = model.Id }; if (userAddedThisPostsToWishlist.ContainsKey(model.Id)) { likeWishlistButtonsModel.AlreadyAddedToWishlist = userAddedThisPostsToWishlist[model.Id]; } if (userLikedThisPosts.ContainsKey(model.Id)) { likeWishlistButtonsModel.AlreadyLiked = userLikedThisPosts[model.Id]; } model.LikeWishlistButtonsModel = likeWishlistButtonsModel; result.Add(model); } return(new StaticPagedList <PostCardViewModel>(result, posts.PageNumber, posts.PageSize, posts.TotalItemCount)); }
public virtual IPagedList <ProductCardViewModel> PrepareProductCardViewModel(IPagedList <TblProducts> products, TblUsers currentUser, UrlHelper url) { var numberOfLikes = _userLikesService.GetNumberOfLikes(products.Select(p => p.Id).ToArray()); var numberOfDownloads = _productService.GetNumberOfDownloads(products.Select(p => p.Id).ToArray()); var userAddedThisPostsToWishlist = _userWishlistService.UserAddedThisPostToWishlist(products.Select(p => p.Id).ToArray(), currentUser?.Id); var userLikedThisPosts = _userLikesService.UserLikedThisPost(products.Select(p => p.Id).ToArray(), currentUser?.Id); var userGroups = _userGroupsService.GetAsEnumerable().ToList(); var result = new List <ProductCardViewModel>(); foreach (var product in products) { var model = product.Adapt <ProductCardViewModel>(); model.LastUpDate = product.LastUpDate ?? product.PublishDate; model.MainImageUrl = product.Images?.OrderBy(p => p.DisplayOrder).FirstOrDefault() ?.ImageUrl ?? ""; model.Categories = product.Categories .Select(p => new PostCategoriesModel() { Id = p.Id, CategoryName = p.GetLocalized(x => x.CategoryName), Slug = p.Slug, CategoryUrl = url.Action("FilterByCategory", "Product", new { slug = p.Slug }) }) .ToList(); var desc = product.Descriptions?.OrderBy(p => p.DisplayOrder).FirstOrDefault()?.GetLocalized(x => x.HtmlDescription) ?? ""; model.DescriptionTruncated = desc.ConvertHtmlToText().TruncateText(350); model.PostUrl = new Uri(url.Action("Index", "Product", new { slug = product.Slug }, _httpContext.Request.Url.Scheme)).ToString(); if (numberOfLikes.ContainsKey(model.Id)) { model.NumberOfLikes = numberOfLikes[model.Id]; } if (numberOfDownloads.ContainsKey(model.Id)) { model.NumberOfDownloads = numberOfDownloads[model.Id]; } var likeWishlistButtonsModel = new LikeWishlistButtonsModel() { PostId = model.Id }; if (userAddedThisPostsToWishlist.ContainsKey(model.Id)) { likeWishlistButtonsModel.AlreadyAddedToWishlist = userAddedThisPostsToWishlist[model.Id]; } if (userLikedThisPosts.ContainsKey(model.Id)) { likeWishlistButtonsModel.AlreadyLiked = userLikedThisPosts[model.Id]; } model.LikeWishlistButtonsModel = likeWishlistButtonsModel; model.DownloadModel = new ProductCardDownloadModel { ProductId = product.Id, DownloadLimitedToUserGroup = product.DownloadLimitedToUserGroupId != null?userGroups.FirstOrDefault(p => p.Id == product.DownloadLimitedToUserGroupId.Value) : null, ShowPurchaseBtn = product.Price > 0 || !string.IsNullOrWhiteSpace(product.LicenseGeneratorServiceId), HigherUserGroupsCanDownload = product.HigherUserGroupsCanDownload, PriceForCurrentUser = product.Price }; result.Add(model); } return(new StaticPagedList <ProductCardViewModel>(result, products.PageNumber, products.PageSize, products.TotalItemCount)); }