diff --git a/OliverBooth/Pages/Blog/Article.cshtml b/OliverBooth/Pages/Blog/Article.cshtml index 3129637..5f04d8a 100644 --- a/OliverBooth/Pages/Blog/Article.cshtml +++ b/OliverBooth/Pages/Blog/Article.cshtml @@ -1,9 +1,32 @@ @page "/blog/{year:int}/{month:int}/{day:int}/{slug}" +@using Humanizer @model OliverBooth.Pages.Blog.Article @if (Model.Post is { } post) { + +

@post.Title

-

@post.Published.ToString("MMMM dd, yyyy") • @Model.Author?.Name

- @Html.Raw(Model.SanitizeContent(post.Body)) -} +

+ + @Model.Author?.Name + • + + @post.Published.Humanize() + +

+ +
+ @Html.Raw(Model.SanitizeContent(post.Body)) +
+ +
+ +} \ No newline at end of file diff --git a/OliverBooth/Pages/Blog/Article.cshtml.cs b/OliverBooth/Pages/Blog/Article.cshtml.cs index 6040487..abaadd0 100644 --- a/OliverBooth/Pages/Blog/Article.cshtml.cs +++ b/OliverBooth/Pages/Blog/Article.cshtml.cs @@ -1,4 +1,4 @@ -using Humanizer; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using OliverBooth.Data; @@ -15,13 +15,13 @@ public class Article : PageModel _dbContextFactory = dbContextFactory; } - public Author? Author { get; private set; } + public Author Author { get; private set; } - public BlogPost? Post { get; private set; } + public BlogPost Post { get; private set; } = new(); public string SanitizeContent(string content) { - content = content.Replace("", string.Empty); + content = content.Replace("", string.Empty); while (content.Contains("\n\n")) { @@ -31,20 +31,22 @@ public class Article : PageModel return Markdig.Markdown.ToHtml(content.Trim()); } - public void OnGet(int year, int month, string slug) + public IActionResult OnGet(int year, int month, int day, string slug) { using BlogContext context = _dbContextFactory.CreateDbContext(); Post = context.BlogPosts.FirstOrDefault(p => p.Published.Year == year && p.Published.Month == month && - p.Slug == slug); + p.Published.Day == day && + p.Slug == slug)!; + // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract if (Post is null) { Response.StatusCode = 404; + return NotFound(); } - else - { - Author = context.Authors.FirstOrDefault(a => a.Id == Post.AuthorId); - } + + Author = context.Authors.FirstOrDefault(a => a.Id == Post.AuthorId)!; + return Page(); } }