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; }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue