Skip to content

Commit

Permalink
feat(discussions): merge get comments by discussion/author endpoints …
Browse files Browse the repository at this point in the history
…into search endpoint
  • Loading branch information
undrcrxwn committed Jan 26, 2024
1 parent 8f233e5 commit 9b7dd17
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CrowdParlay.Social.Application.DTOs;
using CrowdParlay.Social.Application.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace CrowdParlay.Social.Api.v1.Controllers;

Expand All @@ -22,12 +23,12 @@ public async Task<CommentDto> GetCommentById([FromRoute] Guid commentId) =>
await _comments.GetByIdAsync(commentId);

/// <summary>
/// Returns all comments created by author with the specified ID.
/// Returns all comments created in discussion or by author with the specified ID.
/// </summary>
[HttpGet]
public async Task<IEnumerable<CommentDto>> GetCommentsByAuthor
([FromQuery] Guid authorId, [FromQuery] int page, [FromQuery] int size) =>
await _comments.GetByAuthorAsync(authorId, page, size);
public async Task<IEnumerable<CommentDto>> SearchComments
([FromQuery] Guid? discussionId, [FromQuery] Guid? authorId, [FromQuery, BindRequired] int page, [FromQuery, BindRequired] int size) =>
await _comments.SearchAsync(discussionId, authorId, page, size);

/// <summary>
/// Creates a top-level comment in discussion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CrowdParlay.Social.Application.Abstractions;
public interface ICommentRepository
{
public Task<CommentDto> GetByIdAsync(Guid id);
public Task<IEnumerable<CommentDto>> GetByAuthorAsync(Guid authorId, int page, int size);
public Task<IEnumerable<CommentDto>> SearchAsync(Guid? discussionId, Guid? authorId, int page, int size);
public Task<CommentDto> CreateAsync(Guid authorId, Guid discussionId, string content);
public Task<CommentDto> ReplyToCommentAsync(Guid authorId, Guid parentCommentId, string content);
public Task DeleteAsync(Guid id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ AS c
?? throw new NotFoundException();
}

public async Task<IEnumerable<CommentDto>> GetByAuthorAsync(Guid authorId, int page, int size) => await _graphClient.Cypher
.WithParams(new { authorId })
.Match("(c:Comment)-[:AUTHORED_BY]->(a:Author { Id: $authorId })")
public async Task<IEnumerable<CommentDto>> GetByDiscussionAsync(Guid discussionId, int page, int size) => await _graphClient.Cypher
.WithParams(new { discussionId })
.Match("(a:Author)<-[:AUTHORED_BY]-(c:Comment)-[:REPLIES_TO]->(d:Discussion { Id: $discussionId })")
.OptionalMatch("(ra:Author)<-[:AUTHORED_BY]-(r:Comment)-[:REPLIES_TO]->(c)")
.With(
"""
c, a, COUNT(r) AS rc,
c, a, d, COUNT(r) AS rc,
CASE WHEN COUNT(r) > 0 THEN COLLECT(DISTINCT {
Id: ra.Id,
Username: ra.Username,
Expand Down Expand Up @@ -88,6 +88,54 @@ AS c
.Limit(size)
.ResultsAsync;

public async Task<IEnumerable<CommentDto>> SearchAsync(Guid? discussionId, Guid? authorId, int page, int size)
{
var query = _graphClient.Cypher.WithParams(new { discussionId, authorId });

var matchSelector = (discussionId, authorId) switch
{
(not null, not null) => "(a:Author { Id: $authorId })<-[:AUTHORED_BY]-(c:Comment)-[:REPLIES_TO]->(d:Discussion { Id: $discussionId })",
(not null, null) => "(a:Author)<-[:AUTHORED_BY]-(c:Comment)-[:REPLIES_TO]->(d:Discussion { Id: $discussionId })",
(null, not null) => "(a:Author { Id: $authorId })<-[:AUTHORED_BY]-(c:Comment)",
(null, null) => "(a:Author)<-[:AUTHORED_BY]-(c:Comment)"
};

return await query
.Match(matchSelector)
.OptionalMatch("(ra:Author)<-[:AUTHORED_BY]-(r:Comment)-[:REPLIES_TO]->(c)")
.With(
"""
c, a, COUNT(r) AS rc,
CASE WHEN COUNT(r) > 0 THEN COLLECT(DISTINCT {
Id: ra.Id,
Username: ra.Username,
DisplayName: ra.DisplayName,
AvatarUrl: ra.AvatarUrl
})[0..3] ELSE [] END AS fras
""")
.With(
"""
{
Id: c.Id,
Content: c.Content,
Author: {
Id: a.Id,
Username: a.Username,
DisplayName: a.DisplayName,
AvatarUrl: a.AvatarUrl
},
CreatedAt: c.CreatedAt,
ReplyCount: rc,
FirstRepliesAuthors: fras
}
AS c
""")
.Return<CommentDto>("c")
.Skip(page * size)
.Limit(size)
.ResultsAsync;
}

public async Task<CommentDto> CreateAsync(Guid authorId, Guid discussionId, string content)
{
var results = await _graphClient.Cypher
Expand Down

0 comments on commit 9b7dd17

Please sign in to comment.