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
            });
        }
        public async Task <BlogPost?> GetPostAsync(string slug)
        {
            var builder = new MicroCmsQueryBuilder <BlogPostEntity>()
                          .ContentIdIs(slug);

            try
            {
                var entity = await _client.GetContentAsync(builder);

                return(entity?.ToBlogPost());
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to fetch from microCMS");

                return(null);
            }
        }
Exemplo n.º 3
0
 private string CreateEndpoint <T>(MicroCmsQueryBuilder <T> queryBuilder)
 => $"{_endpoint.TrimEnd('/')}/{queryBuilder.Build()}";
Exemplo n.º 4
0
 public Task <TContent?> GetContentAsync <TContent>(
     MicroCmsQueryBuilder <TContent> queryBuilder)
 => GetAsyncCore <TContent>(CreateEndpoint(queryBuilder));
Exemplo n.º 5
0
 public Task <MicroCmsCollection <TContent>?> GetContentsAsync <TContent>(
     MicroCmsQueryBuilder <TContent> queryBuilder)
 => GetAsyncCore <MicroCmsCollection <TContent> >(CreateEndpoint(queryBuilder));