Skip to content

Commit

Permalink
Feature/tag filtering (#133)
Browse files Browse the repository at this point in the history
* Added default page to handle base route

* Created the ability to filter content by tags

* Fixed empty reference aria label

* Updated controller to support page type

* Tidy up

* Removed spacing

* Fixed unit tests

* Moved iD
  • Loading branch information
simonjfirth authored Aug 20, 2024
1 parent 1fab005 commit 67254b6
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 102 deletions.
6 changes: 5 additions & 1 deletion src/Dfe.ContentSupport.Web/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public async Task<IActionResult> Home()
return View(defaultModel);
}



[HttpGet("{slug}/{page?}")]
public async Task<IActionResult> Index(string slug, string page = "", bool isPreview = false)
public async Task<IActionResult> Index(string slug, string page = "", bool isPreview = false, [FromQuery] List<string>? tags = null)
{
if (!ModelState.IsValid) return RedirectToAction("error");
if (string.IsNullOrEmpty(slug)) return RedirectToAction("error");
Expand All @@ -38,10 +40,12 @@ public async Task<IActionResult> Index(string slug, string page = "", bool isPre
if (resp is null) return RedirectToAction("error");

resp = layoutService.GenerateLayout(resp, Request, page);
ViewBag.tags = tags;

return View("CsIndex", resp);
}


public IActionResult Privacy()
{
return View();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Dfe.ContentSupport.Web.Controllers;
public class SitemapController(IContentService contentfulService) : Controller
{


[HttpGet]
[Route("/sitemap.xml")]
public async Task<IActionResult> Sitemap()
Expand Down
26 changes: 7 additions & 19 deletions src/Dfe.ContentSupport.Web/Dfe.ContentSupport.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,12 @@
</PropertyGroup>

<ItemGroup>
<Content Remove="appsettings.json"/>
</ItemGroup>

<ItemGroup>
<None Include="appsettings.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1"/>
<PackageReference Include="Azure.Identity" Version="1.11.4"/>
<PackageReference Include="contentful.aspnetcore" Version="7.5.1"/>
<PackageReference Include="contentful.csharp" Version="7.5.1"/>
<PackageReference Include="GovUk.Frontend.AspNetCore" Version="1.5.0"/>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="contentful.aspnetcore" Version="7.5.1" />
<PackageReference Include="contentful.csharp" Version="7.5.1" />
<PackageReference Include="GovUk.Frontend.AspNetCore" Version="1.5.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
3 changes: 1 addition & 2 deletions src/Dfe.ContentSupport.Web/Models/ContentBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace Dfe.ContentSupport.Web.Models;

[ExcludeFromCodeCoverage]
public class ContentBase : ContentType
public class ContentBase : Contentful.Core.Models.Entry<ContentBase>
{
public string InternalName { get; set; } = null!;

Expand Down
3 changes: 2 additions & 1 deletion src/Dfe.ContentSupport.Web/Models/ContentItemBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Contentful.Core.Models;
using System.Diagnostics.CodeAnalysis;

namespace Dfe.ContentSupport.Web.Models;

Expand Down
2 changes: 1 addition & 1 deletion src/Dfe.ContentSupport.Web/Models/Mapped/CsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public class CsPage
public DateTime? UpdatedAt { get; init; }
public bool HasFeedbackBanner { get; set; }
public List<PageLink>? MenuItems { get; set; }

public List<string> Tags { get;set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class RichTextContentItem : CsContentItem
public List<RichTextContentItem> Content { get; set; } = null!;
public RichTextNodeType NodeType { get; set; } = RichTextNodeType.Unknown;
public string Value { get; set; } = null!;
public List<string> Tags { get; set; } = [];
}
24 changes: 19 additions & 5 deletions src/Dfe.ContentSupport.Web/Services/ModelMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.ContentSupport.Web.Common;
using Contentful.Core.Models;
using Dfe.ContentSupport.Web.Common;
using Dfe.ContentSupport.Web.Configuration;
using Dfe.ContentSupport.Web.Models;
using Dfe.ContentSupport.Web.Models.Mapped;
Expand Down Expand Up @@ -29,12 +30,20 @@ public CsPage MapToCsPage(ContentSupportPage incoming)
HasPrint = incoming.HasPrint,
Content = MapEntriesToContent(incoming.Content),
ShowVerticalNavigation = incoming.ShowVerticalNavigation,
CreatedAt = incoming.Sys.CreatedAt,
UpdatedAt = incoming.Sys.UpdatedAt
CreatedAt = incoming.SystemProperties.CreatedAt,
UpdatedAt = incoming.SystemProperties.UpdatedAt,
Tags = FlattenMetadata(incoming.Metadata)
};
return result;
}

private List<string> FlattenMetadata(ContentfulMetadata item)
{
if (item is null) return new();

return item.Tags.Select(_ => _.Sys.Id).ToList();
}

private List<CsContentItem> MapEntriesToContent(List<Entry> entries)
{
return entries.Select(ConvertEntryToContentItem).ToList();
Expand All @@ -59,6 +68,7 @@ public CsContentItem ConvertEntryToContentItem(Entry entry)
Subtitle = entry.Subtitle,
NodeType = ConvertToRichTextNodeType(richText.NodeType),
Content = MapRichTextNodes(richText.Content),
Tags = FlattenMetadata(entry.Metadata)
};
return item;
}
Expand All @@ -69,11 +79,14 @@ public List<RichTextContentItem> MapRichTextNodes(List<ContentItem> nodes)
{ NodeType = RichTextNodeType.Unknown, InternalName = node.InternalName }).ToList();
}


