Skip to content

Commit

Permalink
Pulled getEntries out
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Whittington committed May 8, 2024
1 parent cc0e4e5 commit 6d38436
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Dfe.ContentSupport.Web/Services/ContentfulService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Xml.Linq;
using Contentful.Core;
using Contentful.Core.Models;
using Contentful.Core.Search;
using Dfe.ContentSupport.Web.Models;

Expand All @@ -9,17 +10,13 @@ public class ContentfulService(IContentfulClient contentfulClient) : IContentful
{
public async Task<object> GetContent(string slug)
{
var builder = QueryBuilder<ContentSupportPage>.New.ContentTypeIs(nameof(ContentSupportPage))
.FieldEquals($"fields.{nameof(ContentSupportPage.Slug)}", slug);
var resp = await contentfulClient.GetEntries(builder);
var resp = await GetContentSupportPages(nameof(ContentSupportPage.Slug), slug);
return resp.FirstOrDefault();
}

public async Task<string> GenerateSitemap(string baseUrl)
{
var builder = QueryBuilder<ContentSupportPage>.New.ContentTypeIs(nameof(ContentSupportPage))
.FieldEquals($"fields.{nameof(ContentSupportPage.IsSitemap)}", "true");
var resp = await contentfulClient.GetEntries(builder);
var resp = await GetContentSupportPages(nameof(ContentSupportPage.IsSitemap), "true");

XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
var sitemap = new XDocument(
Expand All @@ -36,4 +33,12 @@ from url in resp

return sitemap.ToString();
}

private async Task<ContentfulCollection<ContentSupportPage>> GetContentSupportPages(string field,
string value)
{
var builder = QueryBuilder<ContentSupportPage>.New.ContentTypeIs(nameof(ContentSupportPage))
.FieldEquals($"fields.{field}", value);
return await contentfulClient.GetEntries(builder);
}
}

0 comments on commit 6d38436

Please sign in to comment.