From 4cabaf31917c8860411349c0e5fc1c62e7c5c384 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 6 May 2025 01:30:59 +0200 Subject: [PATCH] Refactor envelope handling to use ISQLExecutor This commit removes the IEnvelopeExecutor interface and its implementation, replacing it with ISQLExecutor. The CreateEnvelopeAsync method in Extension.cs now directly creates DynamicParameters. The CreateEnvelopeCommandHandler has been updated to utilize ISQLExecutor. Additionally, the EnvelopeExecutor class has been removed, and a new SQL command class, EnvelopeReceiverCreateReadSQL, has been added for managing envelope receiver SQL operations. --- .../Contracts/SQLExecutor/Extension.cs | 40 +++-------------- .../SQLExecutor/IEnvelopeExecutor.cs | 19 -------- .../Commands/CreateEnvelopeCommandHandler.cs | 10 ++--- .../SQL/EnvelopeReceiverCreateReadSQL.cs | 33 ++++++++++++++ .../DependencyExtensions.cs | 2 - .../Executor/EnvelopeExecutor.cs | 43 ------------------- 6 files changed, 45 insertions(+), 102 deletions(-) delete mode 100644 EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs create mode 100644 EnvelopeGenerator.Application/SQL/EnvelopeReceiverCreateReadSQL.cs delete mode 100644 EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs index a765a090..2972ce0d 100644 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs @@ -9,20 +9,9 @@ namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; /// 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; - } - /// /// /// - /// /// /// /// @@ -30,29 +19,14 @@ public static class Extension /// /// /// - public static async Task CreateEnvelopeAsync(this ISQLExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) - where TEntity : class + public static async Task CreateEnvelopeAsync(this ISQLExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) { - var parameters = CreateParmas(userId, title, message, tfaEnabled); - - var envelopes = await executor.Execute(parameters, cancellation); + var parameters = new DynamicParameters(); + parameters.Add("@UserId", userId); + parameters.Add("@Title", title); + parameters.Add("@TfaEnabled", tfaEnabled ? 1 : 0); + parameters.Add("@Message", message); + var envelopes = await executor.Execute(parameters, cancellation); return envelopes.FirstOrDefault(); } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static async Task 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); - } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs deleted file mode 100644 index f197cadf..00000000 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Dapper; -using EnvelopeGenerator.Domain.Entities; - -namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; - -/// -/// -/// -public interface IEnvelopeExecutor : ISQLExecutor -{ - /// - /// - /// - /// - /// - /// - /// - Task CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default); -} diff --git a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs index 5c8cb3e9..35bfe61f 100644 --- a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs +++ b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs @@ -9,18 +9,18 @@ namespace EnvelopeGenerator.Application.Envelopes.Commands; /// public class CreateEnvelopeCommandHandler : IRequestHandler { - private readonly IEnvelopeExecutor _envelopeExecutor; + private readonly ISQLExecutor _executor; private readonly IMapper _mapper; /// /// /// - /// + /// /// - public CreateEnvelopeCommandHandler(IEnvelopeExecutor envelopeExecutor, IMapper mapper) + public CreateEnvelopeCommandHandler(ISQLExecutor executor, IMapper mapper) { - _envelopeExecutor = envelopeExecutor; + _executor = executor; _mapper = mapper; } @@ -34,7 +34,7 @@ public class CreateEnvelopeCommandHandler : IRequestHandler(envelope); } diff --git a/EnvelopeGenerator.Application/SQL/EnvelopeReceiverCreateReadSQL.cs b/EnvelopeGenerator.Application/SQL/EnvelopeReceiverCreateReadSQL.cs new file mode 100644 index 00000000..3f9b2db8 --- /dev/null +++ b/EnvelopeGenerator.Application/SQL/EnvelopeReceiverCreateReadSQL.cs @@ -0,0 +1,33 @@ +using EnvelopeGenerator.Application.Contracts.SQLExecutor; +using EnvelopeGenerator.Domain.Entities; + +namespace EnvelopeGenerator.Application.SQL; + +/// +/// +/// +public class EnvelopeReceiverCreateReadSQL : ISQL +{ + /// + /// + /// + public string Raw => @" + USE [DD_ECM] + GO + + DECLARE @OUT_RECEIVER_ID int + + DECLARE @ENV_UID varchar(36) = @ENV_UID + + EXEC [dbo].[PRSIG_API_CREATE_RECEIVER] + @ENV_UID = @ENV_UID, + @EMAIL_ADRESS = @EMAIL_ADRESS , + @SALUTATION = @SALUTATION, + @PHONE = @PHONE, + @OUT_RECEIVER_ID = @OUT_RECEIVER_ID OUTPUT + + SELECT TOP(1) * + FROM TBSIG_ENVELOPE_RECEIVER + WHERE [GUID] = @OUT_RECEIVER_ID; + "; +} diff --git a/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs b/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs index d9d3bd92..c1138538 100644 --- a/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs +++ b/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs @@ -82,8 +82,6 @@ public static class DIExtensions SetDapperTypeMap(); SetDapperTypeMap(); - services.AddScoped(); - if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null) services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions); diff --git a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs deleted file mode 100644 index dca802ba..00000000 --- a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Dapper; -using DigitalData.UserManager.Application.Contracts.Repositories; -using DigitalData.UserManager.Infrastructure.Repositories; -using EnvelopeGenerator.Application.Contracts.SQLExecutor; -using EnvelopeGenerator.Application.SQL; -using EnvelopeGenerator.Domain.Entities; -using Microsoft.Data.SqlClient; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace EnvelopeGenerator.Infrastructure.Executor; - -public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor -{ - private readonly IUserRepository _userRepository; - - public EnvelopeExecutor(ILogger logger, IServiceProvider provider, IOptions sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions) - { - _userRepository = userRepository; - } - - public async Task CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default) - { - using var connection = new SqlConnection(Params.ConnectionString); - var sql = Provider.GetRequiredService(); - await connection.OpenAsync(cancellation); - var envelopes = await connection.QueryAsync(sql.Raw, parameters); - var envelope = envelopes.FirstOrDefault(); - - if (envelope is null) - return null; - - // Add User - if (addUser) - { - var user = await _userRepository.ReadByIdAsync(envelope.UserId); - envelope.User = user; - } - - return envelope; - } -} \ No newline at end of file