Refactor DocumentController for clarity and updates

Updated the DocumentController to use new namespace for constants and added a directive for domain entities. Marked the constructor as obsolete for future MediatR implementation. Refactored Get and Open methods to enhance readability by simplifying error handling. Maintained authorization attributes in their original positions.
This commit is contained in:
tekh 2025-06-27 10:26:56 +02:00
parent 2a4255e5a3
commit 08601adc49

View File

@ -3,76 +3,77 @@ using EnvelopeGenerator.CommonServices;
using EnvelopeGenerator.Web.Services; using EnvelopeGenerator.Web.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using EnvelopeGenerator.Extensions; using EnvelopeGenerator.Extensions;
using static EnvelopeGenerator.CommonServices.Constants;
using EnvelopeGenerator.Application.Contracts.Services; using EnvelopeGenerator.Application.Contracts.Services;
using static EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Web.Controllers namespace EnvelopeGenerator.Web.Controllers;
[Authorize(Roles = ReceiverRole.FullyAuth)]
[Route("api/[controller]")]
public class DocumentController : BaseController
{ {
[Authorize(Roles = ReceiverRole.FullyAuth)] private readonly EnvelopeOldService envelopeService;
[Route("api/[controller]")] private readonly ActionService? actionService;
public class DocumentController : BaseController [Obsolete("Use MediatR")]
private readonly IEnvelopeDocumentService _envDocService;
[Obsolete("Use MediatR")]
public DocumentController(DatabaseService database, EnvelopeOldService envelope, IEnvelopeDocumentService envDocService, ILogger<DocumentController> logger) : base(database, logger)
{ {
private readonly EnvelopeOldService envelopeService; envelopeService = envelope;
private readonly ActionService? actionService; actionService = database.Services?.actionService;
private readonly IEnvelopeDocumentService _envDocService; _envDocService = envDocService;
}
public DocumentController(DatabaseService database, EnvelopeOldService envelope, IEnvelopeDocumentService envDocService, ILogger<DocumentController> logger) : base(database, logger) [NonAction]
public async Task<IActionResult> Get([FromRoute] string envelopeKey, [FromQuery] int index)
{
try
{ {
envelopeService = envelope; // Validate Envelope Key and load envelope
actionService = database.Services?.actionService; envelopeService.EnsureValidEnvelopeKey(envelopeKey);
_envDocService = envDocService; EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
}
[NonAction] // Load document info
public async Task<IActionResult> Get([FromRoute] string envelopeKey, [FromQuery] int index) var document = await envelopeService.GetDocument(index, envelopeKey);
// Load the document from disk
var bytes = await envelopeService.GetDocumentContents(document);
// Return the document as bytes
return File(bytes, "application/octet-stream");
}
catch(Exception ex)
{ {
try _logger.LogError(ex, "{Message}", ex.Message);
{ return StatusCode(StatusCodes.Status500InternalServerError);
// Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
// Load document info
var document = await envelopeService.GetDocument(index, envelopeKey);
// Load the document from disk
var bytes = await envelopeService.GetDocumentContents(document);
// Return the document as bytes
return File(bytes, "application/octet-stream");
}
catch(Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
} }
}
[Authorize(Roles = ReceiverRole.FullyAuth)] [Authorize(Roles = ReceiverRole.FullyAuth)]
[HttpPost("{envelopeKey}")] [HttpPost("{envelopeKey}")]
public async Task<IActionResult> Open(string envelopeKey) public async Task<IActionResult> Open(string envelopeKey)
{
try
{ {
try var authSignature = this.GetAuthReceiverSignature();
{
var authSignature = this.GetAuthReceiverSignature();
if (authSignature != envelopeKey.GetReceiverSignature()) if (authSignature != envelopeKey.GetReceiverSignature())
return Forbid(); return Forbid();
// Validate Envelope Key and load envelope // Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey); envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
actionService?.OpenEnvelope(response.Envelope, response.Receiver); actionService?.OpenEnvelope(response.Envelope, response.Receiver);
return Ok(new object()); return Ok(new object());
} }
catch(Exception ex) catch(Exception ex)
{ {
_logger.LogError(ex, "{Message}", ex.Message); _logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError); return StatusCode(StatusCodes.Status500InternalServerError);
}
} }
} }
} }