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

@ -2,7 +2,7 @@ using Femto.Common.Domain;
namespace Femto.Modules.Blog.Application.Commands.CreatePost;
public record CreatePostCommand(Guid AuthorId, string Content, IEnumerable<CreatePostMedia> Media)
public record CreatePostCommand(Guid AuthorId, string Content, IEnumerable<CreatePostMedia> Media, bool? IsPublic)
: ICommand<Guid>;
public record CreatePostMedia(Guid MediaId, Uri Url, string? Type, int Order, int? Width, int? Height);

View file

@ -22,6 +22,8 @@ internal class CreatePostCommandHandler(BlogContext context)
))
.ToList()
);
post.IsPublic = request.IsPublic is true;
await context.AddAsync(post, cancellationToken);

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,
}
);

View file

@ -1,6 +1,5 @@
using Femto.Common.Domain;
using Femto.Modules.Blog.Domain.Posts.Events;
using Femto.Modules.Blog.Domain.Posts.Rules;
namespace Femto.Modules.Blog.Domain.Posts;
@ -10,16 +9,17 @@ internal class Post : Entity
public Guid AuthorId { get; private set; }
public string Content { get; private set; } = null!;
public IList<PostMedia> Media { get; private set; }
public bool IsPublic { get; set; }
private Post() { }
public Post(Guid authorId,string content, IList<PostMedia> media)
public Post(Guid authorId, string content, IList<PostMedia> media)
{
this.Id = Guid.CreateVersion7();
this.AuthorId = authorId;
this.Content = content;
this.Media = media;
this.AddDomainEvent(new PostCreated(this));
}
}
}