2024-07-15 19:44:02 +01:00
|
|
|
using System.Web;
|
2023-08-11 15:51:20 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-08-06 15:57:23 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
2024-05-05 02:18:20 +01:00
|
|
|
using OliverBooth.Common.Data.Blog;
|
|
|
|
using OliverBooth.Common.Services;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
namespace OliverBooth.Pages.Blog;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-11 14:26:21 +01:00
|
|
|
[Area("blog")]
|
2023-08-06 15:57:23 +01:00
|
|
|
public class Index : PageModel
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
private readonly IBlogPostService _blogPostService;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-12 20:13:47 +01:00
|
|
|
public Index(IBlogPostService blogPostService)
|
2023-08-06 15:57:23 +01:00
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
_blogPostService = blogPostService;
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 19:38:56 +01:00
|
|
|
public string[] Tag { get; private set; } = [];
|
|
|
|
|
2023-08-11 14:09:13 +01:00
|
|
|
public IActionResult OnGet([FromQuery(Name = "pid")] Guid? postId = null,
|
2024-07-15 19:38:56 +01:00
|
|
|
[FromQuery(Name = "p")] int? wpPostId = null,
|
|
|
|
[FromQuery(Name = "tag")] string? tag = 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
|
|
|
{
|
2024-07-15 19:44:02 +01:00
|
|
|
Tag = (tag?.Split('+').Select(HttpUtility.UrlDecode).ToArray() ?? [])!;
|
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
|
|
|
}
|
|
|
|
|
2023-08-11 14:09:13 +01:00
|
|
|
private IActionResult HandleNewRoute(Guid postId)
|
2023-08-08 12:07:30 +01:00
|
|
|
{
|
2023-08-12 20:13:47 +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)
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
return _blogPostService.TryGetPost(wpPostId, out IBlogPost? post) ? RedirectToPost(post) : NotFound();
|
2023-08-08 12:07:30 +01:00
|
|
|
}
|
|
|
|
|
2024-05-05 18:13:06 +01:00
|
|
|
private RedirectResult RedirectToPost(IBlogPost post)
|
2023-08-08 12:07:30 +01:00
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
var route = new
|
2023-08-08 01:30:32 +01:00
|
|
|
{
|
2023-08-11 15:43:06 +01:00
|
|
|
year = post.Published.ToString("yyyy"),
|
|
|
|
month = post.Published.ToString("MM"),
|
|
|
|
day = post.Published.ToString("dd"),
|
2023-08-08 11:35:22 +01:00
|
|
|
slug = post.Slug
|
|
|
|
};
|
2023-09-20 14:48:26 +01:00
|
|
|
return Redirect(Url.Page("/Blog/Article", route)!);
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
|
|
|
}
|