Developer 02 39ff4b8867 Refactor envelope creation logic and SQL execution
- Changed `_sqlExecutor` type in `CreateEnvelopeCommandHandler` to non-generic `ISQLExecutor`.
- Updated `Handle` method to use `CreateEnvelopeAsync` for simplified parameter handling.
- Restructured `CreateEnvelopeSQL` to implement `ISQL<Envelope>` with a raw SQL command for creating envelopes.
- Added `SetDappeTypeMap<TModel>` method in `DependencyExtensions` for Dapper type mappings.
- Improved overall code structure for better separation of concerns and maintainability.
2025-05-05 13:53:31 +02:00

45 lines
1.4 KiB
C#

using AutoMapper;
using Dapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using EnvelopeGenerator.Application.SQL;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
namespace EnvelopeGenerator.Application.Envelopes.Commands;
/// <summary>
///
/// </summary>
public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeCommand, CreateEnvelopeResponse?>
{
private readonly ISQLExecutor _sqlExecutor;
private readonly IMapper _mapper;
/// <summary>
///
/// </summary>
/// <param name="sqlExecutor"></param>
/// <param name="mapper"></param>
public CreateEnvelopeCommandHandler(ISQLExecutor<Envelope> sqlExecutor, IMapper mapper)
{
_sqlExecutor = sqlExecutor;
_mapper = mapper;
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<CreateEnvelopeResponse?> 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<Envelope>(userId, request.Title, request.Message, request.TFAEnabled, cancellationToken);
return _mapper.Map<CreateEnvelopeResponse>(envelope);
}
}