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

101 lines
2.8 KiB
C#
Raw Normal View History

2023-08-11 15:51:20 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Primitives;
using OliverBooth.Common.Data.Blog;
using OliverBooth.Common.Services;
using BC = BCrypt.Net.BCrypt;
namespace OliverBooth.Pages.Blog;
/// <summary>
/// Represents the page model for the <c>Article</c> page.
/// </summary>
[Area("blog")]
public class Article : PageModel
{
private readonly IBlogPostService _blogPostService;
/// <summary>
/// Initializes a new instance of the <see cref="Article" /> class.
/// </summary>
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
public Article(IBlogPostService blogPostService)
{
_blogPostService = blogPostService;
}
/*
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>
public bool IsWordPressLegacyPost => Post.WordPressId.HasValue;
*/
2023-08-08 01:17:23 +01:00
/// <summary>
/// Gets the requested blog post.
/// </summary>
/// <value>The requested blog post.</value>
public IBlogPost Post { get; private set; } = null!;
/// <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)
{
var date = new DateOnly(year, month, day);
if (!_blogPostService.TryGetPost(date, slug, out IBlogPost? post))
{
Response.StatusCode = 404;
2023-08-08 00:28:33 +01:00
return NotFound();
}
2023-08-08 00:28:33 +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))
{
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;
2023-08-08 00:28:33 +01:00
return Page();
}
}