using Dapper; using DigitalData.UserManager.Application.Contracts.Repositories; using EnvelopeGenerator.Application.Interfaces.Repositories; using EnvelopeGenerator.Application.Interfaces.SQLExecutor; using EnvelopeGenerator.Application.SQL; using EnvelopeGenerator.Domain.Entities; using Microsoft.Data.SqlClient; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.Infrastructure.Executor; public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor { [Obsolete("Use IRepository")] private readonly IUserRepository _userRepository; [Obsolete("Use IRepository")] public EnvelopeExecutor(IServiceProvider provider, IOptions sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions) { _userRepository = userRepository; } [Obsolete("Use IRepository")] public async Task CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) { using var connection = new SqlConnection(Params.ConnectionString); var sql = Provider.GetRequiredService(); var formattedSql = string.Format(sql.Raw, userId.ToSqlParam(), title.ToSqlParam(), tfaEnabled.ToSqlParam(), message.ToSqlParam()); await connection.OpenAsync(cancellation); var envelopes = await connection.QueryAsync(formattedSql); var envelope = envelopes.FirstOrDefault() ?? throw new InvalidOperationException($"Envelope creation failed. Parameters:" + $"userId={userId}, title='{title}', message='{message}', tfaEnabled={tfaEnabled}."); envelope.User = await _userRepository.ReadByIdAsync(envelope.UserId); return envelope; } }