diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs index 2972ce0d..a765a090 100644 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/Extension.cs @@ -9,6 +9,36 @@ 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; + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static async Task CreateEnvelopeAsync(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(parameters, cancellation); + return envelopes.FirstOrDefault(); + } + /// /// /// @@ -19,14 +49,10 @@ public static class Extension /// /// /// - public static async Task CreateEnvelopeAsync(this ISQLExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) + public static async Task CreateEnvelopeAsync(this IEnvelopeExecutor executor, int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) { - 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(); + 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 new file mode 100644 index 00000000..f197cadf --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs @@ -0,0 +1,19 @@ +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 35bfe61f..5c8cb3e9 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 ISQLExecutor _executor; + private readonly IEnvelopeExecutor _envelopeExecutor; private readonly IMapper _mapper; /// /// /// - /// + /// /// - public CreateEnvelopeCommandHandler(ISQLExecutor executor, IMapper mapper) + public CreateEnvelopeCommandHandler(IEnvelopeExecutor envelopeExecutor, IMapper mapper) { - _executor = executor; + _envelopeExecutor = envelopeExecutor; _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 deleted file mode 100644 index 3f9b2db8..00000000 --- a/EnvelopeGenerator.Application/SQL/EnvelopeReceiverCreateReadSQL.cs +++ /dev/null @@ -1,33 +0,0 @@ -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 c1138538..d9d3bd92 100644 --- a/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs +++ b/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs @@ -82,6 +82,8 @@ 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 new file mode 100644 index 00000000..dca802ba --- /dev/null +++ b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs @@ -0,0 +1,43 @@ +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