64 lines
2.5 KiB
C#
64 lines
2.5 KiB
C#
namespace Netina.Repository.Handlers.Comments;
|
|
|
|
public class GetCommentsQueryHandler(IRepositoryWrapper repositoryWrapper)
|
|
: IRequestHandler<GetCommentsQuery, List<CommentSDto>>
|
|
{
|
|
public async Task<List<CommentSDto>> Handle(GetCommentsQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var count = request.Count == 0 ? Refers.SizeL : request.Count;
|
|
if (count > 50)
|
|
throw new BaseApiException(ApiResultStatusCode.BadRequest, "Count limit is 50");
|
|
|
|
if (request.BlogId != null)
|
|
{
|
|
var blogQuery = repositoryWrapper.SetRepository<BlogComment>().TableNoTracking
|
|
.Where(b => b.IsRoot && b.BlogId == request.BlogId)
|
|
.Skip(request.Page * count)
|
|
.Take(count)
|
|
.Select(CommentMapper.ProjectToSDto);
|
|
var blogRoots = await blogQuery.ToListAsync(cancellationToken);
|
|
foreach (var root in blogRoots)
|
|
await LoadChildrenAsync(root, cancellationToken);
|
|
return blogRoots;
|
|
}
|
|
|
|
if (request.ProductId != null)
|
|
{
|
|
|
|
var blogQuery = repositoryWrapper.SetRepository<ProductComment>().TableNoTracking
|
|
.Where(b => b.IsRoot && b.ProductId == request.ProductId)
|
|
.Skip(request.Page * count)
|
|
.Take(count)
|
|
.Select(CommentMapper.ProjectToSDto);
|
|
var blogRoots = await blogQuery.ToListAsync(cancellationToken);
|
|
foreach (var root in blogRoots)
|
|
await LoadChildrenAsync(root, cancellationToken);
|
|
return blogRoots;
|
|
}
|
|
|
|
var query = repositoryWrapper.SetRepository<Comment>().TableNoTracking
|
|
.Where(b => b.IsRoot)
|
|
.Skip(request.Page * count)
|
|
.Take(count)
|
|
.Select(CommentMapper.ProjectToSDto);
|
|
var roots = await query.ToListAsync(cancellationToken);
|
|
foreach (var root in roots)
|
|
await LoadChildrenAsync(root, cancellationToken);
|
|
|
|
return roots;
|
|
}
|
|
|
|
private async Task LoadChildrenAsync(CommentSDto comment, CancellationToken cancellationToken)
|
|
{
|
|
var children = await repositoryWrapper.SetRepository<Comment>()
|
|
.TableNoTracking
|
|
.Where(c => c.ParentId == comment.Id)
|
|
.Select(CommentMapper.ProjectToSDto)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
foreach (var blogComment in children)
|
|
await LoadChildrenAsync(blogComment, cancellationToken);
|
|
|
|
comment.Children = children;
|
|
}
|
|
} |