using AutoMapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using MediatR;
namespace EnvelopeGenerator.Application.Envelopes.Commands;
///
///
///
public class CreateEnvelopeCommandHandler : IRequestHandler
{
private readonly ISQLExecutor _executor;
private readonly IMapper _mapper;
///
///
///
///
///
public CreateEnvelopeCommandHandler(ISQLExecutor executor, IMapper mapper)
{
_executor = executor;
_mapper = mapper;
}
///
///
///
///
///
///
public async Task Handle(CreateEnvelopeCommand request, CancellationToken cancellationToken)
{
int userId = request.UserId ?? throw new InvalidOperationException("UserId cannot be null when creating an envelope.");
var envelope = await _executor.CreateEnvelopeAsync(userId, request.Title, request.Message, request.TFAEnabled, cancellationToken);
return _mapper.Map(envelope);
}
}