Skip to content

Commit

Permalink
Added initial Contentful setup (#2)
Browse files Browse the repository at this point in the history
* Added initial contentful setup and dummy models & views to test the integration

* Added a custom renderer for Unordered lists as a POC
  • Loading branch information
sam-c-dfe authored Jan 23, 2024
1 parent cb87964 commit d3f2f29
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Dfe.EarlyYearsQualification.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{CE49B579
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dfe.EarlyYearsQualification.UnitTests", "tests\Dfe.EarlyYearsQualification.UnitTests\Dfe.EarlyYearsQualification.UnitTests.csproj", "{C989778A-398C-4092-97C4-4BCF5C233918}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dfe.EarlyYearsQualification.Content", "src\Dfe.EarlyYearsQualification.Content\Dfe.EarlyYearsQualification.Content.csproj", "{4DEA254F-34B6-47BA-A61A-83872A42BFD5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,9 +30,14 @@ Global
{C989778A-398C-4092-97C4-4BCF5C233918}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C989778A-398C-4092-97C4-4BCF5C233918}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C989778A-398C-4092-97C4-4BCF5C233918}.Release|Any CPU.Build.0 = Release|Any CPU
{4DEA254F-34B6-47BA-A61A-83872A42BFD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DEA254F-34B6-47BA-A61A-83872A42BFD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DEA254F-34B6-47BA-A61A-83872A42BFD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DEA254F-34B6-47BA-A61A-83872A42BFD5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{73200EBC-285A-499F-9489-AD3AB7D07D52} = {5ED920E6-E35D-4D6A-8CBE-C76D493EDEE4}
{C989778A-398C-4092-97C4-4BCF5C233918} = {CE49B579-76F7-4B4B-B61A-8059FE2F2BAB}
{4DEA254F-34B6-47BA-A61A-83872A42BFD5} = {5ED920E6-E35D-4D6A-8CBE-C76D493EDEE4}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="contentful.csharp" Version="7.5.0" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/Dfe.EarlyYearsQualification.Content/Entities/LandingPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Contentful.Core.Models;

namespace Dfe.EarlyYearsQualification.Content.Entities;

public class LandingPage
{
public string Header { get; set; } = string.Empty;

public Document? ServiceIntroduction { get; set; }

public string ServiceIntroductionHtml { get; set; } = string.Empty;

public string StartButtonText { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text;
using Contentful.Core.Models;

namespace Dfe.EarlyYearsQualification.Content.Renderers;

public class UnorderedListRenderer : IContentRenderer
{
public int Order { get; set; }

public Task<string> RenderAsync(IContent content)
{
var list = content as List;
var sb = new StringBuilder();
sb.Append("<ul class=\"govuk-list govuk-list--bullet\">");
foreach (IContent listItem in list!.Content)
{
var listItemValue = listItem as ListItem;
var listItemParagraph = listItemValue!.Content[0] as Paragraph;
var listItemText = listItemParagraph!.Content[0] as Text;
sb.Append($"<li>{listItemText!.Value}</li>");
}
sb.Append("</ul>");
return Task.FromResult(sb.ToString());
}

public bool SupportsContent(IContent content)
{
return content is List && (content as List)!.NodeType == "unordered-list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Contentful.Core;
using Contentful.Core.Models;
using Dfe.EarlyYearsQualification.Content.Entities;
using Dfe.EarlyYearsQualification.Content.Renderers;

namespace Dfe.EarlyYearsQualification.Content.Services;

public class ContentfulContentService : IContentService
{
private readonly IContentfulClient _contentfulClient;

public ContentfulContentService(IContentfulClient contentfulClient)
{
_contentfulClient = contentfulClient;
}

public async Task<LandingPage> GetLandingPage()
{
var landingPageEntries = await _contentfulClient.GetEntriesByType<LandingPage>("landingPage");
var landingPageContent = landingPageEntries.First();
var htmlRenderer = new HtmlRenderer();
htmlRenderer.AddRenderer(new UnorderedListRenderer() { Order = 10 });
landingPageContent.ServiceIntroductionHtml = await htmlRenderer.ToHtml(landingPageContent.ServiceIntroduction);
return landingPageContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Dfe.EarlyYearsQualification.Content.Entities;

namespace Dfe.EarlyYearsQualification.Content.Services;

public interface IContentService
{
Task<LandingPage> GetLandingPage();
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Dfe.EarlyYearsQualification.Web.Models;
using Dfe.EarlyYearsQualification.Content.Services;
using Dfe.EarlyYearsQualification.Web.Models.Content;

namespace Dfe.EarlyYearsQualification.Web.Controllers;

public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IContentService _contentService;

public HomeController(ILogger<HomeController> logger)
public HomeController(ILogger<HomeController> logger, IContentService contentService)
{
_logger = logger;
_contentService = contentService;
}

[HttpGet]
public IActionResult Index()
{
return View();
}

[HttpGet]
public async Task<IActionResult> Content()
{
var landingPageContent = await _contentService.GetLandingPage();
var model = new LandingPageModel()
{
Header = landingPageContent.Header,
ServiceIntroduction = landingPageContent.ServiceIntroductionHtml,
StartButtonText = landingPageContent.StartButtonText
};
return View(model);
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="contentful.aspnetcore" Version="7.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dfe.EarlyYearsQualification.Content\Dfe.EarlyYearsQualification.Content.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Dfe.EarlyYearsQualification.Web.Models.Content;

public class LandingPageModel
{
public string Header { get; set; } = string.Empty;

public string ServiceIntroduction { get; set; } = string.Empty;

public string StartButtonText { get; set; } = string.Empty;
}
6 changes: 6 additions & 0 deletions src/Dfe.EarlyYearsQualification.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using Contentful.AspNetCore;
using Dfe.EarlyYearsQualification.Content.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddContentful(builder.Configuration);
builder.Services.AddTransient<IContentService, ContentfulContentService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
11 changes: 11 additions & 0 deletions src/Dfe.EarlyYearsQualification.Web/Views/Home/Content.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@model Dfe.EarlyYearsQualification.Web.Models.Content.LandingPageModel

@{
ViewData["Title"] = "Content Page";
}

<div class="text-center">
<h1 class="display-4">@Model.Header</h1>
@Html.Raw(Model.ServiceIntroduction)
<button>@Model.StartButtonText</button>
</div>
7 changes: 7 additions & 0 deletions src/Dfe.EarlyYearsQualification.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
"Microsoft.AspNetCore": "Warning"
}
},
"ContentfulOptions": {
"DeliveryApiKey": "",
"PreviewApiKey": "",
"SpaceId": "",
"UsePreviewApi": false,
"MaxNumberOfRateLimitRetries": 1
},
"AllowedHosts": "*"
}

0 comments on commit d3f2f29

Please sign in to comment.