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;
|
2023-09-26 12:46:18 +01:00
|
|
|
using Microsoft.Extensions.Primitives;
|
2023-08-13 17:33:54 +01:00
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
using OliverBooth.Services;
|
2023-09-26 12:46:18 +01:00
|
|
|
using BC = BCrypt.Net.BCrypt;
|
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-08 02:06:11 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Represents the page model for the <c>Article</c> page.
|
|
|
|
/// </summary>
|
2023-08-11 14:26:21 +01:00
|
|
|
[Area("blog")]
|
2023-08-06 15:57:23 +01:00
|
|
|
public class Article : PageModel
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
private readonly IBlogPostService _blogPostService;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-08 02:06:11 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Article" /> class.
|
|
|
|
/// </summary>
|
2023-08-12 20:13:47 +01:00
|
|
|
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
|
|
|
|
public Article(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
|
|
|
}
|
|
|
|
|
2023-08-12 20:13:47 +01:00
|
|
|
/*
|
2023-08-08 01:17:23 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether the post is a legacy WordPress post.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <see langword="true" /> if the post is a legacy WordPress post; otherwise, <see langword="false" />.
|
|
|
|
/// </value>
|
2023-08-08 02:06:11 +01:00
|
|
|
public bool IsWordPressLegacyPost => Post.WordPressId.HasValue;
|
2023-08-12 20:13:47 +01:00
|
|
|
*/
|
2023-08-08 01:17:23 +01:00
|
|
|
|
2023-08-08 02:06:11 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the requested blog post.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The requested blog post.</value>
|
2023-08-12 20:13:47 +01:00
|
|
|
public IBlogPost Post { get; private set; } = null!;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-09-26 12:46:18 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether to show the password prompt.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <see langword="true" /> if the password prompt should be shown; otherwise, <see langword="false" />.
|
|
|
|
/// </value>
|
|
|
|
public bool ShowPasswordPrompt { get; private set; }
|
|
|
|
|
2023-08-08 00:28:33 +01:00
|
|
|
public IActionResult OnGet(int year, int month, int day, string slug)
|
2023-08-06 15:57:23 +01:00
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
var date = new DateOnly(year, month, day);
|
|
|
|
if (!_blogPostService.TryGetPost(date, slug, out IBlogPost? post))
|
2023-08-06 15:57:23 +01:00
|
|
|
{
|
|
|
|
Response.StatusCode = 404;
|
2023-08-08 00:28:33 +01:00
|
|
|
return NotFound();
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
2023-08-08 00:28:33 +01:00
|
|
|
|
2023-09-26 12:46:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-12 20:13:47 +01:00
|
|
|
if (post.IsRedirect)
|
|
|
|
{
|
|
|
|
return Redirect(post.RedirectUrl!.ToString());
|
|
|
|
}
|
|
|
|
|
2023-08-08 02:06:11 +01:00
|
|
|
Post = post;
|
2023-08-08 00:28:33 +01:00
|
|
|
return Page();
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
|
|
|
}
|