40 lines
1.8 KiB
C#

using Dapper;
using DigitalData.UserManager.Application.Contracts.Repositories;
using EnvelopeGenerator.Application.Common.SQL;
using EnvelopeGenerator.Application.Interfaces.Repositories;
using EnvelopeGenerator.Application.Interfaces.SQLExecutor;
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<SQLExecutorParams> sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions)
{
_userRepository = userRepository;
}
[Obsolete("Use IRepository")]
public async Task<Envelope> CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default)
{
using var connection = new SqlConnection(Params.ConnectionString);
var sql = Provider.GetRequiredService<EnvelopeCreateReadSQL>();
var formattedSql = string.Format(sql.Raw, userId.ToSqlParam(), title.ToSqlParam(), tfaEnabled.ToSqlParam(), message.ToSqlParam());
await connection.OpenAsync(cancellation);
var envelopes = await connection.QueryAsync<Envelope>(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;
}
}