Ejemplo n.º 1
0
        public async Task <PagedPostCollection> GetPagedPostsAsync(int page, int postsPerPage)
        {
            IList <PostContent>?postContents;
            var count = 0;

            try
            {
                var queryBuilder = new StrapiQueryBuilder <PostContent>()
                                   .Start(postsPerPage * page)
                                   .Limit(postsPerPage)
                                   .OrderByDescending(m => m.PublishedAt);
                postContents = await _client.GetAsync(queryBuilder);

                count = await _client.CountAsync();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to fetch from Strapi");
                postContents = null;
            }

            if (postContents == null)
            {
                return(PagedPostCollection.Empty(postsPerPage));
            }

            return(new PagedPostCollection
            {
                Posts = postContents.Select(m => m.ToBlogPost(_baseUrl)).ToList().AsReadOnly(),
                CurrentPage = page,
                TotalPosts = count,
                PostsPerPage = postsPerPage
            });
        }
Ejemplo n.º 2
0
        public async Task <PagedPostCollection> GetPagedPostsAsync(int page, int postsPerPage)
        {
            PostsResponse <PostContent>?postsResponses;

            try
            {
                var queryBuilder = new GhostQueryBuilder <PostContent>()
                                   .Limit(postsPerPage)
                                   .Page(page + 1)
                                   .OrderByDescending(m => m.PublishedAt);
                postsResponses = await _client.GetPostsAsync(queryBuilder);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to fetch from Ghost");
                postsResponses = null;
            }

            if (postsResponses?.Posts == null)
            {
                return(PagedPostCollection.Empty(postsPerPage));
            }

            return(Utils.ToPagedPostCollection(postsResponses));
        }
Ejemplo n.º 3
0
        public async Task <PagedPostCollection> GetPagedPostsAsync(int page, int postsPerPage)
        {
            var builder = new MicroCmsQueryBuilder <BlogPostEntity>()
                          .Limit(postsPerPage)
                          .Offset(postsPerPage * page)
                          .OrderByDescending(m => m.PublishedAt);

            MicroCmsCollection <BlogPostEntity>?entries;

            try
            {
                entries = await _client.GetContentsAsync(builder);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to fetch from microCMS");
                return(PagedPostCollection.Empty(postsPerPage));
            }

            if (entries == null)
            {
                return(PagedPostCollection.Empty(postsPerPage));
            }

            return(new PagedPostCollection
            {
                Posts = entries.Contents.Select(m => m.ToBlogPost()).ToList().AsReadOnly(),
                CurrentPage = page,
                TotalPosts = entries.TotalCount,
                PostsPerPage = postsPerPage
            });
        }
Ejemplo n.º 4
0
        public async Task <PagedPostCollection> GetPagedPostsAsync(int page, int postsPerPage)
        {
            var builder = new QueryBuilder <BlogPostEntry>()
                          .ContentTypeIs("blogPost")
                          .Skip(postsPerPage * page)
                          .Limit(postsPerPage);

            ContentfulCollection <BlogPostEntry>?entries;

            try
            {
                entries = await _client.GetEntries(builder);
            }
            catch (ContentfulException e)
            {
                _logger.LogError(e, "Failed to fetch from Contentful");
                return(PagedPostCollection.Empty(postsPerPage));
            }

            var list = new List <BlogPost>();

            foreach (var entry in entries.Items)
            {
                list.Add(await entry.ToBlogPostAsync());
            }

            return(new PagedPostCollection
            {
                Posts = list.AsReadOnly(),
                CurrentPage = page,
                TotalPosts = entries.Total,
                PostsPerPage = postsPerPage
            });
        }
Ejemplo n.º 5
0
        public async Task GetPagedPostsAsync_ReturnsCorrectly()
        {
            var getAsyncReturn = Task.FromResult(new MicroCmsCollection <BlogPostEntity>
            {
                Contents   = new [] { TestData.BlogPostEntity },
                Limit      = 5,
                Offset     = 15,
                TotalCount = 16
            });
            var client = Mock.Of <IMicroCmsClient>(m =>
                                                   m.GetContentsAsync(It.IsAny <MicroCmsQueryBuilder <BlogPostEntity> >())
                                                   == getAsyncReturn);

            var subject = CreateMicroCmsBlogRepository(client);

            // Act
            var actual = await subject.GetPagedPostsAsync(3, 5);

            var expected = new PagedPostCollection
            {
                CurrentPage  = 3,
                PostsPerPage = 5,
                TotalPosts   = 16,
                Posts        = new[] { TestData.BlogPost }
            };

            Assert.Equal(expected.CurrentPage, actual.CurrentPage);
            Assert.Equal(expected.TotalPosts, actual.TotalPosts);
            Assert.Equal(expected.PostsPerPage, actual.PostsPerPage);
            Assert.Equal(expected.Posts, actual.Posts);
        }
