return postdto from post create
This commit is contained in:
parent
8ad4302ec8
commit
d1687f276b
15 changed files with 226 additions and 111 deletions
|
@ -6,4 +6,6 @@ public record PostDto(
|
|||
IList<PostMediaDto> Media,
|
||||
DateTimeOffset CreatedAt,
|
||||
PostAuthorDto Author,
|
||||
IList<PostReactionDto> Reactions);
|
||||
IList<PostReactionDto> Reactions,
|
||||
IEnumerable<string> PossibleReactions
|
||||
);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System.Text.Json;
|
||||
using Dapper;
|
||||
using Femto.Common.Infrastructure.DbConnection;
|
||||
using Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
@ -15,42 +16,29 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
|||
{
|
||||
using var conn = connectionFactory.GetConnection();
|
||||
|
||||
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 = $$"""
|
||||
with page as (
|
||||
select blog.post.*, blog.author.username as Username, blog.author.id as AuthorId
|
||||
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}}
|
||||
limit @amount
|
||||
)
|
||||
select
|
||||
page.id as PostId,
|
||||
page.content as Content,
|
||||
blog.post_media.url as MediaUrl,
|
||||
blog.post_media.width as MediaWidth,
|
||||
blog.post_media.height as MediaHeight,
|
||||
page.posted_on as PostedOn,
|
||||
page.Username,
|
||||
page.AuthorId
|
||||
from page
|
||||
left join blog.post_media on blog.post_media.post_id = page.id
|
||||
order by page.id {{orderBy}}
|
||||
""";
|
||||
|
||||
var result = await conn.QueryAsync<QueryResult>(
|
||||
sql,
|
||||
var loadPostsResult = await conn.QueryAsync<LoadPostRow>(
|
||||
"""
|
||||
select
|
||||
blog.post.id as PostId,
|
||||
blog.post.content as Content,
|
||||
blog.post.posted_on as PostedOn,
|
||||
blog.author.username as Username,
|
||||
blog.author.id as AuthorId,
|
||||
blog.post.possible_reactions as PossibleReactions
|
||||
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 <= @cursor)
|
||||
order by blog.post.id desc
|
||||
limit @amount
|
||||
""",
|
||||
new
|
||||
{
|
||||
username,
|
||||
|
@ -62,54 +50,115 @@ public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
|||
}
|
||||
);
|
||||
|
||||
var rows = result.ToList();
|
||||
var loadedPosts = loadPostsResult.ToList();
|
||||
var posts = loadedPosts.Take(query.Amount).ToList();
|
||||
var next = loadedPosts.LastOrDefault()?.PostId;
|
||||
|
||||
var posts = rows.GroupBy(row => row.PostId)
|
||||
.Select(group =>
|
||||
{
|
||||
var postId = group.Key;
|
||||
var post = group.First();
|
||||
var media = group
|
||||
.Select(row =>
|
||||
{
|
||||
if (row.MediaUrl is not null)
|
||||
{
|
||||
return new PostMediaDto(
|
||||
new Uri(row.MediaUrl),
|
||||
row.MediaHeight,
|
||||
row.MediaHeight
|
||||
);
|
||||
}
|
||||
else
|
||||
return null;
|
||||
})
|
||||
.OfType<PostMediaDto>()
|
||||
.ToList();
|
||||
return new PostDto(
|
||||
postId,
|
||||
post.Content,
|
||||
media,
|
||||
post.PostedOn,
|
||||
new PostAuthorDto(post.AuthorId, post.Username)
|
||||
);
|
||||
})
|
||||
.ToList();
|
||||
var postIds = posts.Select(p => p.PostId).ToList();
|
||||
|
||||
var next = rows.Count >= query.Amount ? rows.LastOrDefault()?.PostId : null;
|
||||
var loadMediaResult = await conn.QueryAsync<LoadMediaRow>(
|
||||
"""
|
||||
select
|
||||
pm.url as MediaUrl,
|
||||
pm.type as MediaType,
|
||||
pm.width as MediaWidth,
|
||||
pm.height as MediaHeight,
|
||||
pm.post_id as PostId
|
||||
from blog.post_media pm where pm.post_id = ANY (@postIds)
|
||||
order by pm.ordering
|
||||
""",
|
||||
new { postIds }
|
||||
);
|
||||
|
||||
return new GetPostsQueryResult(posts, next);
|
||||
var loadReactionsResult = await conn.QueryAsync<LoadReactionRow>(
|
||||
"""
|
||||
select
|
||||
pr.post_id as PostId,
|
||||
pr.author_id as AuthorId,
|
||||
pr.emoji as Emoji
|
||||
from blog.post_reaction pr
|
||||
where pr.post_id = ANY (@postIds)
|
||||
""",
|
||||
new { postIds }
|
||||
);
|
||||
|
||||
var reactionsByPostId = loadReactionsResult
|
||||
.GroupBy(r => r.PostId)
|
||||
.ToDictionary(
|
||||
group => group.Key,
|
||||
group =>
|
||||
group
|
||||
.GroupBy(
|
||||
r => r.Emoji,
|
||||
(key, g) =>
|
||||
{
|
||||
var reactions = g.ToList();
|
||||
return new PostReactionDto(
|
||||
key,
|
||||
reactions.Count,
|
||||
reactions.Any(r => r.AuthorId == query.CurrentUserId)
|
||||
);
|
||||
}
|
||||
)
|
||||
.ToList()
|
||||
);
|
||||
|
||||
var mediaByPostId = loadMediaResult
|
||||
.GroupBy(m => m.PostId)
|
||||
.ToDictionary(
|
||||
g => g.Key,
|
||||
g =>
|
||||
g.Select(m => new PostMediaDto(
|
||||
new Uri(m.MediaUrl),
|
||||
m.MediaWidth,
|
||||
m.MediaHeight
|
||||
))
|
||||
.ToList()
|
||||
);
|
||||
|
||||
return new GetPostsQueryResult(
|
||||
posts
|
||||
.Select(p => new PostDto(
|
||||
p.PostId,
|
||||
p.Content,
|
||||
mediaByPostId.TryGetValue(p.PostId, out var mediaDtos) ? mediaDtos : [],
|
||||
p.PostedOn,
|
||||
new PostAuthorDto(p.AuthorId, p.Username),
|
||||
reactionsByPostId.TryGetValue(p.PostId, out var reactionDtos)
|
||||
? reactionDtos.ToList()
|
||||
: [],
|
||||
!string.IsNullOrEmpty(p.PossibleReactions)
|
||||
? JsonSerializer.Deserialize<IEnumerable<string>>(p.PossibleReactions)!
|
||||
: []
|
||||
))
|
||||
.ToList(),
|
||||
next
|
||||
);
|
||||
}
|
||||
|
||||
internal class QueryResult
|
||||
internal record LoadPostRow
|
||||
{
|
||||
public Guid PostId { get; set; }
|
||||
public string Content { get; set; }
|
||||
public string? MediaUrl { get; set; }
|
||||
public string? MediaType { get; set; }
|
||||
public int? MediaWidth { get; set; }
|
||||
public int? MediaHeight { get; set; }
|
||||
public DateTimeOffset PostedOn { get; set; }
|
||||
public Guid AuthorId { get; set; }
|
||||
public string Username { get; set; }
|
||||
public Guid PostId { get; init; }
|
||||
public string Content { get; init; }
|
||||
public DateTimeOffset PostedOn { get; init; }
|
||||
public string Username { get; init; }
|
||||
public Guid AuthorId { get; init; }
|
||||
public string? PossibleReactions { get; init; }
|
||||
}
|
||||
|
||||
internal record LoadMediaRow
|
||||
{
|
||||
public string MediaUrl { get; init; }
|
||||
public string? MediaType { get; init; }
|
||||
public int? MediaWidth { get; init; }
|
||||
public int? MediaHeight { get; init; }
|
||||
public Guid PostId { get; init; }
|
||||
}
|
||||
|
||||
internal record LoadReactionRow
|
||||
{
|
||||
public Guid PostId { get; init; }
|
||||
public Guid AuthorId { get; init; }
|
||||
public string Emoji { get; init; }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue