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

50 lines
1.3 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
namespace OliverBooth.Pages.Blog;
public class Index : PageModel
{
private readonly BlogService _blogService;
public Index(BlogService blogService)
{
_blogService = blogService;
}
2023-08-08 11:07:30 +00:00
public IActionResult OnGet([FromQuery(Name = "pid")] int? postId = null,
[FromQuery(Name = "p")] int? wpPostId = null)
{
2023-08-08 11:41:12 +00:00
if (postId.HasValue == wpPostId.HasValue)
2023-08-08 11:07:30 +00:00
{
return Page();
}
2023-08-08 11:41:12 +00:00
return postId.HasValue ? HandleNewRoute(postId.Value) : HandleWordPressRoute(wpPostId!.Value);
2023-08-08 11:07:30 +00:00
}
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,
month = post.Published.Month,
day = post.Published.Day,
slug = post.Slug
};
return Redirect(Url.Page("/Blog/Article", route)!);
}
}