Update signature handling and database connection management

- Changed `Signature` properties from `int` to `double` for precise positioning.
- Added necessary using directives in `EnvelopeReceiverController.cs` for database operations.
- Introduced a private `_cnnStr` field in `EnvelopeReceiverController` to store the connection string.
- Updated the constructor to accept `IOptions<ConnectionString>` for dependency injection of the connection string.
- Added a SQL command block to handle document element additions using a stored procedure.
- Configured the `ConnectionString` class in `Program.cs` to bind the connection string from app settings.
- Created a new `ConnectionString` class to manage the connection string value.
This commit is contained in:
Developer 02
2025-05-07 02:13:26 +02:00
parent f2a09ea10e
commit 486b717801
4 changed files with 62 additions and 2 deletions

View File

@@ -14,7 +14,7 @@ namespace EnvelopeGenerator.Application.EnvelopeReceivers.Commands.Create;
/// <param name="X">X-Position</param> /// <param name="X">X-Position</param>
/// <param name="Y">Y-Position</param> /// <param name="Y">Y-Position</param>
/// <param name="Page">Seite, auf der sie sich befindet</param> /// <param name="Page">Seite, auf der sie sich befindet</param>
public record Signature([Required] int X, [Required] int Y, [Required] int Page); public record Signature([Required] double X, [Required] double Y, [Required] int Page);
/// <summary> /// <summary>
/// DTO für Empfänger, die erstellt oder abgerufen werden sollen. /// DTO für Empfänger, die erstellt oder abgerufen werden sollen.

View File

@@ -7,9 +7,13 @@ using EnvelopeGenerator.Application.EnvelopeReceivers.Commands.Create;
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries.Read; using EnvelopeGenerator.Application.EnvelopeReceivers.Queries.Read;
using EnvelopeGenerator.Application.Envelopes.Queries.ReceiverName; using EnvelopeGenerator.Application.Envelopes.Queries.ReceiverName;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.GeneratorAPI.Models;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using System.Data;
using System.Threading.Channels; using System.Threading.Channels;
namespace EnvelopeGenerator.GeneratorAPI.Controllers; namespace EnvelopeGenerator.GeneratorAPI.Controllers;
@@ -39,6 +43,8 @@ public class EnvelopeReceiverController : ControllerBase
private readonly IDocumentExecutor _documentExecutor; private readonly IDocumentExecutor _documentExecutor;
private readonly string _cnnStr;
/// <summary> /// <summary>
/// Konstruktor für den EnvelopeReceiverController. /// Konstruktor für den EnvelopeReceiverController.
/// </summary> /// </summary>
@@ -48,7 +54,7 @@ public class EnvelopeReceiverController : ControllerBase
/// <param name="mapper"></param> /// <param name="mapper"></param>
/// <param name="envelopeExecutor"></param> /// <param name="envelopeExecutor"></param>
/// <param name="erExecutor"></param> /// <param name="erExecutor"></param>
public EnvelopeReceiverController(ILogger<EnvelopeReceiverController> logger, IEnvelopeReceiverService envelopeReceiverService, IMediator mediator, IMapper mapper, IEnvelopeExecutor envelopeExecutor, IEnvelopeReceiverExecutor erExecutor, IDocumentExecutor documentExecutor) public EnvelopeReceiverController(ILogger<EnvelopeReceiverController> logger, IEnvelopeReceiverService envelopeReceiverService, IMediator mediator, IMapper mapper, IEnvelopeExecutor envelopeExecutor, IEnvelopeReceiverExecutor erExecutor, IDocumentExecutor documentExecutor, IOptions<ConnectionString> csOpt)
{ {
_logger = logger; _logger = logger;
_erService = envelopeReceiverService; _erService = envelopeReceiverService;
@@ -57,6 +63,7 @@ public class EnvelopeReceiverController : ControllerBase
_envelopeExecutor = envelopeExecutor; _envelopeExecutor = envelopeExecutor;
_erExecutor = erExecutor; _erExecutor = erExecutor;
_documentExecutor = documentExecutor; _documentExecutor = documentExecutor;
_cnnStr = csOpt.Value.Value;
} }
/// <summary> /// <summary>
@@ -228,7 +235,45 @@ public class EnvelopeReceiverController : ControllerBase
if(document is null) if(document is null)
return StatusCode(StatusCodes.Status500InternalServerError, "Document creation is failed."); return StatusCode(StatusCodes.Status500InternalServerError, "Document creation is failed.");
#endregion
#region Add document element
string sql = @"
DECLARE @OUT_SUCCESS bit;
EXEC [dbo].[PRSIG_API_ADD_DOC_RECEIVER_ELEM]
@DOC_ID = @DOC_ID,
@RECEIVER_ID = @RECEIVER_ID,
@POSITION_X = @POSITION_X,
@POSITION_Y = @POSITION_Y,
@PAGE = @PAGE,
@OUT_SUCCESS = @OUT_SUCCESS OUTPUT;
SELECT @OUT_SUCCESS as [@OUT_SUCCESS];";
foreach(var rcv in res.SentRecipients)
foreach(var sign in request.Receivers.Where(r => r.EmailAddress == rcv.EmailAddress).FirstOrDefault()?.Signatures ?? Array.Empty<Signature>())
{
using (SqlConnection conn = new(_cnnStr))
{
conn.Open();
using SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@DOC_ID", document.Id);
cmd.Parameters.AddWithValue("@RECEIVER_ID", rcv.Id);
cmd.Parameters.AddWithValue("@POSITION_X", sign.X.ToString());
cmd.Parameters.AddWithValue("@POSITION_Y", sign.Y.ToString());
cmd.Parameters.AddWithValue("@PAGE", sign.Page.ToString());
using SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
bool outSuccess = reader.GetBoolean(0);
}
}
}
#endregion #endregion
return Ok(res); return Ok(res);

View File

@@ -0,0 +1,12 @@
namespace EnvelopeGenerator.GeneratorAPI.Models;
/// <summary>
///
/// </summary>
public class ConnectionString
{
/// <summary>
///
/// </summary>
public string Value { get; set; } = string.Empty;
}

View File

@@ -90,6 +90,9 @@ builder.Services.AddSwaggerGen(options =>
builder.Services.AddOpenApi(); builder.Services.AddOpenApi();
// DbContext // DbContext
var connStr = config.GetConnectionString("Default") ?? throw new InvalidOperationException("There is no default connection string in appsettings.json."); var connStr = config.GetConnectionString("Default") ?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
builder.Services.Configure<ConnectionString>(cs => cs.Value = connStr);
builder.Services.AddDbContext<EGDbContext>(options => options.UseSqlServer(connStr)); builder.Services.AddDbContext<EGDbContext>(options => options.UseSqlServer(connStr));
builder.Services.AddAuthHubClient(config.GetSection("AuthClientParams")); builder.Services.AddAuthHubClient(config.GetSection("AuthClientParams"));