Refactor envelope creation methods and parameter handling

- Updated `Extension.cs` to remove old methods and introduce a new parameter creation method.
- Modified `IEnvelopeExecutor.cs` to change `CreateEnvelopeAsync` signature for clarity.
- Added a consistent `CreateParmas` method in `EnvelopeCreateReadSQL.cs`.
- Changed service registration in `DependencyExtensions.cs` from singleton to scoped.
- Revised `CreateEnvelopeAsync` in `EnvelopeExecutor.cs` to utilize the new parameter structure and removed user addition logic.

These changes enhance code clarity, maintainability, and consistency in parameter handling.
This commit is contained in:
Developer 02 2025-05-06 11:24:14 +02:00
parent 3cc8e2b5db
commit b5b9155bc0
5 changed files with 34 additions and 98 deletions

View File

@ -1,80 +0,0 @@
using Dapper;
using EnvelopeGenerator.Application.SQL;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Application.Contracts.SQLExecutor;
/// <summary>
///
/// </summary>
public static class Extension
{
private static DynamicParameters CreateParmas(int userId, string title = "", string message = "", bool tfaEnabled = false)
{
var parameters = new DynamicParameters();
parameters.Add("@UserId", userId);
parameters.Add("@Title", title);
parameters.Add("@TfaEnabled", tfaEnabled ? 1 : 0);
parameters.Add("@Message", message);
return parameters;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="executor"></param>
/// <param name="userId"></param>
/// <param name="title"></param>
/// <param name="message"></param>
/// <param name="tfaEnabled"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public static async Task<TEntity?> CreateEnvelopeAsync<TEntity>(this ISQLExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
where TEntity : class
{
var parameters = CreateParmas(userId, title, message, tfaEnabled);
var envelopes = await executor.Execute<TEntity, EnvelopeCreateReadSQL>(parameters, cancellation);
return envelopes.FirstOrDefault();
}
/// <summary>
///
/// </summary>
/// <param name="executor"></param>
/// <param name="userId"></param>
/// <param name="title"></param>
/// <param name="message"></param>
/// <param name="tfaEnabled"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public static async Task<Envelope?> CreateEnvelopeAsync(this IEnvelopeExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
{
var parameters = CreateParmas(userId, title, message, tfaEnabled);
return await executor.CreateEnvelopeAsync(parameters, cancellation: cancellation);
}
/// <summary>
///
/// </summary>
/// <param name="executor"></param>
/// <param name="envelope_uuid"></param>
/// <param name="emailAdress"></param>
/// <param name="salutation"></param>
/// <param name="phone"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public static async Task<EnvelopeReceiver?> AddEnvelopeReceiver(this ISQLExecutor executor, string envelope_uuid, string emailAdress, string salutation, string? phone = null, CancellationToken cancellation = default)
{
var parameters = new DynamicParameters();
parameters.Add("@ENV_UID", envelope_uuid);
parameters.Add("@EMAIL_ADRESS", emailAdress);
parameters.Add("@SALUTATION", salutation);
parameters.Add("@PHONE", phone);
var envelopeReceivers = await executor.Execute<EnvelopeReceiver, EnvelopeReceiverAddReadSQL>(parameters, cancellation);
return envelopeReceivers.FirstOrDefault();
}
}

View File

@ -11,9 +11,11 @@ public interface IEnvelopeExecutor : ISQLExecutor
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="parameters"></param> /// <param name="userId"></param>
/// <param name="addUser"></param> /// <param name="title"></param>
/// <param name="message"></param>
/// <param name="tfaEnabled"></param>
/// <param name="cancellation"></param> /// <param name="cancellation"></param>
/// <returns></returns> /// <returns></returns>
Task<Envelope?> CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default); Task<Envelope?> CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default);
} }

View File

@ -1,4 +1,5 @@
using EnvelopeGenerator.Application.Contracts.SQLExecutor; using Dapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Application.SQL; namespace EnvelopeGenerator.Application.SQL;
@ -26,4 +27,22 @@ public class EnvelopeCreateReadSQL : ISQL<Envelope>
FROM [dbo].[TBSIG_ENVELOPE] FROM [dbo].[TBSIG_ENVELOPE]
WHERE [ENVELOPE_UUID] = @OUT_UID; WHERE [ENVELOPE_UUID] = @OUT_UID;
"; ";
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="title"></param>
/// <param name="message"></param>
/// <param name="tfaEnabled"></param>
/// <returns></returns>
public static DynamicParameters CreateParmas(int userId, string title = "", string message = "", bool tfaEnabled = false)
{
var parameters = new DynamicParameters();
parameters.Add("@UserId", userId);
parameters.Add("@Title", title);
parameters.Add("@TfaEnabled", tfaEnabled ? 1 : 0);
parameters.Add("@Message", message);
return parameters;
}
} }

View File

@ -82,8 +82,8 @@ public static class DIExtensions
SetDapperTypeMap<DocumentReceiverElement>(); SetDapperTypeMap<DocumentReceiverElement>();
SetDapperTypeMap<DocumentStatus>(); SetDapperTypeMap<DocumentStatus>();
services.AddSingleton<IEnvelopeExecutor, EnvelopeExecutor>(); services.AddScoped<IEnvelopeExecutor, EnvelopeExecutor>();
services.AddSingleton<IEnvelopeReceiverExecutor, EnvelopeReceiverExecutor>(); services.AddScoped<IEnvelopeReceiverExecutor, EnvelopeReceiverExecutor>();
if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null) if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null)
services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions); services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions);

View File

@ -1,5 +1,6 @@
using Dapper; using Dapper;
using DigitalData.UserManager.Application.Contracts.Repositories; using DigitalData.UserManager.Application.Contracts.Repositories;
using EnvelopeGenerator.Application.Contracts.Repositories;
using EnvelopeGenerator.Application.Contracts.SQLExecutor; using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Application.SQL; using EnvelopeGenerator.Application.SQL;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
@ -12,31 +13,25 @@ namespace EnvelopeGenerator.Infrastructure.Executor;
public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor
{ {
private readonly IUserRepository _userRepository; private readonly IEnvelopeRepository _envelopeRepository;
public EnvelopeExecutor(IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions) public EnvelopeExecutor(IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions, IEnvelopeRepository envelopeRepository) : base(provider, sqlExecutorParamsOptions)
{ {
_userRepository = userRepository; _envelopeRepository = envelopeRepository;
} }
public async Task<Envelope?> CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default) public async Task<Envelope?> CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
{ {
using var connection = new SqlConnection(Params.ConnectionString); using var connection = new SqlConnection(Params.ConnectionString);
var sql = Provider.GetRequiredService<EnvelopeCreateReadSQL>(); var sql = Provider.GetRequiredService<EnvelopeCreateReadSQL>();
await connection.OpenAsync(cancellation); await connection.OpenAsync(cancellation);
var parameters = EnvelopeCreateReadSQL.CreateParmas(userId, title, message, tfaEnabled);
var envelopes = await connection.QueryAsync<Envelope>(sql.Raw, parameters); var envelopes = await connection.QueryAsync<Envelope>(sql.Raw, parameters);
var envelope = envelopes.FirstOrDefault(); var envelope = envelopes.FirstOrDefault();
if (envelope is null) if (envelope is null)
return null; return null;
// Add User return await _envelopeRepository.ReadByUuidAsync(envelope.Uuid, withAll: true);
if (addUser)
{
var user = await _userRepository.ReadByIdAsync(envelope.UserId);
envelope.User = user;
}
return envelope;
} }
} }