- 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.
45 lines
1.4 KiB
C#
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);
|
|
}
|
|
}
|