2023-08-11 14:08:14 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-08-06 15:57:23 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using OliverBooth.Data.Blog;
|
2023-08-08 11:35:22 +01:00
|
|
|
using OliverBooth.Services;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-11 14:08:14 +01:00
|
|
|
namespace OliverBooth.Areas.Blog.Pages;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
|
|
|
public class Index : PageModel
|
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
private readonly BlogService _blogService;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-08 11:35:22 +01:00
|
|
|
public Index(BlogService blogService)
|
2023-08-06 15:57:23 +01:00
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
_blogService = blogService;
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
|
|
|
|
2023-08-08 12:07:30 +01:00
|
|
|
public IActionResult OnGet([FromQuery(Name = "pid")] int? postId = null,
|
|
|
|
[FromQuery(Name = "p")] int? wpPostId = null)
|
2023-08-06 15:57:23 +01:00
|
|
|
{
|
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(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)
|
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
var route = new
|
2023-08-08 01:30:32 +01:00
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
year = post.Published.Year,
|
|
|
|
month = post.Published.Month,
|
|
|
|
day = post.Published.Day,
|
|
|
|
slug = post.Slug
|
|
|
|
};
|
|
|
|
return Redirect(Url.Page("/Blog/Article", route)!);
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
|
|
|
}
|