public RichTextContentItem? MapContent(ContentItem contentItem)
{
RichTextContentItem? item;
var nodeType = ConvertToRichTextNodeType(contentItem.NodeType);
var internalName = contentItem.InternalName;


switch (nodeType)
{
case RichTextNodeType.Text:
Expand All @@ -84,7 +97,7 @@ public List<RichTextContentItem> MapRichTextNodes(List<ContentItem> nodes)
break;
case RichTextNodeType.Hyperlink:
var uri = contentItem.Data.Uri.ToString();
item = new Hyperlink
item = new Models.Mapped.Standard.Hyperlink
{
Uri = uri,
IsVimeo = uri.Contains("vimeo.com")
Expand Down Expand Up @@ -137,12 +150,13 @@ public List<RichTextContentItem> MapRichTextNodes(List<ContentItem> nodes)
item.Content = MapRichTextNodes(contentItem.Content);
item.Value = contentItem.Value;
item.InternalName = internalName;
item.Tags = FlattenMetadata(contentItem.Metadata);
return item;
}

public CustomComponent? GenerateCustomComponent(Target target)
{
var contentType = target.Sys.ContentType?.Sys.Id;
var contentType = target.SystemProperties.ContentType?.SystemProperties.Id;
if (contentType is null) return null;
return contentType switch
{
Expand Down
41 changes: 23 additions & 18 deletions src/Dfe.ContentSupport.Web/Views/Shared/RichText/_RichText.cshtml
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
@model RichTextContentItem

@{
var tags = ViewBag.tags ?? new List<string>();
}


@if (tags.Count == 0 || (Model.Tags.Count == 0 || Model.Tags.Exists(el => tags.Contains(el))))
{
var nodeType = Model.NodeType;

switch (nodeType)
{
case RichTextNodeType.Document:
<partial name="RichText/_Document" model="@Model"/>
<partial name="RichText/_Document" model="@Model" />
break;
case RichTextNodeType.Paragraph:
<partial name="RichText/_Paragraph" model="@Model"/>
<partial name="RichText/_Paragraph" model="@Model" />
break;
case RichTextNodeType.Heading2:
case RichTextNodeType.Heading3:
case RichTextNodeType.Heading4:
case RichTextNodeType.Heading5:
case RichTextNodeType.Heading6:
<partial name="RichText/_H" model="@Model"/>
<partial name="RichText/_H" model="@Model" />
break;
case RichTextNodeType.UnorderedList:
<partial name="RichText/_UnorderedList" model="@Model"/>
<partial name="RichText/_UnorderedList" model="@Model" />
break;
case RichTextNodeType.OrderedList:
<partial name="RichText/_OrderedList" model="@Model"/>
<partial name="RichText/_OrderedList" model="@Model" />
break;
case RichTextNodeType.ListItem:
<partial name="RichText/_ListItem" model="@Model"/>
<partial name="RichText/_ListItem" model="@Model" />
break;
case RichTextNodeType.Hyperlink:
<partial name="RichText/_Hyperlink" model="@Model"/>
<partial name="RichText/_Hyperlink" model="@Model" />
break;
case RichTextNodeType.Table:
<partial name="RichText/_Table" model="@Model"/>
<partial name="RichText/_Table" model="@Model" />
break;
case RichTextNodeType.TableRow:
<partial name="RichText/_TableRow" model="@Model"/>
<partial name="RichText/_TableRow" model="@Model" />
break;
case RichTextNodeType.TableHeaderCell:
<partial name="RichText/_TableHeaderCell" model="@Model"/>
<partial name="RichText/_TableHeaderCell" model="@Model" />
break;
case RichTextNodeType.TableCell:
<partial name="RichText/_TableCell" model="@Model"/>
<partial name="RichText/_TableCell" model="@Model" />
break;
case RichTextNodeType.Hr:
<hr/>
<hr />
break;
case RichTextNodeType.EmbeddedAsset:
<partial name="RichText/_Asset" model="@Model"/>
<partial name="RichText/_Asset" model="@Model" />
break;
case RichTextNodeType.Text:
<partial name="RichText/_Text" model="@Model"/>
<partial name="RichText/_Text" model="@Model" />
break;
case RichTextNodeType.EmbeddedEntry:
<partial name="RichText/_Entry" model="@Model"/>
<partial name="RichText/_Entry" model="@Model" />
break;
default:
<partial name="_UnsupportedElement" model="@Model"/>
<partial name="_UnsupportedElement" model="@Model" />
break;
}
}
}
20 changes: 0 additions & 20 deletions src/Dfe.ContentSupport.Web/appsettings.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.ContentSupport.Web.Common;
using Contentful.Core.Models;
using Dfe.ContentSupport.Web.Common;
using Dfe.ContentSupport.Web.Configuration;
using Dfe.ContentSupport.Web.Models;
using Dfe.ContentSupport.Web.Models.Mapped;
Expand Down Expand Up @@ -28,11 +29,11 @@ public class CustomAccordionTests
InternalName = InternalName,
Title = Title,
SummaryLine = SummaryLine,
Sys = new Sys
SystemProperties = new SystemProperties
{
ContentType = new ContentType
ContentType = new Contentful.Core.Models.ContentType
{
Sys = new Sys
SystemProperties = new SystemProperties
{
Id = ContentId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public class CustomAttachmentTests
{
InternalName = InternalName,
Title = Title,
Sys = new Sys
SystemProperties = new SystemProperties
{
ContentType = new ContentType
ContentType = new Contentful.Core.Models.ContentType
{
Sys = new Sys

SystemProperties = new SystemProperties
{
Id = ContentId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public class CustomCardTests
Meta = Meta,
ImageAlt = ImageAlt,
Description = Description,
Sys = new Sys
SystemProperties = new Contentful.Core.Models.SystemProperties
{
ContentType = new ContentType
ContentType = new Contentful.Core.Models.ContentType
{
Sys = new Sys
SystemProperties = new Contentful.Core.Models.SystemProperties
{
Id = ContentId
}
Expand All @@ -52,10 +52,10 @@ public class CustomCardTests
{
Url = ImageUri
}
}
},
}
}
}
},
},
};

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.ContentSupport.Web.Common;
using Contentful.Core.Models;
using Dfe.ContentSupport.Web.Common;
using Dfe.ContentSupport.Web.Configuration;
using Dfe.ContentSupport.Web.Models;
using Dfe.ContentSupport.Web.Models.Mapped.Custom;
Expand Down Expand Up @@ -31,11 +32,12 @@ public class CustomGridContainerTests
Meta = CardMeta,
ImageAlt = CardImageAlt,
Description = CardDescription,
Sys = new Sys
SystemProperties = new SystemProperties
{
ContentType = new ContentType
ContentType = new Contentful.Core.Models.ContentType
{
Sys = new Sys

SystemProperties = new SystemProperties
{
Id = CardContentId
}
Expand All @@ -45,7 +47,7 @@ public class CustomGridContainerTests
{
Fields = new Fields
{
File = new FileDetails
File = new Web.Models.FileDetails
{
Url = CardImageUri
}
Expand All @@ -61,11 +63,11 @@ public class CustomGridContainerTests
Target = new Target
{
InternalName = ContainerInternalName,
Sys = new Sys
SystemProperties = new Contentful.Core.Models.SystemProperties
{
ContentType = new ContentType
ContentType = new Contentful.Core.Models.ContentType
{
Sys = new Sys
SystemProperties = new Contentful.Core.Models.SystemProperties
{
Id = ContainerContentId
}
Expand Down
Loading

0 comments on commit 67254b6

Please sign in to comment.