feat: add visibility search to GetAllBlogPosts

This commit is contained in:
Oliver Booth 2024-02-25 14:15:21 +00:00
parent 9593979d7b
commit 1d1acd2a40
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
2 changed files with 12 additions and 5 deletions

View File

@ -41,12 +41,17 @@ internal sealed class BlogPostService : IBlogPostService
} }
/// <inheritdoc /> /// <inheritdoc />
public IReadOnlyList<IBlogPost> GetAllBlogPosts(int limit = -1) public IReadOnlyList<IBlogPost> GetAllBlogPosts(int limit = -1,
BlogPostVisibility visibility = BlogPostVisibility.Published)
{ {
using BlogContext context = _dbContextFactory.CreateDbContext(); using BlogContext context = _dbContextFactory.CreateDbContext();
IQueryable<BlogPost> ordered = context.BlogPosts IQueryable<BlogPost> ordered = context.BlogPosts;
.Where(p => p.Visibility == BlogPostVisibility.Published) if (visibility != (BlogPostVisibility)(-1))
.OrderByDescending(post => post.Published); {
ordered = ordered.Where(p => p.Visibility == visibility);
}
ordered = ordered.OrderByDescending(post => post.Published);
if (limit > -1) if (limit > -1)
{ {
ordered = ordered.Take(limit); ordered = ordered.Take(limit);

View File

@ -12,12 +12,14 @@ public interface IBlogPostService
/// Returns a collection of all blog posts. /// Returns a collection of all blog posts.
/// </summary> /// </summary>
/// <param name="limit">The maximum number of posts to return. A value of -1 returns all posts.</param> /// <param name="limit">The maximum number of posts to return. A value of -1 returns all posts.</param>
/// <param name="visibility">The visibility of the posts to retrieve.</param>
/// <returns>A collection of all blog posts.</returns> /// <returns>A collection of all blog posts.</returns>
/// <remarks> /// <remarks>
/// This method may slow down execution if there are a large number of blog posts being requested. It is /// This method may slow down execution if there are a large number of blog posts being requested. It is
/// recommended to use <see cref="GetBlogPosts" /> instead. /// recommended to use <see cref="GetBlogPosts" /> instead.
/// </remarks> /// </remarks>
IReadOnlyList<IBlogPost> GetAllBlogPosts(int limit = -1); IReadOnlyList<IBlogPost> GetAllBlogPosts(int limit = -1,
BlogPostVisibility visibility = BlogPostVisibility.Published);
/// <summary> /// <summary>
/// Returns the total number of blog posts. /// Returns the total number of blog posts.