Exemple #1
0
        /// <summary>
        /// Gets the post archive for the specified archive page
        /// with the given filters applied.
        /// </summary>
        /// <param name="archiveId">The unique archive page id</param>
        /// <param name="currentPage">The optional page number</param>
        /// <param name="categoryId">The optional category id</param>
        /// <param name="tagId">The optional tag id</param>
        /// <param name="year">The optional year</param>
        /// <param name="month">The optional year</param>
        /// <param name="pageSize">The optional page size. If not provided, this will be read from config</param>
        /// <typeparam name="T">The post type</typeparam>
        /// <returns>The post archive</returns>
        public async Task <PostArchive <T> > GetByIdAsync <T>(Guid archiveId, int?currentPage = 1,
                                                              Guid?categoryId = null, Guid?tagId = null, int?year = null, int?month = null, int?pageSize = null)
            where T : Models.PostBase
        {
            var model = new PostArchive <T>();

            // Ensure page size
            if (!pageSize.HasValue)
            {
                using (var config = new Config(_paramService))
                {
                    // No page size provided, get from config
                    pageSize = config.ArchivePageSize;

                    if (!pageSize.HasValue || pageSize == 0)
                    {
                        // No config available, default to 5
                        pageSize = 5;
                    }
                }
            }

            // Set basic fields
            model.Year  = year;
            model.Month = month;

            // Get paging info
            model.TotalPosts = await _repo.GetPostCount(archiveId, categoryId, tagId, year, month).ConfigureAwait(false);

            model.TotalPages  = Math.Max(Convert.ToInt32(Math.Ceiling((double)model.TotalPosts / pageSize.Value)), 1);
            model.CurrentPage = Math.Min(Math.Max(1, currentPage.HasValue ? currentPage.Value : 1), model.TotalPages);

            // Set related info
            if (categoryId.HasValue)
            {
                model.Category = await _postService.GetCategoryByIdAsync(categoryId.Value);
            }
            if (tagId.HasValue)
            {
                model.Tag = await _postService.GetTagByIdAsync(tagId.Value);
            }

            // Get the id of the current posts
            var posts = await _repo.GetPosts(archiveId, pageSize.Value, model.CurrentPage, categoryId, tagId, year, month).ConfigureAwait(false);

            // Get the posts
            foreach (var postId in posts)
            {
                var post = await _postService.GetByIdAsync <T>(postId).ConfigureAwait(false);

                if (post != null)
                {
                    model.Posts.Add(post);
                }
            }
            return(model);
        }