internal static IEnumerable <AuthorModel> GetContentByAuthors(this UmbracoHelper helper, IMasterModel masterModel) { var authorsNode = GetAuthorsNode(masterModel); var authors = authorsNode.Children.ToList(); //TODO: Should we have a paged result instead of returning everything? var pager = new PagerModel(int.MaxValue, 0, 1); var listNodes = GetListNodes(masterModel); var listNodeIds = listNodes.Select(x => x.Id).ToArray(); //used to lazily retrieve posts by author - as it turns out however this extra work to do this lazily in case the //Children property of the AuthorModel is not used is a waste because as soon as we get the latest post date for an author it will //iterate. Oh well, it's still lazy just in case. var lazyAuthorPosts = new Lazy <Dictionary <string, Tuple <IPublishedContent, List <IPublishedContent> > > >(() => { var authorNames = authors.Select(x => x.Name).ToArray(); //this will track author names to document ids var postsWithAuthors = new Dictionary <int, Tuple <string, IPublishedContent> >(); var posts = helper.GetPostsSortedByPublishedDate(pager, x => { //ensure there's an author and one that matches someone in the author list var xmlNode = x.SelectSingleNode("author [not(@isDoc)]"); var hasName = xmlNode != null && authorNames.Contains(xmlNode.Value); if (hasName) { postsWithAuthors[int.Parse(x.GetAttribute("id", ""))] = Tuple.Create(xmlNode.Value, authors.First(a => a.Name == xmlNode.Value)); } return(hasName); }, listNodeIds); //this tracks all documents to an author name/author content var authorPosts = new Dictionary <string, Tuple <IPublishedContent, List <IPublishedContent> > >(); //read forward foreach (var post in posts) { var authorInfo = postsWithAuthors[post.Id]; var authorName = authorInfo.Item1; var authorContent = authorInfo.Item2; if (authorPosts.ContainsKey(authorName)) { authorPosts[authorName].Item2.Add(post); } else { authorPosts.Add(authorName, Tuple.Create(authorContent, new List <IPublishedContent> { post })); } } return(authorPosts); }); return(authors.OrderBy(x => x.Name) .Select(x => new AuthorModel(x, GetLazyAuthorPosts(x, lazyAuthorPosts), pager, GetPostCount(helper, x.Name, listNodeIds)))); }
internal static IEnumerable <IPublishedContent> GetContentByAuthor(this UmbracoHelper helper, IPublishedContent[] listNodes, string authorName, PagerModel pager) { var listNodeIds = listNodes.Select(x => x.Id).ToArray(); var postWithAuthor = helper.GetPostsSortedByPublishedDate(pager, x => string.Equals(x.Value <string>("author"), authorName.Replace("-", " "), StringComparison.InvariantCultureIgnoreCase), listNodeIds); var rootPageModel = new ListModel(listNodes[0], postWithAuthor, pager); return(rootPageModel.Posts); }
/// <summary> /// Returns a list of the most recent posts by archive /// </summary> /// <param name="helper"></param> /// <param name="masterModel"></param> /// <param name="page"></param> /// <param name="pageSize"></param> /// <returns></returns> public static IEnumerable <PostModel> GetRecentPostsByArchive(this UmbracoHelper helper, IMasterModel masterModel, int page, int pageSize) { var pager = new PagerModel(pageSize, page - 1, 1); var listItems = helper.GetPostsSortedByPublishedDate(pager, null, masterModel.Id); var rootPageModel = new ListModel(masterModel, listItems, pager); return(rootPageModel.Posts); }
/// <summary> /// Returns a list of the most recent posts /// </summary> /// <param name="helper"></param> /// <param name="masterModel"></param> /// <param name="page"></param> /// <param name="pageSize"></param> /// <returns></returns> public static IEnumerable <PostModel> GetRecentPosts(this UmbracoHelper helper, IMasterModel masterModel, int page, int pageSize) { var listNodes = GetListNodes(masterModel); var listNodeIds = listNodes.Select(x => x.Id).ToArray(); var pager = new PagerModel(pageSize, page - 1, 1); var listItems = helper.GetPostsSortedByPublishedDate(pager, null, listNodeIds); var rootPageModel = new ListModel(listNodes[0], listItems, pager); return(rootPageModel.Posts); }
internal static IEnumerable <IPublishedContent> GetContentByAuthor(this UmbracoHelper helper, IPublishedContent[] listNodes, string authorName, PagerModel pager) { var listNodeIds = listNodes.Select(x => x.Id).ToArray(); var postWithAuthor = helper.GetPostsSortedByPublishedDate(pager, x => { //filter by author var xmlNode = x.SelectSingleNode("author [not(@isDoc)]"); return(xmlNode != null && string.Equals(xmlNode.Value, authorName.Replace("-", " "), StringComparison.InvariantCultureIgnoreCase)); }, listNodeIds); var rootPageModel = new ListModel(listNodes[0], postWithAuthor, pager); return(rootPageModel.Posts); }
/// <summary> /// Returns a list of the most recent posts /// </summary> /// <param name="helper"></param> /// <param name="masterModel"></param> /// <param name="count"></param> /// <returns></returns> public static IEnumerable <PostModel> GetRecentPosts(this UmbracoHelper helper, IMasterModel masterModel, int count) { var listNode = masterModel.RootBlogNode.Children .FirstOrDefault(x => x.DocumentTypeAlias.InvariantEquals("ArticulateArchive")); if (listNode == null) { throw new InvalidOperationException("An ArticulateArchive document must exist under the root Articulate document"); } var pager = new PagerModel(count, 0, 1); var listItems = helper.GetPostsSortedByPublishedDate(listNode.Id, pager); var rootPageModel = new ListModel(listNode, listItems, pager); return(rootPageModel.Children <PostModel>()); }