diff --git a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs index d33d8d3d..7ac1c166 100644 --- a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs +++ b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs @@ -1,9 +1,9 @@ using AutoMapper; using Dapper; using EnvelopeGenerator.Application.Contracts.SQLExecutor; +using EnvelopeGenerator.Application.SQL; using EnvelopeGenerator.Domain.Entities; using MediatR; -using Microsoft.Data.SqlClient; namespace EnvelopeGenerator.Application.Envelopes.Commands; @@ -12,7 +12,7 @@ namespace EnvelopeGenerator.Application.Envelopes.Commands; /// public class CreateEnvelopeCommandHandler : IRequestHandler { - private readonly ISQLExecutor _sqlExecutor; + private readonly ISQLExecutor _sqlExecutor; private readonly IMapper _mapper; @@ -35,14 +35,10 @@ public class CreateEnvelopeCommandHandler : IRequestHandler public async Task Handle(CreateEnvelopeCommand request, CancellationToken cancellationToken) { - var parameters = new DynamicParameters(); - parameters.Add("@UserId", request.UserId); - parameters.Add("@Title", request.Title); - parameters.Add("@TfaEnabled", request.TFAEnabled ? 1 : 0); - parameters.Add("@Message", request.Message); + int userId = request.UserId ?? throw new InvalidOperationException("UserId cannot be null when creating an envelope."); - var envelopes = await _sqlExecutor.Execute(parameters, cancellationToken); + var envelope = await _sqlExecutor.CreateEnvelopeAsync(userId, request.Title, request.Message, request.TFAEnabled, cancellationToken); - return _mapper.Map(envelopes.FirstOrDefault()); + return _mapper.Map(envelope); } } diff --git a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeSQL.cs b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeSQL.cs deleted file mode 100644 index e9b19ca7..00000000 --- a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeSQL.cs +++ /dev/null @@ -1,29 +0,0 @@ -using EnvelopeGenerator.Application.Contracts.SQLExecutor; -using EnvelopeGenerator.Domain.Entities; -using Microsoft.Data.SqlClient; - -namespace EnvelopeGenerator.Application.Envelopes.Commands; -/// -/// -/// -public class CreateEnvelopeSQL : ISQL -{ - /// - /// - /// - public string Raw => @" - USE [DD_ECM]; - DECLARE @OUT_UID varchar(36); - - EXEC [dbo].[PRSIG_API_CREATE_ENVELOPE] - @USER_ID = @UserId, - @TITLE = @Title, - @TFAEnabled = @TfaEnabled, - @MESSAGE = @Message, - @OUT_UID = @OUT_UID OUTPUT; - - SELECT TOP(1) * - FROM [dbo].[TBSIG_ENVELOPE] - WHERE [ENVELOPE_UUID] = @OUT_UID; - "; -} diff --git a/EnvelopeGenerator.Application/SQL/CreateEnvelopeSQL.cs b/EnvelopeGenerator.Application/SQL/CreateEnvelopeSQL.cs new file mode 100644 index 00000000..fb231334 --- /dev/null +++ b/EnvelopeGenerator.Application/SQL/CreateEnvelopeSQL.cs @@ -0,0 +1,60 @@ +using Dapper; +using EnvelopeGenerator.Application.Contracts.SQLExecutor; +using EnvelopeGenerator.Domain.Entities; + +namespace EnvelopeGenerator.Application.SQL; + +/// +/// +/// +public class CreateEnvelopeSQL : ISQL +{ + /// + /// + /// + public string Raw => @" + USE [DD_ECM]; + DECLARE @OUT_UID varchar(36); + + EXEC [dbo].[PRSIG_API_CREATE_ENVELOPE] + @USER_ID = @UserId, + @TITLE = @Title, + @TFAEnabled = @TfaEnabled, + @MESSAGE = @Message, + @OUT_UID = @OUT_UID OUTPUT; + + SELECT TOP(1) * + FROM [dbo].[TBSIG_ENVELOPE] + WHERE [ENVELOPE_UUID] = @OUT_UID; + "; +} + +/// +/// +/// +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 + { + 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(); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs b/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs index 7a2a1ce6..dd8458ed 100644 --- a/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs +++ b/EnvelopeGenerator.Infrastructure/DependencyExtensions.cs @@ -9,6 +9,9 @@ using DigitalData.Core.Infrastructure.AutoMapper; using EnvelopeGenerator.Application.Contracts.SQLExecutor; using Microsoft.Extensions.Configuration; using EnvelopeGenerator.Infrastructure.Executor; +using Dapper; +using System.ComponentModel.DataAnnotations.Schema; +using System.Reflection; namespace EnvelopeGenerator.Infrastructure; @@ -71,6 +74,12 @@ public static class DIExtensions services.AddSQLExecutor(); services.AddSQLExecutor(); + SetDappeTypeMap(); + SetDappeTypeMap(); + SetDappeTypeMap(); + SetDappeTypeMap(); + SetDappeTypeMap(); + if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null) services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions); @@ -91,6 +100,22 @@ public static class DIExtensions return services.AddSingleton(); } + private static void SetDappeTypeMap() + { + Dapper.SqlMapper.SetTypeMap(typeof(TModel), new CustomPropertyTypeMap( + typeof(TModel), + (type, columnName) => + { + return type.GetProperties().FirstOrDefault(prop => + { + var attr = prop.GetCustomAttribute(); + return attr != null && string.Equals(attr.Name, columnName, StringComparison.OrdinalIgnoreCase) + || string.Equals(prop.Name, columnName, StringComparison.OrdinalIgnoreCase); + })!; + } + )); + } + public static IServiceCollection AddSQLExecutor(this IServiceCollection services, IConfiguration? configuration = null, Action? configureOptions = null) where T : class { if (configuration is not null && configureOptions is not null)