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

52 lines
1.4 KiB
C#
Raw Normal View History

2023-08-11 15:43:06 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
2023-08-11 14:08:14 +01:00
namespace OliverBooth.Areas.Blog.Pages;
[Area("blog")]
public class Index : PageModel
{
private readonly BlogService _blogService;
public Index(BlogService blogService)
{
_blogService = blogService;
}
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 _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
{
2023-08-11 15:42:48 +01:00
area = "blog",
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
};
2023-08-11 15:42:48 +01:00
return Redirect(Url.Page("/Article", route)!);
}
}