refactor(DocumentController): migrate from legacy service to MediatR pattern
- Replace obsolete DocumentController implementation using EnvelopeOldService and direct dependencies - Migrate to MediatR-based query handling via ReadEnvelopeReceiverAsync - Remove obsolete constructors and actions marked for deprecation - Add proper error handling with NotFoundException for missing envelope or document data - Improve logging with structured JSON output for diagnostics - Update base class from BaseController to ControllerBase and add ApiController attribute - Rename action to GetDocument and apply explicit HttpGet routing - Use IMediator for clean separation of concerns and alignment with CQRS pattern
This commit is contained in:
parent
f6e34c6d91
commit
e623680c3f
@ -1,35 +1,50 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using EnvelopeGenerator.CommonServices;
|
||||
using DigitalData.Core.Exceptions;
|
||||
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
|
||||
using EnvelopeGenerator.Web.Services;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Controllers;
|
||||
|
||||
[Authorize(Roles = Domain.Constants.ReceiverRole.FullyAuth)]
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class DocumentController : BaseController
|
||||
public class DocumentController : ControllerBase
|
||||
{
|
||||
private readonly EnvelopeOldService envelopeService;
|
||||
private readonly ActionService? actionService;
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
private readonly ILogger<DocumentController> _logger;
|
||||
|
||||
[Obsolete("Use MediatR")]
|
||||
public DocumentController(DatabaseService database, EnvelopeOldService envelope, ILogger<DocumentController> logger) : base(database, logger)
|
||||
public DocumentController(IMediator mediator, ILogger<DocumentController> logger)
|
||||
{
|
||||
envelopeService = envelope;
|
||||
actionService = database.Services?.actionService;
|
||||
_mediator = mediator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[Obsolete("Use MediatR")]
|
||||
[NonAction]
|
||||
public async Task<IActionResult> Get([FromRoute] string envelopeKey, [FromQuery] int index)
|
||||
[HttpGet("{envelopeKey}")]
|
||||
public async Task<IActionResult> GetDocument([FromRoute] string envelopeKey, CancellationToken cancel)
|
||||
{
|
||||
// Load document info
|
||||
var document = await envelopeService.GetDocument(index, envelopeKey);
|
||||
var envRcv = await _mediator.ReadEnvelopeReceiverAsync(envelopeKey, cancel) ?? throw new NotFoundException("Envelope Receiver is not found.");
|
||||
|
||||
// Load the document from disk
|
||||
var bytes = await envelopeService.GetDocumentContents(document);
|
||||
var byteData = envRcv.Envelope?.Documents?.FirstOrDefault()?.ByteData;
|
||||
|
||||
// Return the document as bytes
|
||||
return File(bytes, "application/octet-stream");
|
||||
if(byteData is null || byteData.Length == 0)
|
||||
{
|
||||
_logger.LogError("Document byte data is null or empty for envelope-receiver entity:\n{envelopeKey}.",
|
||||
JsonConvert.SerializeObject(envRcv, JsonFormattingForDiagnostics));
|
||||
throw new NotFoundException("Document is empty.");
|
||||
}
|
||||
|
||||
return File(byteData, "application/octet-stream");
|
||||
}
|
||||
|
||||
private static readonly JsonSerializerSettings JsonFormattingForDiagnostics = new()
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
Formatting = Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user