using AutoMapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.Data.SqlClient;
namespace EnvelopeGenerator.Application.Envelopes.Commands;
///
///
///
public class CreateEnvelopeCommandHandler : IRequestHandler
{
private readonly ISQLExecutor _sqlExecutor;
private readonly IMapper _mapper;
///
///
///
///
///
public CreateEnvelopeCommandHandler(ISQLExecutor sqlExecutor, IMapper mapper)
{
_sqlExecutor = sqlExecutor;
_mapper = mapper;
}
///
///
///
///
///
///
public async Task Handle(CreateEnvelopeCommand request, CancellationToken cancellationToken)
{
object[] parameters = new object[]
{
new SqlParameter("@UserId", request.UserId),
new SqlParameter("@Title", request.Title),
new SqlParameter("@TfaEnabled", request.TFAEnabled ? 1 : 0),
new SqlParameter("@Message", request.Message)
};
var envelope = await _sqlExecutor.Execute(cancellationToken, parameters).FirstOrDefaultAsync();
return _mapper.Map(envelope);
}
}