using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Primitives;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
using BC = BCrypt.Net.BCrypt;
namespace OliverBooth.Pages.Blog;
///
/// Represents the page model for the Article page.
///
[Area("blog")]
public class Article : PageModel
{
private readonly IBlogPostService _blogPostService;
///
/// Initializes a new instance of the class.
///
/// The .
public Article(IBlogPostService blogPostService)
{
_blogPostService = blogPostService;
}
/*
///
/// Gets a value indicating whether the post is a legacy WordPress post.
///
///
/// if the post is a legacy WordPress post; otherwise, .
///
public bool IsWordPressLegacyPost => Post.WordPressId.HasValue;
*/
///
/// Gets the requested blog post.
///
/// The requested blog post.
public IBlogPost Post { get; private set; } = null!;
///
/// Gets a value indicating whether to show the password prompt.
///
///
/// if the password prompt should be shown; otherwise, .
///
public bool ShowPasswordPrompt { get; private set; }
public IActionResult OnGet(int year, int month, int day, string slug)
{
var date = new DateOnly(year, month, day);
if (!_blogPostService.TryGetPost(date, slug, out IBlogPost? post))
{
Response.StatusCode = 404;
return NotFound();
}
if (!string.IsNullOrWhiteSpace(post.Password))
{
ShowPasswordPrompt = true;
}
if (post.IsRedirect)
{
return Redirect(post.RedirectUrl!.ToString());
}
Post = post;
return Page();
}
public IActionResult OnPost([FromRoute] int year,
[FromRoute] int month,
[FromRoute] int day,
[FromRoute] string slug)
{
var date = new DateOnly(year, month, day);
if (!_blogPostService.TryGetPost(date, slug, out IBlogPost? post))
{
Response.StatusCode = 404;
return NotFound();
}
ShowPasswordPrompt = true;
if (Request.Form.TryGetValue("password", out StringValues password) && BC.Verify(password, post.Password))
{
ShowPasswordPrompt = false;
}
if (post.IsRedirect)
{
return Redirect(post.RedirectUrl!.ToString());
}
Post = post;
return Page();
}
}