Ejemplo n.º 6
0
        public async Task GetPagedPostsAsync_ReturnsCorrectly()
        {
            var sendQueryAsyncReturn = Task.FromResult(new GraphQLResponse <PagedPostsResponse>
            {
                Data = TestData.PagedPostsResponse
            });

            var client = Mock.Of <IGraphCmsClient>(m =>
                                                   m.SendQueryAsync <PagedPostsResponse>(It.IsAny <string>(), It.IsAny <object>()) == sendQueryAsyncReturn);

            var subject = CreateGraphCmsBlogRepository(client);

            // Act
            var actual = await subject.GetPagedPostsAsync(2, 5);

            var expected = new PagedPostCollection
            {
                CurrentPage  = 2,
                PostsPerPage = 5,
                TotalPosts   = 1,
                Posts        = new[] { TestData.BlogPost }
            };

            Assert.Equal(expected.CurrentPage, actual.CurrentPage);
            Assert.Equal(expected.TotalPosts, actual.TotalPosts);
            Assert.Equal(expected.PostsPerPage, actual.PostsPerPage);
            Assert.Equal(expected.Posts, actual.Posts);
        }
        public void Empty_ReturnsCorrectly()
        {
            var actual = PagedPostCollection.Empty(5);

            Assert.Equal(0, actual.TotalPosts);
            Assert.Equal(0, actual.CurrentPage);
            Assert.Equal(5, actual.PostsPerPage);
            Assert.Equal(Array.Empty <BlogPost>(), actual.Posts);
        }
        public void TotalPages_ReturnsCorrectValue(int totalPosts, int postsPerPage, int expected)
        {
            var pagedPosts = new PagedPostCollection
            {
                TotalPosts   = totalPosts,
                PostsPerPage = postsPerPage,
            };

            Assert.Equal(expected, pagedPosts.TotalPages);
        }
        public void HasNextPage_CurrentPageLessThanLastPage_ReturnsTrue()
        {
            var pagedPosts = new PagedPostCollection
            {
                TotalPosts   = 100,
                PostsPerPage = 10,
                CurrentPage  = 8
            };

            Assert.True(pagedPosts.HasNextPage);
        }
        public void HasPrevPage_CurrentPageNotGreaterThan0_ReturnsFalse()
        {
            var pagedPosts = new PagedPostCollection
            {
                TotalPosts   = 100,
                PostsPerPage = 10,
                CurrentPage  = 0
            };

            Assert.False(pagedPosts.HasPrevPage);
        }
        public void HasPrevPage_CurrentPageNotLessThanLastPage_ReturnsFalse()
        {
            var pagedPosts = new PagedPostCollection
            {
                TotalPosts   = 100,
                PostsPerPage = 10,
                CurrentPage  = 9
            };

            Assert.False(pagedPosts.HasNextPage);
        }
Ejemplo n.º 12
0
        public IndexTests()
        {
            _ctx        = new TestContext();
            _pagedPosts = new PagedPostCollection
            {
                Posts        = new[] { new BlogPost("Post #1", "post-1", "body", new DateTime(2021, 1, 1)) },
                TotalPosts   = 1,
                PostsPerPage = 5
            };

            _ctx.Services.AddSingleton(
                Mock.Of <IBlogService>(m =>
                                       m.GetPagedPostsAsync(It.IsAny <int>(), It.IsAny <int>()) == Task.FromResult(_pagedPosts)));
            _ctx.Services.AddSingleton(Mock.Of <IUriGenerator>());
        }
Ejemplo n.º 13
0
        public void ToPagedPostCollection_CreatesPagedPostCollection()
        {
            var postsResponse = TestData.PostsResponse;

            var actual = Utils.ToPagedPostCollection(postsResponse);

            var expected = new PagedPostCollection
            {
                Posts        = new [] { TestData.BlogPost },
                CurrentPage  = 2,
                TotalPosts   = 7,
                PostsPerPage = 1
            };

            Assert.Equal(expected.Posts, actual.Posts);
            Assert.Equal(expected.CurrentPage, actual.CurrentPage);
            Assert.Equal(expected.TotalPages, actual.TotalPages);
            Assert.Equal(expected.PostsPerPage, actual.PostsPerPage);
        }
