From 83e5757429e8ae82cec4db607f552a04bbf5656a Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 8 Aug 2023 01:30:32 +0100 Subject: [PATCH] feat: delegate blog listing to BlogService --- OliverBooth/Pages/Blog/Index.cshtml | 77 ++++++++++++++++++++++---- OliverBooth/Pages/Blog/Index.cshtml.cs | 24 ++++++-- OliverBooth/Program.cs | 1 + OliverBooth/Services/BlogService.cs | 32 +++++++++++ 4 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 OliverBooth/Services/BlogService.cs diff --git a/OliverBooth/Pages/Blog/Index.cshtml b/OliverBooth/Pages/Blog/Index.cshtml index b17bfed..565a365 100644 --- a/OliverBooth/Pages/Blog/Index.cshtml +++ b/OliverBooth/Pages/Blog/Index.cshtml @@ -1,16 +1,73 @@ @page +@using Humanizer @using OliverBooth.Data.Blog +@using OliverBooth.Services @model OliverBooth.Pages.Blog.Index +@inject BlogService BlogService -@foreach (BlogPost post in Model.BlogPosts) +@foreach (BlogPost post in BlogService.AllPosts) { -

- @post.Title -

-

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

-

@Html.Raw(Model.SanitizeContent(Model.TrimContent(post.Body, out bool trimmed)))

- if (trimmed) - { -

Read more...

- } + Author? author = Model.GetAuthor(post); + DateTimeOffset published = post.Published; + var year = published.ToString("yyyy"); + var month = published.ToString("MM"); + var day = published.ToString("dd"); + + bool isLegacyPost = post.WordPressId is not null; + string disqusDomain = isLegacyPost ? "https://blog.oliverbooth.dev" : "https://oliverbooth.dev/blog"; + string disqusId = isLegacyPost ? $"{post.WordPressId} {disqusDomain}/?p={post.WordPressId}" : post.Id.ToString(); + +
+
+

+ + @post.Title + +

+ +

+ + @author?.Name + • + + @post.Published.Humanize() + + @if (post.EnableComments) + { + + + 0 Comments + + } +

+ +

@Html.Raw(Model.SanitizeContent(Model.TrimContent(post.Body, out bool trimmed)))

+ + +
+
+ + } \ No newline at end of file diff --git a/OliverBooth/Pages/Blog/Index.cshtml.cs b/OliverBooth/Pages/Blog/Index.cshtml.cs index 58c6d83..3b960e6 100644 --- a/OliverBooth/Pages/Blog/Index.cshtml.cs +++ b/OliverBooth/Pages/Blog/Index.cshtml.cs @@ -1,4 +1,5 @@ using Humanizer; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using OliverBooth.Data; @@ -19,7 +20,7 @@ public class Index : PageModel public string SanitizeContent(string content) { - content = content.Replace("", string.Empty); + content = content.Replace("", string.Empty); while (content.Contains("\n\n")) { @@ -32,20 +33,33 @@ public class Index : PageModel public string TrimContent(string content, out bool trimmed) { ReadOnlySpan span = content.AsSpan(); - int moreIndex = span.IndexOf("", StringComparison.Ordinal); + int moreIndex = span.IndexOf("", StringComparison.Ordinal); trimmed = moreIndex != -1 || span.Length > 256; return moreIndex != -1 ? span[..moreIndex].Trim().ToString() : content.Truncate(256); } - + public Author? GetAuthor(BlogPost post) { using BlogContext context = _dbContextFactory.CreateDbContext(); return context.Authors.FirstOrDefault(a => a.Id == post.AuthorId); } - public void OnGet() + public IActionResult OnGet([FromQuery(Name = "p")] int? postId = null) { using BlogContext context = _dbContextFactory.CreateDbContext(); - BlogPosts = context.BlogPosts.ToArray(); + if (postId is null) + { + BlogPosts = context.BlogPosts.ToArray(); + return Page(); + } + + BlogPost? post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == postId); + + if (post is not null) + { + return Redirect($"/blog/{post.Published:yyyy/MM}/{post.Slug}"); + } + + return NotFound(); } } diff --git a/OliverBooth/Program.cs b/OliverBooth/Program.cs index 8155825..4771bcf 100644 --- a/OliverBooth/Program.cs +++ b/OliverBooth/Program.cs @@ -27,6 +27,7 @@ builder.Services.AddSingleton(new MarkdownPipelineBuilder() builder.Services.AddDbContextFactory(); builder.Services.AddDbContextFactory(); +builder.Services.AddSingleton(); builder.Services.AddRazorPages().AddRazorRuntimeCompilation(); builder.Services.AddControllersWithViews(); builder.Services.AddRouting(options => options.LowercaseUrls = true); diff --git a/OliverBooth/Services/BlogService.cs b/OliverBooth/Services/BlogService.cs new file mode 100644 index 0000000..dc005d2 --- /dev/null +++ b/OliverBooth/Services/BlogService.cs @@ -0,0 +1,32 @@ +using Microsoft.EntityFrameworkCore; +using OliverBooth.Data; +using OliverBooth.Data.Blog; + +namespace OliverBooth.Services; + +public sealed class BlogService +{ + private IDbContextFactory _dbContextFactory; + + /// + /// Initializes a new instance of the class. + /// + /// The . + public BlogService(IDbContextFactory dbContextFactory) + { + _dbContextFactory = dbContextFactory; + } + + /// + /// Gets a read-only view of all blog posts. + /// + /// A read-only view of all blog posts. + public IReadOnlyCollection AllPosts + { + get + { + using BlogContext context = _dbContextFactory.CreateDbContext(); + return context.BlogPosts.OrderByDescending(p => p.Published).ToArray(); + } + } +}