comments
This commit is contained in:
parent
cbf67bf5f1
commit
ce3888f1ab
15 changed files with 162 additions and 21 deletions
|
@ -0,0 +1,5 @@
|
|||
using Femto.Common.Domain;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Commands.AddPostComment;
|
||||
|
||||
public record AddPostCommentCommand(Guid PostId, Guid AuthorId, string Content) : ICommand;
|
|
@ -0,0 +1,20 @@
|
|||
using Femto.Common.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Commands.AddPostComment;
|
||||
|
||||
internal class AddPostCommentCommandHandler(BlogContext context) : ICommandHandler<AddPostCommentCommand>
|
||||
{
|
||||
public async Task Handle(AddPostCommentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var post = await context.Posts.SingleOrDefaultAsync(
|
||||
p => p.Id == request.PostId,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
if (post is null)
|
||||
return;
|
||||
|
||||
post.AddComment(request.AuthorId, request.Content);
|
||||
}
|
||||
}
|
|
@ -24,11 +24,9 @@ internal class CreatePostCommandHandler(BlogContext context)
|
|||
media.Width,
|
||||
media.Height
|
||||
))
|
||||
.ToList()
|
||||
)
|
||||
{
|
||||
IsPublic = request.IsPublic is true
|
||||
};
|
||||
.ToList(),
|
||||
request.IsPublic is true
|
||||
);
|
||||
|
||||
await context.AddAsync(post, cancellationToken);
|
||||
|
||||
|
@ -39,7 +37,8 @@ internal class CreatePostCommandHandler(BlogContext context)
|
|||
post.PostedOn,
|
||||
new PostAuthorDto(post.AuthorId, request.CurrentUser.Username),
|
||||
[],
|
||||
post.PossibleReactions
|
||||
post.PossibleReactions,
|
||||
[]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,8 @@ internal class PostConfiguration : IEntityTypeConfiguration<Post>
|
|||
}
|
||||
);
|
||||
|
||||
table.OwnsMany(p => p.Comments).WithOwner();
|
||||
|
||||
table.Property<string>("PossibleReactionsJson").HasColumnName("possible_reactions");
|
||||
|
||||
table.Ignore(e => e.PossibleReactions);
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
||||
public record PostCommentDto(string Author, string Content, DateTimeOffset PostedOn);
|
|
@ -7,5 +7,6 @@ public record PostDto(
|
|||
DateTimeOffset CreatedAt,
|
||||
PostAuthorDto Author,
|
||||
IList<PostReactionDto> Reactions,
|
||||
IEnumerable<string> PossibleReactions
|
||||
);
|
||||
IEnumerable<string> PossibleReactions,
|
||||
IList<PostCommentDto> Comments
|
||||
);
|
|
@ -68,7 +68,7 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
|||
""",
|
||||
new { postIds }
|
||||
);
|
||||
|
||||
|
||||
var media = loadMediaResult.ToList();
|
||||
|
||||
var loadReactionsResult = await conn.QueryAsync<LoadReactionRow>(
|
||||
|
@ -87,16 +87,36 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
|||
|
||||
var reactions = loadReactionsResult.ToList();
|
||||
|
||||
var loadCommentsResult = await conn.QueryAsync<LoadCommentRow>(
|
||||
"""
|
||||
select
|
||||
pc.id as CommentId,
|
||||
pc.post_id as PostId,
|
||||
pc.content as Content,
|
||||
pc.created_at as PostedOn,
|
||||
a.username as AuthorName
|
||||
from blog.post_comment pc
|
||||
join blog.author a on pc.author_id = a.id
|
||||
where pc.post_id = ANY (@postIds)
|
||||
""",
|
||||
new { postIds }
|
||||
);
|
||||
|
||||
var comments = loadCommentsResult.ToList();
|
||||
|
||||
return new GetPostsQueryResult(
|
||||
posts
|
||||
.Select(p => new PostDto(
|
||||
p.PostId,
|
||||
p.Content,
|
||||
media.Where(m => m.PostId == p.PostId).Select(m => new PostMediaDto(
|
||||
new Uri(m.MediaUrl),
|
||||
m.MediaWidth,
|
||||
m.MediaHeight
|
||||
)).ToList(),
|
||||
media
|
||||
.Where(m => m.PostId == p.PostId)
|
||||
.Select(m => new PostMediaDto(
|
||||
new Uri(m.MediaUrl),
|
||||
m.MediaWidth,
|
||||
m.MediaHeight
|
||||
))
|
||||
.ToList(),
|
||||
p.PostedOn,
|
||||
new PostAuthorDto(p.AuthorId, p.Username),
|
||||
reactions
|
||||
|
@ -105,7 +125,11 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
|||
.ToList(),
|
||||
!string.IsNullOrEmpty(p.PossibleReactions)
|
||||
? JsonSerializer.Deserialize<IEnumerable<string>>(p.PossibleReactions)!
|
||||
: []
|
||||
: [],
|
||||
comments
|
||||
.Where(c => c.PostId == p.PostId)
|
||||
.Select(c => new PostCommentDto(c.AuthorName, c.Content, c.PostedOn))
|
||||
.ToList()
|
||||
))
|
||||
.ToList()
|
||||
);
|
||||
|
@ -137,4 +161,13 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
|||
public string Emoji { get; init; }
|
||||
public DateTimeOffset CreatedAt { get; init; }
|
||||
}
|
||||
|
||||
internal record LoadCommentRow
|
||||
{
|
||||
public Guid CommentId { get; init; }
|
||||
public Guid PostId { get; init; }
|
||||
public string Content { get; init; }
|
||||
public DateTimeOffset PostedOn { get; init; }
|
||||
public string AuthorName { get; init; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,9 @@ internal class Post : Entity
|
|||
public IList<PostMedia> Media { get; private set; }
|
||||
|
||||
public IList<PostReaction> Reactions { get; private set; } = [];
|
||||
public bool IsPublic { get; set; }
|
||||
|
||||
public IList<PostComment> Comments { get; private set; } = [];
|
||||
public bool IsPublic { get; private set; }
|
||||
|
||||
public DateTimeOffset PostedOn { get; private set; }
|
||||
|
||||
|
@ -27,7 +29,7 @@ internal class Post : Entity
|
|||
|
||||
private Post() { }
|
||||
|
||||
public Post(Guid authorId, string content, IList<PostMedia> media)
|
||||
public Post(Guid authorId, string content, IList<PostMedia> media, bool isPublic)
|
||||
{
|
||||
this.Id = Guid.CreateVersion7();
|
||||
this.AuthorId = authorId;
|
||||
|
@ -35,6 +37,7 @@ internal class Post : Entity
|
|||
this.Media = media;
|
||||
this.PossibleReactions = AllEmoji.GetRandomEmoji(5);
|
||||
this.PostedOn = DateTimeOffset.UtcNow;
|
||||
this.IsPublic = isPublic;
|
||||
|
||||
this.AddDomainEvent(new PostCreated(this));
|
||||
}
|
||||
|
@ -56,4 +59,14 @@ internal class Post : Entity
|
|||
.Reactions.Where(r => r.AuthorId != reactorId || r.Emoji != emoji)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void AddComment(Guid authorId, string content)
|
||||
{
|
||||
// XXX just ignore empty comments for now. we may want to upgrade this to an error
|
||||
// but it is probably suitable to just consider it a no-op
|
||||
if (string.IsNullOrWhiteSpace(content))
|
||||
return;
|
||||
|
||||
this.Comments.Add(new PostComment(authorId, content));
|
||||
}
|
||||
}
|
||||
|
|
19
Femto.Modules.Blog/Domain/Posts/PostComment.cs
Normal file
19
Femto.Modules.Blog/Domain/Posts/PostComment.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
namespace Femto.Modules.Blog.Domain.Posts;
|
||||
|
||||
internal class PostComment
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public Guid AuthorId { get; private set; }
|
||||
public DateTimeOffset CreatedAt { get; private set; }
|
||||
public string Content { get; private set; }
|
||||
|
||||
private PostComment() {}
|
||||
|
||||
public PostComment(Guid authorId, string content)
|
||||
{
|
||||
this.Id = Guid.CreateVersion7();
|
||||
this.AuthorId = authorId;
|
||||
this.Content = content;
|
||||
this.CreatedAt = TimeProvider.System.GetUtcNow();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue