Skip to content

Commit

Permalink
feat(comments): rename FirstRepliesAuthors to LastRepliesAuthors
Browse files Browse the repository at this point in the history
  • Loading branch information
undrcrxwn committed Nov 20, 2024
1 parent 691ce08 commit de80b9a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/CrowdParlay.Social.Application/DTOs/CommentResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class CommentResponse
public required AuthorResponse? Author { get; set; }
public required DateTimeOffset CreatedAt { get; set; }
public required int ReplyCount { get; set; }
public required IEnumerable<AuthorResponse> FirstRepliesAuthors { get; set; }
public required IEnumerable<AuthorResponse> LastRepliesAuthors { get; set; }
public required IDictionary<string, int> ReactionCounters { get; set; }
public required ISet<string> ViewerReactions { get; set; }
}
26 changes: 5 additions & 21 deletions src/CrowdParlay.Social.Application/Services/CommentsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,14 @@ public async Task<CommentResponse> ReplyToCommentAsync(Guid authorId, Guid paren

return await EnrichAsync(comment);
}

public async Task DeleteAsync(Guid id) => await commentsRepository.DeleteAsync(id);

private async Task<CommentResponse> EnrichAsync(Comment comment)
{
var author = await usersService.GetByIdAsync(comment.AuthorId);
var firstRepliesAuthors = await usersService.GetUsersAsync(comment.FirstRepliesAuthorIds);
public async Task DeleteAsync(Guid id) => await commentsRepository.DeleteAsync(id);

return new CommentResponse
{
Id = comment.Id,
Content = comment.Content,
Author = author.Adapt<AuthorResponse>(),
CreatedAt = comment.CreatedAt,
ReplyCount = comment.ReplyCount,
FirstRepliesAuthors = firstRepliesAuthors.Values.Adapt<IEnumerable<AuthorResponse>>(),
ReactionCounters = comment.ReactionCounters,
ViewerReactions = comment.ViewerReactions
};
}
private async Task<CommentResponse> EnrichAsync(Comment comment) => (await EnrichAsync([comment])).First();

