/// <summary> /// Returns a list of posts for a particular nav that also satisfy other query /// critirions. /// </summary> /// <param name="query"></param> /// <returns></returns> public PostList GetPosts(PostQuery query) { List<Post> posts = new List<Post>(); List<PostEntity> postEnts = null; int totalPosts = 0; //int skip = query.PageIndex * query.PageSize; int skip = (query.PageIndex - 1) * query.PageSize; int take = query.PageSize; using (var db = new BlogDbContext()) { if (query.QueryBy == QueryBy.Index) // index { totalPosts = db.PostEntities.Count(p => p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Published); postEnts = (from p in db.PostEntities.Include(p => p.TagEntities) where p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Published orderby p.DateCreated descending select p) .Skip(skip).Take(take).ToList(); } else if (query.QueryBy == QueryBy.Drafts) // drafts { totalPosts = db.PostEntities.Count(p => p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Draft); postEnts = (from p in db.PostEntities.Include(p => p.TagEntities) where p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Draft orderby p.DateCreated descending select p) .Skip(skip).Take(take).ToList(); } else if (query.QueryBy == QueryBy.Article) { totalPosts = db.PostEntities.Count(p => p.PostType == (short)PostType.Article && p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Published); postEnts = (from p in db.PostEntities.Include(p => p.TagEntities) where p.PostType == PostType.Article && p.PostStatus == PostStatus.Published && p.NavId == query.Nav.NavId orderby p.DateCreated descending select p) .Skip(skip).Take(take).ToList(); } else if (query.QueryBy == QueryBy.Tag) { TagEntity tagEnt = db.TagEntities.FirstOrDefault(t => t.Name.ToLower() == query.TagName.ToLower() && t.NavId == query.Nav.NavId); if (tagEnt != null) { // get the total published posts count from the tag totalPosts = (from p in db.PostEntities from t in p.TagEntities where p.NavId==query.Nav.NavId && t.TagId == tagEnt.TagId && p.PostStatus == PostStatus.Published select p).Count(); //http://stackoverflow.com/questions/15556134/entity-framework-many-to-many-works-but-include-does-not/15577053?noredirect=1#15577053 postEnts = (from p in db.PostEntities.Include(p => p.TagEntities) where p.TagEntities.Any(t=>t.TagId ==tagEnt.TagId) && p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Published orderby p.DateCreated descending select p).Skip(skip).Take(take).ToList(); } } else if (query.QueryBy == QueryBy.Archive) { if (query.Month.HasValue && query.Month >0) { // get the total posts for the year and month totalPosts = (from p in db.PostEntities where p.NavId == query.Nav.NavId && p.DateCreated.Year == query.Year && p.DateCreated.Month == query.Month.Value && p.PostStatus == PostStatus.Published select p).Count(); postEnts = (from p in db.PostEntities.Include(p => p.TagEntities) where p.NavId == query.Nav.NavId && p.DateCreated.Year == query.Year && p.DateCreated.Month == query.Month.Value && p.PostStatus == PostStatus.Published orderby p.DateCreated descending select p) .Skip(skip).Take(take).ToList(); } else { // get the total posts for the year only totalPosts = (from p in db.PostEntities where p.NavId == query.Nav.NavId && p.DateCreated.Year == query.Year && p.PostStatus == PostStatus.Published select p).Count(); postEnts = (from p in db.PostEntities.Include(p => p.TagEntities) where p.NavId == query.Nav.NavId && p.DateCreated.Year == query.Year && p.PostStatus == PostStatus.Published orderby p.DateCreated descending select p) .Skip(skip).Take(take).ToList(); } } foreach (var postEnt in postEnts) { User user = db.Users.FirstOrDefault(u => u.UserId == postEnt.UserId); posts.Add(BlogMapper.MapPost(postEnt, query.Nav, user)); } PostList postList = new PostList(posts, totalPosts); return postList; } }
// ==================================================================== privates /// <summary> /// Returns <see cref="Fan.Blogs.PostList"/> based on where visitors see: blog index, tag, archive pages. /// </summary> /// <param name="query"></param> private static PostList _GetPosts(PostQuery query) { PostList postList; if (query.Cacheable) { postList = SiteCache.Get(query.CacheKey) as PostList; if (postList == null) { postList = Provider.GetPosts(query); // transform MarkDown to Html before add to cache var md = new MarkdownDeep.Markdown(); foreach (var post in postList) { post.Body = md.Transform(post.Body); if (query.QueryBy != QueryBy.Drafts) // don't show as excerpt if it's draft { var ex = BlogHelper.GetBlogPostExcerpt(post.Body); post.Body = ex.Item1; post.ShowReadMore = ex.Item2; } } SiteCache.Add(query.CacheKey, postList, query.CacheTime); } } else { postList = Provider.GetPosts(query); // transform MarkDown to Html before returning var md = new MarkdownDeep.Markdown(); foreach (var post in postList) { post.Body = md.Transform(post.Body); if (query.QueryBy != QueryBy.Drafts) // don't show as excerpt if it's draft { var ex = BlogHelper.GetBlogPostExcerpt(post.Body); post.Body = ex.Item1; post.ShowReadMore = ex.Item2; } } } return postList; }
public PostList GetPosts(PostQuery postQuery) { postList.Reverse(); return new PostList(postList, 10) { TotalPosts = postList.Count }; }
/// <summary> /// Returns posts tagged with the tag. /// </summary> /// <param name="nav"></param> /// <param name="tagName">Will be url encoded here.</param> /// <param name="pageIndex"></param> /// <returns></returns> /// <remarks> /// - Posts for a tag are not cached. <br /> /// - tagSlug will be encoded here. /// </remarks> public static PostList GetPostsForTag(Nav nav, string tagName, int page = 1) { if (string.IsNullOrWhiteSpace(tagName)) { throw new SiteException(); } PostQuery query = new PostQuery(nav, QueryBy.Tag, page); query.TagName = WebHelper.UrlEncode(tagName); // url encode query.Cacheable = false; return _GetPosts(query); }
/// <summary> /// Returns posts for index page for the specific nav. /// </summary> /// <param name="navSlug"></param> /// <returns></returns> public static PostList GetPostsForIndex(Nav nav, int page = 1) { PostQuery query = new PostQuery(nav, QueryBy.Index, page); // cache only first page on index page if (page == 0) { query.Cacheable = true; query.CacheKey = BlogHelper.GetCacheKey_Posts(nav.Slug, QueryBy.Index); query.CacheTime = new TimeSpan(0, 1, 0); } return _GetPosts(query); }
/// <summary> /// Returns posts for drafts page for nav. /// </summary> /// <param name="navSlug"></param> /// <returns></returns> /// <remarks> /// Draft posts are not cached. /// </remarks> public static PostList GetPostsForDrafts(Nav nav, int page = 1) { PostQuery query = new PostQuery(nav, QueryBy.Drafts, page); //query.PageSize = BlogConfig.Admin_PostList_PageSize; query.Cacheable = false; return _GetPosts(query); }
/// <summary> /// /// </summary> /// <param name="nav"></param> /// <param name="page"></param> /// <returns></returns> public static PostList GetPostsForArticles(Nav nav, int page = 1) { PostQuery query = new PostQuery(nav, QueryBy.Article, page); query.Cacheable = false; return _GetPosts(query); }
/// <summary> /// /// </summary> /// <param name="navSlug"></param> /// <param name="year"></param> /// <param name="month"></param> /// <param name="pageIndex"></param> /// <returns></returns> /// <remarks>Posts for an archive are not cached.</remarks> public static PostList GetPostsForArchive(Nav nav, int? year, int? month, int page = 1) { if (!year.HasValue) throw new SiteException("Year must be provided."); //if (year.Value <= 0 || month.Value <= 0) throw new SiteException(); PostQuery query = new PostQuery(nav, QueryBy.Archive, page); query.Year = year.Value; query.Month = month; query.Cacheable = false; return _GetPosts(query); }