Esempio 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
            });
        }
Esempio n. 2
0
        public async Task <IList <T>?> GetAsync <T>(StrapiQueryBuilder <T> queryBuilder)
        {
            var endpoint = _baseEndpoint + queryBuilder.Build();
            var response = await _httpClient.GetAsync(endpoint);

            CheckStatusCode(response.StatusCode);

            try
            {
                return(await response.Content.ReadFromJsonAsync <IList <T> >());
            }
            catch (JsonException e)
            {
                throw new InvalidOperationException("The response body was unknown format", e);
            }
        }
Esempio n. 3
0
        public async Task <BlogPost?> GetPostAsync(string slug)
        {
            IList <PostContent>?postContents;

            try
            {
                var queryBuilder = new StrapiQueryBuilder <PostContent>()
                                   .Eq(m => m.Slug, slug)
                                   .Limit(1);
                postContents = await _client.GetAsync(queryBuilder);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to fetch from Strapi");
                return(null);
            }

            return(postContents?.FirstOrDefault()?.ToBlogPost(_baseUrl));
        }