using EnvelopeGenerator.API.Extensions; using EnvelopeGenerator.Application.Documents.Queries; using EnvelopeGenerator.Domain.Constants; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace EnvelopeGenerator.API.Controllers; /// /// Provides access to envelope documents for authenticated receivers. /// /// /// Initializes a new instance of the class. /// [Authorize] [ApiController] [Route("api/[controller]")] public class DocumentController(IMediator mediator, ILogger logger) : ControllerBase { /// /// Returns the document bytes for the specified envelope receiver key. /// /// Encoded envelope key. /// Cancellation token. [HttpGet] [Authorize(Roles = Role.Sender)] public async Task GetDocument(ReadDocumentQuery query, CancellationToken cancel) { var doc = await mediator.Send(query, cancel); return doc.ByteData is byte[] docByte ? File(docByte, "application/octet-stream") : NotFound("Document is empty."); } /// /// Returns the document bytes for the receiver. /// /// Cancellation token. [HttpGet] [Authorize(Roles = Role.Receiver.FullyAuth)] public async Task GetDocument(CancellationToken cancel) { var envelopeId = User.GetEnvelopeIdOfReceiver(); var doc = await mediator.Send(new ReadDocumentQuery() { EnvelopeId = envelopeId }, cancel); return doc.ByteData is byte[] docByte ? File(docByte, "application/octet-stream") : NotFound("Document is empty."); } }