From 96ec83e5255c0c5edc5858bbd13463ce17e2d1cd Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 8 Aug 2023 12:07:30 +0100 Subject: [PATCH] feat: add new permalink to all posts --- OliverBooth/Pages/Blog/Index.cshtml.cs | 37 +++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/OliverBooth/Pages/Blog/Index.cshtml.cs b/OliverBooth/Pages/Blog/Index.cshtml.cs index 26e4ca9..c04685c 100644 --- a/OliverBooth/Pages/Blog/Index.cshtml.cs +++ b/OliverBooth/Pages/Blog/Index.cshtml.cs @@ -1,4 +1,5 @@ -using Humanizer; +using System.Diagnostics; +using Humanizer; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using OliverBooth.Data.Blog; @@ -35,11 +36,39 @@ public class Index : PageModel return moreIndex != -1 ? span[..moreIndex].Trim().ToString() : content.Truncate(256); } - public IActionResult OnGet([FromQuery(Name = "p")] int? postId = null) + public IActionResult OnGet([FromQuery(Name = "pid")] int? postId = null, + [FromQuery(Name = "p")] int? wpPostId = null) { - if (!postId.HasValue) return Page(); - if (!_blogService.TryGetWordPressBlogPost(postId.Value, out BlogPost? post)) return NotFound(); + if (!(postId.HasValue ^ wpPostId.HasValue)) + { + return Page(); + } + if (wpPostId.HasValue) + { + return HandleWordPressRoute(wpPostId.Value); + } + + if (postId.HasValue) + { + return HandleNewRoute(postId.Value); + } + + throw new UnreachableException(); + } + + private IActionResult HandleNewRoute(int postId) + { + return _blogService.TryGetBlogPost(postId, out BlogPost? post) ? RedirectToPost(post) : NotFound(); + } + + private IActionResult HandleWordPressRoute(int wpPostId) + { + return _blogService.TryGetWordPressBlogPost(wpPostId, out BlogPost? post) ? RedirectToPost(post) : NotFound(); + } + + private IActionResult RedirectToPost(BlogPost post) + { var route = new { year = post.Published.Year,