#if NET using Dapper; using EnvelopeGenerator.Application.Common.Interfaces.SQLExecutor; using EnvelopeGenerator.Application.Common.SQL; using EnvelopeGenerator.Domain.Entities; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.Infrastructure.Executor; public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor { private readonly EGDbContext _dbContext; public EnvelopeExecutor(IServiceProvider provider, IOptions sqlExecutorParamsOptions, EGDbContext dbContext) : base(provider, sqlExecutorParamsOptions) { _dbContext = dbContext; } [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 _dbContext.Users .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == envelope.UserId, cancellation); return envelope; } } #endif