Refactor envelope creation logic and add IEnvelopeExecutor

Updated CreateEnvelopeCommandHandler to use IEnvelopeExecutor for managing envelope creation. Introduced CreateParmas method for parameter handling in CreateEnvelopeSQL. Corrected Dapper type mapping method name and added service registration for IEnvelopeExecutor in DependencyExtensions. Refactored SQLExecutor to enhance encapsulation. Created EnvelopeExecutor class implementing IEnvelopeExecutor for dedicated envelope operations.
This commit is contained in:
Developer 02
2025-05-05 16:11:29 +02:00
parent 39ff4b8867
commit b93ba6be17
6 changed files with 111 additions and 24 deletions

View File

@@ -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<EnvelopeExecutor> logger, IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions)
{
_userRepository = userRepository;
}
public async Task<Envelope?> CreateEnvelopeAsync(DynamicParameters parameters, bool addUser = true, CancellationToken cancellation = default)
{
using var connection = new SqlConnection(Params.ConnectionString);
var sql = Provider.GetRequiredService<CreateEnvelopeSQL>();
await connection.OpenAsync(cancellation);
var envelopes = await connection.QueryAsync<Envelope>(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;
}
}