Пример #1
0
        /// <summary>
        /// Gets post list based on selection for current page
        /// </summary>
        /// <param name="page">Current page</param>
        /// <param name="postType">Selected post type: draft, published or all</param>
        /// <param name="filter">Secondary filter: category, tag, author or all</param>
        /// <param name="title">Value selected in secondary filter</param>
        /// <returns>List of posts</returns>
        public static List<JsonPost> GetPosts(int page, string postType, string filter, string title)
        {
            var pSize = BlogSettings.Instance.PostsPerPage;
            var cntTo = page * pSize;
            var cntFrom = cntTo - pSize;
            var cnt = 0;

            var allPosts = new List<Post>();
            var filteredPosts = new List<Post>();
            var pagePosts = new List<JsonPost>();

            // first filter on selected post type
            switch (postType)
            {
                case "Published":
                    allPosts = (from p in Post.Posts.ToList() where p.IsPublished == true select p).ToList();
                    break;
                case "Draft":
                    allPosts = (from p in Post.Posts where p.IsPublished == false select p).ToList();
                    break;
                default:
                    allPosts = (from p in Post.Posts select p).ToList();
                    break;
            }

            // now filter first results on secondary filter
            switch (filter)
            {
                case "Category":
                    filteredPosts = (from x in allPosts where x.Categories.Contains(Category.GetCategory(new Guid(title))) orderby x.DateCreated descending select x).ToList();
                    break;
                case "Tag":
                    filteredPosts = (from x in allPosts where x.Tags.Contains(title) orderby x.DateCreated descending select x).ToList();
                    break;
                case "Author":
                    filteredPosts = (from x in allPosts where x.Author.Equals(title) orderby x.DateCreated descending select x).ToList();
                    break;
                default:
                    filteredPosts = (from x in allPosts orderby x.DateCreated descending select x).ToList();
                    break;
            }

            // convert each post into smaller Json friendly object
            foreach (var x in filteredPosts)
            {
                cnt++;
                if (cnt <= cntFrom || cnt > cntTo)
                {
                    continue;
                }

                string tags = x.Tags.Aggregate("", (current, tag) => current + (tag + ","));

                var jp = new JsonPost
                {
                    Id = x.Id,
                    Author = GetAuthor(x.Author),
                    Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, x.Title),
                    Date = x.DateCreated.ToString("dd MMM yyyy"),
                    Time = x.DateCreated.ToString("t"),
                    Categories = GetCategories(x.Categories),
                    Tags = GetTags(x.Tags),
                    Comments = GetComments(x.Comments, x.RelativeLink),
                    IsPublished = x.IsPublished,
                    CanUserEdit = x.CanUserEdit,
                    CanUserDelete = x.CanUserDelete
                };
                pagePosts.Add(jp);
            }

            currentPage = page;
            postCnt = cnt;

            return pagePosts;
        }
Пример #2
0
        /// <summary>
        /// Gets post list based on selection for current page
        /// </summary>
        /// <param name="page">Current page</param>
        /// <param name="postType">Selected post type: draft, published or all</param>
        /// <param name="filter">Secondary filter: category, tag, author or all</param>
        /// <param name="title">Value selected in secondary filter</param>
        /// <returns>List of posts</returns>
        public static List <JsonPost> GetPosts(int page, string postType, string filter, string title)
        {
            var pSize   = BlogSettings.Instance.PostsPerPage;
            var cntTo   = page * pSize;
            var cntFrom = cntTo - pSize;
            var cnt     = 0;

            var allPosts      = new List <Post>();
            var filteredPosts = new List <Post>();
            var pagePosts     = new List <JsonPost>();

            // first filter on selected post type
            switch (postType)
            {
            case "Published":
                allPosts = (from p in Post.Posts.ToList() where p.IsPublished == true select p).ToList();
                break;

            case "Draft":
                allPosts = (from p in Post.Posts where p.IsPublished == false select p).ToList();
                break;

            default:
                allPosts = (from p in Post.Posts select p).ToList();
                break;
            }

            // now filter first results on secondary filter
            switch (filter)
            {
            case "Category":
                filteredPosts = (from x in allPosts where x.Categories.Contains(Category.GetCategory(new Guid(title))) orderby x.DateCreated descending select x).ToList();
                break;

            case "Tag":
                filteredPosts = (from x in allPosts where x.Tags.Contains(title) orderby x.DateCreated descending select x).ToList();
                break;

            case "Author":
                filteredPosts = (from x in allPosts where x.Author.Equals(title) orderby x.DateCreated descending select x).ToList();
                break;

            default:
                filteredPosts = (from x in allPosts orderby x.DateCreated descending select x).ToList();
                break;
            }

            // convert each post into smaller Json friendly object
            foreach (var x in filteredPosts)
            {
                cnt++;
                if (cnt <= cntFrom || cnt > cntTo)
                {
                    continue;
                }

                string tags = x.Tags.Aggregate("", (current, tag) => current + (tag + ","));

                var jp = new JsonPost
                {
                    Id            = x.Id,
                    Author        = GetAuthor(x.Author),
                    Title         = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, x.Title),
                    Date          = x.DateCreated.ToString("dd MMM yyyy"),
                    Time          = x.DateCreated.ToString("t"),
                    Categories    = GetCategories(x.Categories),
                    Tags          = GetTags(x.Tags),
                    Comments      = GetComments(x.Comments, x.RelativeLink),
                    IsPublished   = x.IsPublished,
                    CanUserEdit   = x.CanUserEdit,
                    CanUserDelete = x.CanUserDelete
                };
                pagePosts.Add(jp);
            }

            currentPage = page;
            postCnt     = cnt;

            return(pagePosts);
        }