Skip to content

Instantly share code, notes, and snippets.

@undrcrxwn
Last active February 17, 2024 10:57
Show Gist options
  • Save undrcrxwn/8d4237a7c531084abbd7f303e5a41b00 to your computer and use it in GitHub Desktop.
Save undrcrxwn/8d4237a7c531084abbd7f303e5a41b00 to your computer and use it in GitHub Desktop.
var result = await query
.Match(matchSelector)
.With("COLLECT(author) AS authors, COLLECT(comment) AS comments, COUNT(comment) AS totalCount")
.Unwind("authors", "author")
.Unwind("comments", "comment")
.With("author, comment, totalCount")
.OrderBy("comment.CreatedAt")
.Skip(offset)
.Limit(count)
.OptionalMatch("(replyAuthor:Author)<-[:AUTHORED_BY]-(reply:Comment)-[:REPLIES_TO]->(comment)")
.OptionalMatch("(indirectReply:Comment)-[:REPLIES_TO*]->(reply)")
.With(
"""
totalCount, comment, author, COUNT(reply) + COUNT(indirectReply) AS replyCount,
CASE WHEN COUNT(reply) > 0 THEN COLLECT(DISTINCT {
Id: replyAuthor.Id,
Username: replyAuthor.Username,
DisplayName: replyAuthor.DisplayName,
AvatarUrl: replyAuthor.AvatarUrl
})[0..3] ELSE [] END AS firstRepliesAuthors
""")
.With(
"""
{
Id: comment.Id,
Content: comment.Content,
Author: {
Id: author.Id,
Username: author.Username,
DisplayName: author.DisplayName,
AvatarUrl: author.AvatarUrl
},
CreatedAt: datetime(comment.CreatedAt),
ReplyCount: replyCount,
FirstRepliesAuthors: firstRepliesAuthors
}
AS comment, totalCount
""")
.Return((comment, totalCount) => new Page<CommentDto>
{
TotalCount = totalCount.As<int>(),
Items = comment.CollectAs<CommentDto>()
})
.ResultsAsync;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment