From 75e2ebdf76739fd19d08db9bdd363a5ebd43fb14 Mon Sep 17 00:00:00 2001 From: simonjfirth Date: Thu, 27 Jun 2024 13:14:12 +0100 Subject: [PATCH] Adding landing page (#104) * Created temp home page * updated is Preview to true for home page * Preview mode bug fixes for home page --------- Co-authored-by: Tom Whittington --- .../Controllers/HomeController.cs | 22 +++++++++++++++-- .../Http/HttpContentfulClient.cs | 4 ++-- src/Dfe.ContentSupport.Web/Program.cs | 12 ++++++---- .../Services/ContentService.cs | 3 ++- .../Services/IContentService.cs | 3 +++ .../Views/Home/Home.cshtml | 24 +++++++++++++++++++ .../Views/Shared/_Header.cshtml | 1 + 7 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/Dfe.ContentSupport.Web/Views/Home/Home.cshtml diff --git a/src/Dfe.ContentSupport.Web/Controllers/HomeController.cs b/src/Dfe.ContentSupport.Web/Controllers/HomeController.cs index 4ff0a19..e5b4c55 100644 --- a/src/Dfe.ContentSupport.Web/Controllers/HomeController.cs +++ b/src/Dfe.ContentSupport.Web/Controllers/HomeController.cs @@ -8,12 +8,30 @@ namespace Dfe.ContentSupport.Web.Controllers; public class HomeController(IContentService contentService) : Controller { + + public async Task Home() + { + var defaultModel = new ContentSupportPage + { + Heading = new Models.Heading + { + Title = "Department for Education", + Subtitle = "Content and Support", + } + }; + var resp = await contentService.GetContentSupportPages(nameof(ContentSupportPage.IsSitemap), "true", true); + ViewBag.pages = resp; + + return View(defaultModel); + } + + [HttpGet("{slug}")] public async Task Index(string slug, bool isPreview = false) { if (string.IsNullOrEmpty(slug)) return RedirectToAction("error"); var resp = await contentService.GetContent(slug, isPreview); - if(resp is null) return RedirectToAction("error"); + if (resp is null) return RedirectToAction("error"); return View(resp); } @@ -26,6 +44,6 @@ public IActionResult Privacy() public IActionResult Error() { return View(new ErrorViewModel - { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } \ No newline at end of file diff --git a/src/Dfe.ContentSupport.Web/Http/HttpContentfulClient.cs b/src/Dfe.ContentSupport.Web/Http/HttpContentfulClient.cs index 9136c8b..86cf664 100644 --- a/src/Dfe.ContentSupport.Web/Http/HttpContentfulClient.cs +++ b/src/Dfe.ContentSupport.Web/Http/HttpContentfulClient.cs @@ -8,10 +8,10 @@ namespace Dfe.ContentSupport.Web.Http; public class HttpContentfulClient(HttpClient httpClient, CsContentfulOptions options) : ContentfulClient(httpClient, options), IHttpContentfulClient { - public Task> Query(QueryBuilder queryBuilder, + public async Task> Query(QueryBuilder queryBuilder, CancellationToken cancellationToken = default) { queryBuilder = queryBuilder.Include(options.IncludeDepth); - return GetEntries(queryBuilder, cancellationToken); + return await GetEntries(queryBuilder, cancellationToken); } } \ No newline at end of file diff --git a/src/Dfe.ContentSupport.Web/Program.cs b/src/Dfe.ContentSupport.Web/Program.cs index f5f3fa2..edf96e6 100644 --- a/src/Dfe.ContentSupport.Web/Program.cs +++ b/src/Dfe.ContentSupport.Web/Program.cs @@ -47,10 +47,14 @@ public static void Main(string[] args) ); app.MapControllerRoute( - "default/{slug}", - "{slug?}", - new { controller = "Home", action = "Index" } - ); + name: "home", + pattern: "{controller=Home}/{action=Home}"); + + app.MapControllerRoute( + name: "slug", + pattern: "{slug}", + defaults: new { controller = "Home", action = "Index" }); + app.Run(); } diff --git a/src/Dfe.ContentSupport.Web/Services/ContentService.cs b/src/Dfe.ContentSupport.Web/Services/ContentService.cs index 861eccd..2c28a91 100644 --- a/src/Dfe.ContentSupport.Web/Services/ContentService.cs +++ b/src/Dfe.ContentSupport.Web/Services/ContentService.cs @@ -34,7 +34,7 @@ from url in resp return sitemap.ToString(); } - private async Task> GetContentSupportPages( + public async Task> GetContentSupportPages( string field, string value, bool isPreview) { var builder = QueryBuilder.New.ContentTypeIs(nameof(ContentSupportPage)) @@ -42,4 +42,5 @@ private async Task> GetContentSupportPa return await contentfulService.ContentfulClient(isPreview).Query(builder); } + } \ No newline at end of file diff --git a/src/Dfe.ContentSupport.Web/Services/IContentService.cs b/src/Dfe.ContentSupport.Web/Services/IContentService.cs index 22b504c..82a177e 100644 --- a/src/Dfe.ContentSupport.Web/Services/IContentService.cs +++ b/src/Dfe.ContentSupport.Web/Services/IContentService.cs @@ -1,3 +1,4 @@ +using Contentful.Core.Models; using Dfe.ContentSupport.Web.ViewModels; namespace Dfe.ContentSupport.Web.Services; @@ -6,4 +7,6 @@ public interface IContentService { Task GetContent(string slug, bool isPreview); Task GenerateSitemap(string baseUrl); + + Task> GetContentSupportPages(string field, string value, bool isPreview); } \ No newline at end of file diff --git a/src/Dfe.ContentSupport.Web/Views/Home/Home.cshtml b/src/Dfe.ContentSupport.Web/Views/Home/Home.cshtml new file mode 100644 index 0000000..1a1d718 --- /dev/null +++ b/src/Dfe.ContentSupport.Web/Views/Home/Home.cshtml @@ -0,0 +1,24 @@ +@model ContentSupportPage + +

@ViewData["Title"]

+ + +@{ + if (ViewBag?.Pages?.Items != null) + { +
+ @for (var i = 0; i < ViewBag.Pages.Items.Count; i++) + { + var url = ViewBag.Pages.Items[i].Slug + "?isPreview=true"; +
+
+

+ @ViewBag.Pages.Items[i].Heading.Title +

+

@ViewBag.Pages.Items[i].Heading.Subtitle

+
+
+ } +
+ } +} \ No newline at end of file diff --git a/src/Dfe.ContentSupport.Web/Views/Shared/_Header.cshtml b/src/Dfe.ContentSupport.Web/Views/Shared/_Header.cshtml index a5602ac..750993c 100644 --- a/src/Dfe.ContentSupport.Web/Views/Shared/_Header.cshtml +++ b/src/Dfe.ContentSupport.Web/Views/Shared/_Header.cshtml @@ -11,4 +11,5 @@ + \ No newline at end of file