Ejemplo n.º 14
0
        public async Task GetPagedPostsAsync_ReturnsCorrectly()
        {
            var getEntriesReturn = Task.FromResult(
                new ContentfulCollection <BlogPostEntry>
            {
                Items = new[] { TestData.BlogPostEntry },
                Limit = 5,
                Skip  = 10,
                Total = 11
            });

            Expression <Func <QueryBuilder <BlogPostEntry>, bool> > expectedQuery = (b) =>
                                                                                    b.Build()
                                                                                    .AsQueryString()
                                                                                    .Contains(new NameValueCollection()
            {
                { "content_type", "blogPost" },
                { "skip", "10" },
                { "limit", "5" },
            });
            var client = Mock.Of <IContentfulClient>(m =>
                                                     m.GetEntries(It.Is(expectedQuery), It.IsAny <CancellationToken>())
                                                     == getEntriesReturn);

            var subject = CreateContentfulBlogRepository(client);

            // Act
            var actual = await subject.GetPagedPostsAsync(2, 5);

            var expected = new PagedPostCollection
            {
                CurrentPage  = 2,
                PostsPerPage = 5,
                TotalPosts   = 11,
                Posts        = new[] { TestData.BlogPost }
            };

            Assert.Equal(expected.CurrentPage, actual.CurrentPage);
            Assert.Equal(expected.TotalPosts, actual.TotalPosts);
            Assert.Equal(expected.PostsPerPage, actual.PostsPerPage);
            Assert.Equal(expected.Posts, actual.Posts);
        }
        public async Task GetPagedPostsAsync_ReturnsCorrectly()
        {
            var getAsyncReturn   = Task.FromResult((IList <PostContent>) new [] { TestData.PostContent });
            var countAsyncReturn = Task.FromResult(1);

            Expression <Func <StrapiQueryBuilder <PostContent>, bool> > expectedQuery = b =>
                                                                                        b.Build()
                                                                                        .AsQueryString()
                                                                                        .Contains(new NameValueCollection
            {
                { "_start", "10" },
                { "_limit", "5" },
                { "_sort", "published_at:DESC" },
            });

            var strapiClient = Mock.Of <IStrapiClient>(m =>
                                                       m.GetAsync(It.Is(expectedQuery)) == getAsyncReturn &&
                                                       m.CountAsync() == countAsyncReturn);

            var subject = CreateRepository(strapiClient);

            // Act
            var actual = await subject.GetPagedPostsAsync(2, 5);

            var expected = new PagedPostCollection
            {
                CurrentPage  = 2,
                PostsPerPage = 5,
                TotalPosts   = 1,
                Posts        = new[] { TestData.BlogPost }
            };

            Assert.Equal(expected.CurrentPage, actual.CurrentPage);
            Assert.Equal(expected.TotalPosts, actual.TotalPosts);
            Assert.Equal(expected.PostsPerPage, actual.PostsPerPage);
            Assert.Equal(expected.Posts, actual.Posts);
        }
Ejemplo n.º 16
0
        public async Task GetPagedPostsAsync_ReturnsCorrectly()
        {
            var getPostsAsyncReturn = Task.FromResult(TestData.PostsResponse);

            Expression <Func <GhostQueryBuilder <PostContent>, bool> > expectedQuery = b =>
                                                                                       b.Build()
                                                                                       .AsQueryString()
                                                                                       .Contains(new NameValueCollection
            {
                { "fields", "title,slug,html,published_at" },
                { "limit", "5" },
                { "page", "3" },
                { "order", "published_at DESC" },
            });

            var client = Mock.Of <IGhostClient>(m =>
                                                m.GetPostsAsync(It.Is(expectedQuery)) == getPostsAsyncReturn);

            var subject = CreateRepository(client);

            // Act
            var actual = await subject.GetPagedPostsAsync(2, 5);

            var expected = new PagedPostCollection
            {
                CurrentPage  = 2,
                PostsPerPage = 1,
                TotalPosts   = 7,
                Posts        = new[] { TestData.BlogPost }
            };

            Assert.Equal(expected.CurrentPage, actual.CurrentPage);
            Assert.Equal(expected.TotalPosts, actual.TotalPosts);
            Assert.Equal(expected.PostsPerPage, actual.PostsPerPage);
            Assert.Equal(expected.Posts, actual.Posts);
        }
Ejemplo n.º 17
0
        public async Task <PagedPostCollection> GetPagedPostsAsync(int page, int postsPerPage)
        {
            var skip  = postsPerPage * page;
            var first = postsPerPage;
            GraphQLResponse <PagedPostsResponse>?response = null;

            try
            {
                response = await _client.SendQueryAsync <PagedPostsResponse>(PagedPostsQuery, new { skip, first });
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to fetch from GraphCMS");
            }

            if (response?.Data.PostsConnection?.Edges == null ||
                response.Data.PostsConnection.Aggregate?.Count == null)
            {
                return(PagedPostCollection.Empty(postsPerPage));
            }

            var posts = ToBlogPosts(response.Data.PostsConnection.Edges);

            if (posts == null)
            {
                return(PagedPostCollection.Empty(postsPerPage));
            }

            return(new PagedPostCollection
            {
                Posts = posts,
                CurrentPage = page,
                TotalPosts = response.Data.PostsConnection.Aggregate.Count.Value,
                PostsPerPage = postsPerPage
            });
        }