Files

37 lines
1.2 KiB
C#
Raw Permalink Normal View History

2026-09-06 12:33:38 +07:00
using System.Text;
using AutoMapper;
using MediatR;
2026-08-19 23:25:08 +07:00
using mws.backend.dotnet.application.Common;
2026-09-06 12:33:38 +07:00
using mws.backend.dotnet.domain.Documents;
2026-08-19 23:25:08 +07:00
namespace mws.backend.dotnet.application.Documents;
public record GetDocumentQuery(Guid UserId, Guid DocumentId) : IRequest<DocumentDto>;
2026-09-06 12:33:38 +07:00
public class GetDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentContentStore contentStore)
: IRequestHandler<GetDocumentQuery, DocumentDto>
{
public async Task<DocumentDto> Handle(GetDocumentQuery query, CancellationToken ct)
{
var doc = await DocumentAccess.GetDocumentForUserAsync(uow, query.UserId, query.DocumentId, ct);
2026-09-06 12:33:38 +07:00
var dto = mapper.Map<DocumentDto>(doc);
2026-09-15 22:23:54 +07:00
if (doc.Type == DocumentType.Document && !string.IsNullOrEmpty(doc.StorageKey))
2026-09-06 12:33:38 +07:00
{
2026-09-15 22:23:54 +07:00
await using var stream = await contentStore.DownloadAsync(doc.StorageKey, ct);
2026-09-06 12:33:38 +07:00
if (stream != Stream.Null)
{
using var reader = new StreamReader(stream, Encoding.UTF8);
var content = await reader.ReadToEndAsync(ct);
dto.Content = string.IsNullOrEmpty(content) ? null : content;
}
else
{
dto.Content = null;
}
}
return dto;
}
}