/// <summary>
        /// Get content of specified type using ContentType attribute of a type T
        /// </summary>
        /// <param name="client">Contentful client</param>
        /// <param name="queryBuilder">QueryBuilder to filter entries</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <typeparam name="T"></typeparam>
        /// <returns>Entiries for specified content type</returns>
        public static Task <ContentfulCollection <T> > GetContentForTypeAsync <T>(this IContentfulClient client, QueryBuilder <T> queryBuilder = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var contentTypeDefinition = typeof(T).GetTypeInfo()
                                        .GetCustomAttributes <ContentTypeAttribute>().Single();

            return(client.GetEntriesByType <T>(contentTypeDefinition.ContentTypeId, queryBuilder, cancellationToken));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index()
        {
            //var qb = QueryBuilder<Posts>.New.FieldEquals(f => f.sys.Id, id);
            var allPosts = await _client.GetEntriesByType <Posts>("post");

            var orderedPosts = allPosts.OrderByDescending(Posts => Posts.PublishDate);

            ViewData["Title"] = "Home";
            return(View(orderedPosts));
        }
Exemplo n.º 3
0
        public async Task <T> GetFirstByContentType <T>(string contentTypeId) where T : class
        {
            var queryBuilder = new Contentful.Core.Search.QueryBuilder <T>().Limit(1);
            var resp         = await _contentfulClient.GetEntriesByType(contentTypeId, queryBuilder);

            if (resp.Total == 0)
            {
                return(null);
            }
            return(new List <T>(resp.Items).First());
        }
        public static IContentfulClient WithFakeContent <TContent>(this IContentfulClient client, params TContent[] content)
        {
            var response = new ContentfulCollection <TContent> {
                Items = content
            };

            A.CallTo(() => client.GetEntriesByType(A <string> .Ignored, A <QueryBuilder <TContent> > .Ignored, A <CancellationToken> .Ignored))
            .ReturnsLazily(() => response);

            return(client);
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        protected override async Task <IEnumerable <IDocument> > ExecuteContextAsync(IExecutionContext context)
        {
            var qb = QueryBuilder <TContentModel> .New;

            ConfigureQueryBuilder?.Invoke(qb);

            var items = await _client.GetEntriesByType(_contentTypeId, qb);

            var documents = items.Items
                            .OfType <TContentModel>()
                            .Select(item => ContentfulDocumentHelpers.CreateDocument(context, item, GetContent))
                            .ToArray();

            return(documents);
        }
Exemplo n.º 6
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var projects = await _client.GetEntriesByType <Projects>("projects");

            return(View(projects));
        }