-
-
Save undrcrxwn/93434258fa9186e3f31bcd20bd3b5e81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var query = _graphClient.Cypher.WithParams(new { discussionId, authorId }); | |
var matchSelector = authorId is not null | |
? "(author:Author { Id: $authorId })<-[:AUTHORED_BY]-(comment:Comment)" | |
: "(author:Author)<-[:AUTHORED_BY]-(comment:Comment)"; | |
if (discussionId is not null) | |
matchSelector += "-[:REPLIES_TO]->(discussion:Discussion { Id: $discussionId })"; | |
var pages = 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; | |
return pages.SingleOrDefault() ?? Page<CommentDto>.Empty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment