81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
using Socialize.Infrastructure.Security;
|
|
|
|
namespace Socialize.Modules.Comments.Handlers;
|
|
|
|
public record GetCommentsRequest(Guid ContentItemId);
|
|
|
|
public record CommentDto(
|
|
Guid Id,
|
|
Guid WorkspaceId,
|
|
Guid ContentItemId,
|
|
Guid? ParentCommentId,
|
|
Guid AuthorUserId,
|
|
string AuthorDisplayName,
|
|
string AuthorEmail,
|
|
string? AuthorPortraitUrl,
|
|
string Body,
|
|
bool IsResolved,
|
|
DateTimeOffset CreatedAt,
|
|
DateTimeOffset? ResolvedAt);
|
|
|
|
public class GetCommentsHandler(
|
|
AppDbContext dbContext,
|
|
AccessScopeService accessScopeService)
|
|
: Endpoint<GetCommentsRequest, IReadOnlyCollection<CommentDto>>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/comments");
|
|
Options(o => o.WithTags("Comments"));
|
|
}
|
|
|
|
public override async Task HandleAsync(GetCommentsRequest request, CancellationToken ct)
|
|
{
|
|
ContentItem? item = await dbContext.ContentItems
|
|
.SingleOrDefaultAsync(candidate => candidate.Id == request.ContentItemId, ct);
|
|
if (item is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
if (!accessScopeService.CanReviewContent(User, item.WorkspaceId, item.ClientId, item.ProjectId))
|
|
{
|
|
await SendForbiddenAsync(ct);
|
|
return;
|
|
}
|
|
|
|
List<Comment> comments = await dbContext.Comments
|
|
.Where(comment => comment.ContentItemId == request.ContentItemId)
|
|
.OrderBy(comment => comment.CreatedAt)
|
|
.ToListAsync(ct);
|
|
|
|
List<Guid> authorIds = comments
|
|
.Select(comment => comment.AuthorUserId)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
Dictionary<Guid, string?> authorPortraits = await dbContext.Users
|
|
.Where(user => authorIds.Contains(user.Id))
|
|
.ToDictionaryAsync(user => user.Id, user => user.PortraitUrl, ct);
|
|
|
|
List<CommentDto> dtos = comments
|
|
.Select(comment => new CommentDto(
|
|
comment.Id,
|
|
comment.WorkspaceId,
|
|
comment.ContentItemId,
|
|
comment.ParentCommentId,
|
|
comment.AuthorUserId,
|
|
comment.AuthorDisplayName,
|
|
comment.AuthorEmail,
|
|
authorPortraits.GetValueOrDefault(comment.AuthorUserId),
|
|
comment.Body,
|
|
comment.IsResolved,
|
|
comment.CreatedAt,
|
|
comment.ResolvedAt))
|
|
.ToList();
|
|
|
|
await SendOkAsync(dtos, ct);
|
|
}
|
|
}
|