private async Task<IEnumerable<CommentResponse>> EnrichAsync(IReadOnlyList<Comment> comments)
{
var authorIds = comments.SelectMany(comment => comment.FirstRepliesAuthorIds.Append(comment.AuthorId)).ToHashSet();
var authorIds = comments.SelectMany(comment => comment.LastRepliesAuthorIds.Append(comment.AuthorId)).ToHashSet();
var authorsById = await usersService.GetUsersAsync(authorIds);

return comments.Select(comment => new CommentResponse
Expand All @@ -103,10 +87,10 @@ private async Task<IEnumerable<CommentResponse>> EnrichAsync(IReadOnlyList<Comme
Author = authorsById[comment.AuthorId].Adapt<AuthorResponse>(),
CreatedAt = comment.CreatedAt,
ReplyCount = comment.ReplyCount,
FirstRepliesAuthors = comment.FirstRepliesAuthorIds
LastRepliesAuthors = comment.LastRepliesAuthorIds
.Select(replyAuthorId => authorsById[replyAuthorId].Adapt<AuthorResponse>()),
ReactionCounters = comment.ReactionCounters,
ViewerReactions = comment.ViewerReactions
});
}
}
}
2 changes: 1 addition & 1 deletion src/CrowdParlay.Social.Domain/DTOs/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class Page<T>
public static Page<T> Empty => new()
{
TotalCount = 0,
Items = Enumerable.Empty<T>()
Items = []
};
}
2 changes: 1 addition & 1 deletion src/CrowdParlay.Social.Domain/Entities/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Comment
public required Guid AuthorId { get; set; }
public required DateTimeOffset CreatedAt { get; set; }
public required int ReplyCount { get; set; }
public required ISet<Guid> FirstRepliesAuthorIds { get; set; }
public required ISet<Guid> LastRepliesAuthorIds { get; set; }
public required IDictionary<string, int> ReactionCounters { get; set; }
public required ISet<string> ViewerReactions { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ ORDER BY deepReply.CreatedAt DESC
WITH author, comment, viewerReactions, reactionCounters,
count(deepReply) AS deepReplyCount,
collect(DISTINCT deepReplyAuthor.Id)[0..3] AS firstDeepRepliesAuthorIds
collect(DISTINCT deepReplyAuthor.Id)[0..3] AS lastDeepRepliesAuthorIds
RETURN {
Id: comment.Id,
Content: comment.Content,
AuthorId: author.Id,
CreatedAt: comment.CreatedAt,
ReplyCount: deepReplyCount,
FirstRepliesAuthorIds: firstDeepRepliesAuthorIds,
LastRepliesAuthorIds: lastDeepRepliesAuthorIds,
ReactionCounters: reactionCounters,
ViewerReactions: viewerReactions
}
Expand Down Expand Up @@ -92,7 +92,7 @@ OPTIONAL MATCH (comment)<-[viewerReaction:REACTED_TO]-(:Author { Id: $viewerId }
WITH author, comment, viewerReactions, reactionCounters,
COUNT(deepReply) AS deepReplyCount,
COLLECT(DISTINCT deepReplyAuthor.Id)[0..3] AS firstDeepRepliesAuthorIds
COLLECT(DISTINCT deepReplyAuthor.Id)[0..3] AS lastDeepRepliesAuthorIds
RETURN {
TotalCount: COUNT(comment),
Expand All @@ -102,7 +102,7 @@ OPTIONAL MATCH (comment)<-[viewerReaction:REACTED_TO]-(:Author { Id: $viewerId }
AuthorId: author.Id,
CreatedAt: comment.CreatedAt,
ReplyCount: deepReplyCount,
FirstRepliesAuthorIds: firstDeepRepliesAuthorIds,
LastRepliesAuthorIds: lastDeepRepliesAuthorIds,
ReactionCounters: reactionCounters,
ViewerReactions: viewerReactions
})[$offset..$offset + $count]
Expand All @@ -122,7 +122,7 @@ OPTIONAL MATCH (comment)<-[viewerReaction:REACTED_TO]-(:Author { Id: $viewerId }
return new Page<Comment>
{
TotalCount = 0,
Items = Enumerable.Empty<Comment>()
Items = []
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task CreateComment()
comment.AuthorId.Should().Be(authorId);
comment.Content.Should().Be("Comment content");
comment.ReplyCount.Should().Be(0);
comment.FirstRepliesAuthorIds.Should().BeEmpty();
comment.LastRepliesAuthorIds.Should().BeEmpty();
comment.CreatedAt.Should().BeCloseTo(DateTime.Now, TimeSpan.FromMinutes(1));
comment.ReactionCounters.Should().BeEmpty();
comment.ViewerReactions.Should().BeEmpty();
Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task SearchComments()
page.TotalCount.Should().Be(3);
page.Items.Should().HaveCount(2);
page.Items.Should().BeEquivalentTo([comment1, comment2]);
page.Items.First().FirstRepliesAuthorIds.Should().BeEquivalentTo([authorId4, authorId2, authorId1]);
page.Items.First().LastRepliesAuthorIds.Should().BeEquivalentTo([authorId4, authorId2, authorId1]);
page.Items.Should().OnlyContain(comment => comment.ReactionCounters.Count == 0);
page.Items.Should().OnlyContain(comment => comment.ViewerReactions.Count == 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task ListenToNewCommentsInDiscussion()
},
CreatedAt = DateTimeOffset.Now,
ReplyCount = 0,
FirstRepliesAuthors = [],
LastRepliesAuthors = [],
ReactionCounters = new Dictionary<string, int>(),
ViewerReactions = new HashSet<string>()
};
Expand Down

0 comments on commit de80b9a

Please sign in to comment.