-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web: Set up ContentfulService for fetching Contentful CDA
- Loading branch information
1 parent
609386c
commit ca9f60a
Showing
6 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Contentful.Core.Configuration; | ||
|
||
namespace Web.Configuration.Contentful | ||
{ | ||
/// <summary> | ||
/// Resolves a strong type from a content type id. Instructing the serialization engine how to deserialize items in a collection. | ||
/// </summary> | ||
public class ContentfulModuleResolver : IContentTypeResolver | ||
{ | ||
private Dictionary<string, Type> _types = new Dictionary<string, Type>() | ||
{ | ||
}; | ||
|
||
/// <summary> | ||
/// Method to get a type based on the specified content type id. | ||
/// </summary> | ||
/// <param name="contentTypeId">The content type id to resolve to a type.</param> | ||
/// <returns>The type for the content type id or null if none is found.</returns> | ||
public Type Resolve(string contentTypeId) | ||
{ | ||
return _types.TryGetValue(contentTypeId, out var type) ? type : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Contentful.Core; | ||
|
||
namespace Web.Services | ||
{ | ||
public interface IContentfulService | ||
{ | ||
|
||
Task<T> GetFirstByContentType<T>(string contentTypeId) where T : class; | ||
} | ||
|
||
public class ContentfulService : IContentfulService | ||
{ | ||
private readonly IContentfulClient _contentfulClient; | ||
|
||
public ContentfulService(IContentfulClient contentfulClient) | ||
{ | ||
_contentfulClient = contentfulClient; | ||
_contentfulClient.ContentTypeResolver = new Configuration.Contentful.ContentfulModuleResolver(); | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@using Web | ||
@using Web.Models | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers | ||
@addTagHelper *, Contentful.AspNetCore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters