oliverbooth.dev/OliverBooth/Pages/Blog/Index.cshtml.cs

51 lines
1.4 KiB
C#
Raw Normal View History

2023-08-11 15:51:20 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
namespace OliverBooth.Pages.Blog;
[Area("blog")]
public class Index : PageModel
{
private readonly IBlogPostService _blogPostService;
public Index(IBlogPostService blogPostService)
{
_blogPostService = blogPostService;
}
public IActionResult OnGet([FromQuery(Name = "pid")] Guid? postId = null,
2023-08-08 12:07:30 +01:00
[FromQuery(Name = "p")] int? wpPostId = null)
{
2023-08-08 12:41:12 +01:00
if (postId.HasValue == wpPostId.HasValue)
2023-08-08 12:07:30 +01:00
{
return Page();
}
2023-08-08 12:41:12 +01:00
return postId.HasValue ? HandleNewRoute(postId.Value) : HandleWordPressRoute(wpPostId!.Value);
2023-08-08 12:07:30 +01:00
}
private IActionResult HandleNewRoute(Guid postId)
2023-08-08 12:07:30 +01:00
{
return _blogPostService.TryGetPost(postId, out IBlogPost? post) ? RedirectToPost(post) : NotFound();
2023-08-08 12:07:30 +01:00
}
private IActionResult HandleWordPressRoute(int wpPostId)
{
return _blogPostService.TryGetPost(wpPostId, out IBlogPost? post) ? RedirectToPost(post) : NotFound();
2023-08-08 12:07:30 +01:00
}
private IActionResult RedirectToPost(IBlogPost post)
2023-08-08 12:07:30 +01:00
{
var route = new
{
2023-08-11 15:43:06 +01:00
year = post.Published.ToString("yyyy"),
month = post.Published.ToString("MM"),
day = post.Published.ToString("dd"),
slug = post.Slug
};
return Redirect(Url.Page("/Blog/Article", route)!);
}
}