Skip to content

Commit

Permalink
Web: Set up ContentfulService for fetching Contentful CDA
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonho215 committed Feb 14, 2021
1 parent 609386c commit ca9f60a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Front Line Live code and target operating model
|APP_DATACONTEXT*|-|
|WEB_USERSECRETSID*|-|
|SQL_ADMIN_PASSWORD*|-|
|CONTENTFUL_DELIVERY_ACCESS_TOKEN|Content Delivery API - access token|
|CONTENTFUL_SPACE_ID|Contentful Space ID|

## Local development

Expand Down
26 changes: 26 additions & 0 deletions Web/Configuration/Contentful/ModuleResolver.cs
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;
}
}
}
36 changes: 36 additions & 0 deletions Web/Services/IContentfulService.cs
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();
}
}
}
7 changes: 6 additions & 1 deletion Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Contentful.AspNetCore;
using Web.Db;
using Web.Infrastructure;
using Web.Snippets;
using Web.Snippets.Microsoft.Extensions.Configuration;
using Web.Services;

namespace Web
{
Expand Down Expand Up @@ -48,7 +50,9 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton<IEmailSender, IdentityEmailSender>();

services.AddRazorPages()
services.AddContentful(_configuration);

services.AddRazorPages()
.AddRazorPagesOptions(options => //locks down following folder and page for only users that are logged in (regardless of Role)
{
options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage"); //folder for managing own identity
Expand All @@ -61,6 +65,7 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddTransient<UsersFacade>();
services.AddScoped<IContentfulService, ContentfulService>();
}

public void Configure(IApplicationBuilder app)
Expand Down
1 change: 1 addition & 0 deletions Web/Views/_ViewImports.cshtml
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
4 changes: 4 additions & 0 deletions src/infra/deploy_terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ echo "{
\"ReCaptcha__SiteKey\": \"$APP_RECAPTCHA_SITEKEY\",
\"APP_DATACONTEXT\": \"$APP_DATACONTEXT\",
\"WEB_USERSECRETSID\": \"$WEB_USERSECRETSID\"
\"ContentfulOptions__DeliveryApiKey\": \"$CONTENTFUL_DELIVERY_ACCESS_TOKEN\",
\"ContentfulOptions__SpaceId\": \"$CONTENTFUL_SPACE_ID\",
\"ContentfulOptions__Environment\": \"$ENVIRONMENT_NAME\",
\"ContentfulOptions__UsePreviewApi\": false
}" > ./app-settings.json

echo "Initializing Terraform..."
Expand Down

0 comments on commit ca9f60a

Please sign in to comment.