diff --git a/OliverBooth/Middleware/RssMiddleware.cs b/OliverBooth/Controllers/Blog/RssController.cs similarity index 68% rename from OliverBooth/Middleware/RssMiddleware.cs rename to OliverBooth/Controllers/Blog/RssController.cs index 8f38ea2..83b1a08 100644 --- a/OliverBooth/Middleware/RssMiddleware.cs +++ b/OliverBooth/Controllers/Blog/RssController.cs @@ -1,32 +1,39 @@ -using System.Diagnostics.CodeAnalysis; -using System.Xml.Serialization; +using System.Xml.Serialization; +using Microsoft.AspNetCore.Mvc; using OliverBooth.Data.Blog; using OliverBooth.Data.Blog.Rss; using OliverBooth.Services; -namespace OliverBooth.Middleware; +namespace OliverBooth.Controllers.Blog; -internal sealed class RssMiddleware +[ApiController] +[Route("blog/feed")] +public class RssController : Controller { private readonly IBlogPostService _blogPostService; - public RssMiddleware(RequestDelegate _, IBlogPostService blogPostService) + /// + /// Initializes a new instance of the class. + /// + /// The . + public RssController(IBlogPostService blogPostService) { _blogPostService = blogPostService; } - [SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Middleware")] - public async Task Invoke(HttpContext context) + [HttpGet] + [Produces("application/rss+xml")] + public IActionResult OnGet() { - context.Response.ContentType = "application/rss+xml"; + Response.ContentType = "application/rss+xml"; - var baseUrl = $"https://{context.Request.Host}/blog"; + var baseUrl = $"https://{Request.Host}/blog"; var blogItems = new List(); foreach (IBlogPost post in _blogPostService.GetAllBlogPosts()) { var url = $"{baseUrl}/{post.Published:yyyy/MM/dd}/{post.Slug}"; - string excerpt = _blogPostService.RenderExcerpt(post, out _); + string excerpt = _blogPostService.RenderPost(post); var description = $"{excerpt}

Read more...

"; var item = new BlogItem @@ -36,7 +43,7 @@ internal sealed class RssMiddleware Comments = $"{url}#disqus_thread", Creator = post.Author.DisplayName, PubDate = post.Published.ToString("R"), - Guid = $"{baseUrl}?pid={post.Id}", + Guid = post.WordPressId.HasValue ? $"{baseUrl}?p={post.WordPressId.Value}" : $"{baseUrl}?pid={post.Id}", Description = description }; blogItems.Add(item); @@ -68,7 +75,9 @@ internal sealed class RssMiddleware xmlNamespaces.Add("sy", "http://purl.org/rss/1.0/modules/syndication/"); xmlNamespaces.Add("slash", "http://purl.org/rss/1.0/modules/slash/"); - await using var writer = new StreamWriter(context.Response.BodyWriter.AsStream()); + using var writer = new StreamWriter(Response.BodyWriter.AsStream()); serializer.Serialize(writer, rss, xmlNamespaces); + + return Ok(); } } diff --git a/OliverBooth/Data/Blog/BlogPost.cs b/OliverBooth/Data/Blog/BlogPost.cs index eb0fe2c..ec8e877 100644 --- a/OliverBooth/Data/Blog/BlogPost.cs +++ b/OliverBooth/Data/Blog/BlogPost.cs @@ -37,6 +37,9 @@ internal sealed class BlogPost : IBlogPost /// public DateTimeOffset? Updated { get; internal set; } + /// + public int? WordPressId { get; set; } + /// /// Gets or sets the ID of the author of this blog post. /// @@ -61,12 +64,6 @@ internal sealed class BlogPost : IBlogPost /// The Disqus URL path. internal string? DisqusPath { get; set; } - /// - /// Gets or sets the WordPress ID of this blog post. - /// - /// The WordPress ID of this blog post. - internal int? WordPressId { get; set; } - /// /// Gets the Disqus domain for the blog post. /// diff --git a/OliverBooth/Data/Blog/IBlogPost.cs b/OliverBooth/Data/Blog/IBlogPost.cs index 177badc..9af773d 100644 --- a/OliverBooth/Data/Blog/IBlogPost.cs +++ b/OliverBooth/Data/Blog/IBlogPost.cs @@ -69,6 +69,14 @@ public interface IBlogPost /// The update date and time, or if the post has not been updated. DateTimeOffset? Updated { get; } + /// + /// Gets the WordPress ID of the post. + /// + /// + /// The WordPress ID of the post, or if the post was not imported from WordPress. + /// + int? WordPressId { get; } + /// /// Gets the Disqus identifier for the post. /// diff --git a/OliverBooth/Middleware/RssEndpointExtensions.cs b/OliverBooth/Middleware/RssEndpointExtensions.cs deleted file mode 100644 index 8b0d501..0000000 --- a/OliverBooth/Middleware/RssEndpointExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace OliverBooth.Middleware; - -internal static class RssEndpointExtensions -{ - public static IEndpointConventionBuilder MapRssFeed(this IEndpointRouteBuilder endpoints, string pattern) - { - RequestDelegate pipeline = endpoints.CreateApplicationBuilder() - .UseMiddleware() - .Build(); - - return endpoints.Map(pattern, pipeline).WithDisplayName("RSS Feed"); - } -}