load with public flag

This commit is contained in:
john 2025-05-18 13:40:05 +02:00
parent e3c95eb109
commit 322dd01ee0
14 changed files with 73 additions and 38 deletions

View file

@ -3,23 +3,22 @@ using Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
namespace Femto.Modules.Blog.Application.Queries.GetPosts;
public class GetPostsQuery : IQuery<GetPostsQueryResult>
public record GetPostsQuery(Guid? CurrentUserId) : IQuery<GetPostsQueryResult>
{
public Guid? From { get; init; }
public int Amount { get; init; } = 20;
public Guid? AuthorId { get; init; }
public string? Author { get; set; }
public string? Author { get; init; }
/// <summary>
/// Default is to load in reverse chronological order
/// TODO this is not exposed on the client as it probably wouldn't work that well
/// </summary>
public GetPostsDirection Direction { get; init; } = GetPostsDirection.Backward;
}
public enum GetPostsDirection
{
Forward,
Backward
}
Backward,
}

View file

@ -17,6 +17,10 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
var orderBy = query.Direction is GetPostsDirection.Backward ? "desc" : "asc";
var pageFilter = query.Direction is GetPostsDirection.Backward ? "<=" : ">=";
var username = query.Author;
var authorGuid = query.AuthorId;
var cursor = query.From;
var showPrivate = query.CurrentUserId is not null;
// lang=sql
var sql = $$"""
@ -25,6 +29,7 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
from blog.post
inner join blog.author on blog.author.id = blog.post.author_id
where (@username is null or blog.author.username = @username)
and (@showPrivate or blog.post.is_public = true)
and (@authorGuid is null or blog.author.id = @authorGuid)
and (@cursor is null or blog.post.id {{pageFilter}} @cursor)
order by blog.post.id {{orderBy}}
@ -48,11 +53,12 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
sql,
new
{
username = query.Author,
authorGuid = query.AuthorId,
cursor = query.From,
// load an extra one to take for the curst
username,
authorGuid,
cursor,
// load an extra one to take for the cursor
amount = query.Amount + 1,
showPrivate,
}
);