示例#1
0
        public IActionResult GetBlogPosts(BlogPostsFilter filter)
        {
            IEnumerable <PostDto> posts  = _blogQueries.GetBlogPostsAsync(out int numberOfRecords, filter);
            PagedDataResult       result = new PagedDataResult(posts, numberOfRecords);

            return(Ok(result));
        }
        public IEnumerable <BlogPostDto> GetBlogPostsAsync(out int totalRecords, BlogPostsFilter filter)
        {
            totalRecords = 0;
            if (filter.BlogId < 1)
            {
                throw new ArgumentOutOfRangeException("filter.BlogId", "The filter.BlogId is invalid.");
            }
            if (filter.Skip < 0)
            {
                throw new ArgumentOutOfRangeException("filter.Skip", "The filter.Skip number must be 0 or greater.");
            }
            if (filter.Take < 1)
            {
                throw new ArgumentOutOfRangeException("filter.Take", "The filter.Take must be greater than zero.");
            }

            var entities = Context.BlogPost
                           .Where(i => i.BlogId == filter.BlogId)
                           .Select(i => i.Post);

            if (!string.IsNullOrEmpty(filter.Title))
            {
                entities = entities.Where(i => EF.Functions.Like(i.Title, $"%{filter.Title}%"));
            }

            totalRecords = entities.Count();
            if (totalRecords <= filter.Skip)
            {
                filter.Skip = 0;
            }

            entities = entities.ApplySortToEntities(filter.SortMembers);

            List <BlogPostDto> blogPosts = entities.Skip(filter.Skip).Take(filter.Take)
                                           .ProjectToType <BlogPostDto>().ToList();

            blogPosts.ForEach(i => {
                i.BlogId = filter.BlogId;
            });

            return(blogPosts);
        }
        public void BlogPostCRUD()
        {
            Task.Run(async() =>
            {
                int primaryAuthorId = 1;
                string blogName01   = "testBlog01";

                var blogThatShouldNotBe = _blogRqs.GetBlog(blogName01);
                if (blogThatShouldNotBe != null)
                {
                    await _blogCmds.DeleteBlogAsync(blogThatShouldNotBe.Id);
                }

                List <string> tags = new List <string>()
                {
                    "Programming", "Art"
                };
                BlogDto blogDto = new BlogDto()
                {
                    Name            = blogName01,
                    DisplayName     = "My Test Blog",
                    BlogStatusId    = (int)BlogStatuses.Draft,
                    PrimaryAuthorId = 1
                };
                blogDto.Tags.AddRange(tags);
                await _blogCmds.AddBlogAsync(blogDto);

                List <string> postTags = new List <string>()
                {
                    "PostTag01", "PostTag02", "PostTag03", "PostTag04", "PostTag05"
                };
                BlogPostDto blogPost = new BlogPostDto()
                {
                    BlogId          = blogDto.Id,
                    CommentStatusId = (int)CommentStatuses.MemberOnly,
                    PrimaryAuthorId = primaryAuthorId,
                    PostStatusId    = (int)PostStatuses.Draft,
                    Title           = "My Test Blog01 Post Title",
                    PostContent     = "Here's my first blog post content.",
                    Excerpt         = "Test excerpt content.",
                };
                blogPost.Tags.AddRange(postTags);
                BlogPostDto testPost01 = await _blogCmds.AddBlogPostAsync(blogPost);
                Assert.IsTrue(testPost01.Id > 0);

                testPost01.PostContent  = "Here's my first blog post with updated content.";
                testPost01.PostStatusId = (int)PostStatuses.Draft;
                testPost01.Excerpt      = "Updated test excerpt";

                PostCommentDto firstComment   = await _blogCmds.AddPostComment(testPost01.Id, primaryAuthorId, "A wanted to add a few test comments.");
                PostCommentDto secondsComment = await _blogCmds.AddPostComment(testPost01.Id, primaryAuthorId, "This is a second test comment.");
                PostCommentDto subComment     = await _blogCmds.AddPostComment(testPost01.Id, primaryAuthorId, "This is a sub comment for the second test comment.", secondsComment.Id);

                BlogPostDto testPost02 = await _blogCmds.AddBlogPostAsync(new BlogPostDto()
                {
                    BlogId          = blogDto.Id,
                    PrimaryAuthorId = primaryAuthorId,
                    PostStatusId    = (int)PostStatuses.Draft,
                    Title           = "My Test Blog01 Second Post Title",
                    PostContent     = "Here's my second blog post content.",
                    Excerpt         = "Second test excerpt content.",
                });
                testPost02.Tags.AddRange(postTags);
                Assert.IsTrue(testPost01.Id > 0);

                //await _blogMng.ApprovePostComment()

                var blogPostListItem = await _blogRqs.GetAllBlogPostsAsync(blogDto.Id);
                Assert.IsTrue(blogPostListItem.Count() > 1);
                int numberOfPosts          = 0;
                BlogPostsFilter blogFilter = new BlogPostsFilter()
                {
                    BlogId = blogDto.Id, Skip = 0, Take = 1
                };
                blogFilter.SortMembers.Add(new Data.Helpers.SortDescriptor("UpdatedAt", System.ComponentModel.ListSortDirection.Ascending));
                var blogPosts = _blogRqs.GetBlogPostsAsync(out numberOfPosts, blogFilter);
                Assert.IsTrue(blogPosts.Count() == 1);

                var myTestPost = await _blogRqs.GetPostAsync(testPost01.Id);

                await _blogCmds.DeletePostAsync(testPost01.Id);
                await _blogCmds.DeletePostAsync(testPost02.Id);

                await _blogCmds.DeleteBlogAsync(blogDto.Id);
                BlogDto blog = _blogRqs.GetBlog(blogDto.Id);
                Assert.IsNull(blog);
            }).GetAwaiter().GetResult();
        }