Files
mws.backend.dotnet/mws.application/Documents/Queries/GetDocumentTree.cs
T
namdhandClaude Sonnet 5 c3384d76d9 Convert Documents module to MediatR commands/queries
Replaces IDocumentService/DocumentService. The permission-check and
descendant-deletion logic moves to a shared DocumentAccess static
helper used by all seven handlers.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-17 21:03:16 +07:00

36 lines
1.1 KiB
C#

using AutoMapper;
using MediatR;
using Mws.Application.Common;
using Mws.Application.Permissions;
namespace Mws.Application.Documents;
public record GetDocumentTreeQuery(Guid UserId, Guid ProjectId) : IRequest<List<DocumentNodeDto>>;
public class GetDocumentTreeHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetDocumentTreeQuery, List<DocumentNodeDto>>
{
public async Task<List<DocumentNodeDto>> Handle(GetDocumentTreeQuery query, CancellationToken ct)
{
await DocumentAccess.EnsureDocumentPermissionAsync(uow, query.UserId, query.ProjectId, PermissionAction.View, ct);
var docs = await uow.Documents.GetTreeForProjectAsync(query.ProjectId, ct);
var nodes = docs.ToDictionary(d => d.Id, d => mapper.Map<DocumentNodeDto>(d));
var roots = new List<DocumentNodeDto>();
foreach (var node in nodes.Values)
{
if (node.ParentId is { } parentId && nodes.TryGetValue(parentId, out var parent))
{
parent.Children.Add(node);
}
else
{
roots.Add(node);
}
}
return roots;
}
}