From 1d1acd2a408292445d67317ae574d24de359beb1 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 25 Feb 2024 14:15:21 +0000 Subject: [PATCH] feat: add visibility search to GetAllBlogPosts --- OliverBooth/Services/BlogPostService.cs | 13 +++++++++---- OliverBooth/Services/IBlogPostService.cs | 4 +++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/OliverBooth/Services/BlogPostService.cs b/OliverBooth/Services/BlogPostService.cs index 9d2be3e..78a4ad9 100644 --- a/OliverBooth/Services/BlogPostService.cs +++ b/OliverBooth/Services/BlogPostService.cs @@ -41,12 +41,17 @@ internal sealed class BlogPostService : IBlogPostService } /// - public IReadOnlyList GetAllBlogPosts(int limit = -1) + public IReadOnlyList GetAllBlogPosts(int limit = -1, + BlogPostVisibility visibility = BlogPostVisibility.Published) { using BlogContext context = _dbContextFactory.CreateDbContext(); - IQueryable ordered = context.BlogPosts - .Where(p => p.Visibility == BlogPostVisibility.Published) - .OrderByDescending(post => post.Published); + IQueryable ordered = context.BlogPosts; + if (visibility != (BlogPostVisibility)(-1)) + { + ordered = ordered.Where(p => p.Visibility == visibility); + } + + ordered = ordered.OrderByDescending(post => post.Published); if (limit > -1) { ordered = ordered.Take(limit); diff --git a/OliverBooth/Services/IBlogPostService.cs b/OliverBooth/Services/IBlogPostService.cs index bcb48ee..8383872 100644 --- a/OliverBooth/Services/IBlogPostService.cs +++ b/OliverBooth/Services/IBlogPostService.cs @@ -12,12 +12,14 @@ public interface IBlogPostService /// Returns a collection of all blog posts. /// /// The maximum number of posts to return. A value of -1 returns all posts. + /// The visibility of the posts to retrieve. /// A collection of all blog posts. /// /// This method may slow down execution if there are a large number of blog posts being requested. It is /// recommended to use instead. /// - IReadOnlyList GetAllBlogPosts(int limit = -1); + IReadOnlyList GetAllBlogPosts(int limit = -1, + BlogPostVisibility visibility = BlogPostVisibility.Published); /// /// Returns the total number of blog posts.