using AutoMapper;
using Dapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Application.SQL;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
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)
{
int userId = request.UserId ?? throw new InvalidOperationException("UserId cannot be null when creating an envelope.");
var envelope = await _sqlExecutor.CreateEnvelopeAsync(userId, request.Title, request.Message, request.TFAEnabled, cancellationToken);
return _mapper.Map(envelope);
}
}