feat(envelopes): add support for SQLExecutor or repository when creating envelopes
- Refactored `CreateEnvelopeCommandHandler` to resolve dependencies via `IServiceProvider` - Added `IRepository<Envelope>` to allow repository-based envelope creation - Updated handler logic to choose between `IEnvelopeExecutor` and repository based on `UseSQLExecutor` flag
This commit is contained in:
parent
ee7eb08e75
commit
00077a647a
@ -36,7 +36,10 @@ public record CreateEnvelopeCommand : IRequest<EnvelopeDto?>
|
|||||||
public int UserId { get; set; }
|
public int UserId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// if true, use <seealso cref="IEnvelopeExecutor"/>; otherwise, use <seealso cref="IRepository{Envelope}"/>
|
/// Determines which component is used for envelope processing.
|
||||||
|
/// When <c>true</c>, processing is delegated to <see cref="IEnvelopeExecutor"/>;
|
||||||
|
/// when <c>false</c>, <see cref="IRepository{Envelope}"/> is used instead.
|
||||||
|
/// Note: <see cref="IRepository{Envelope}"/> should only be used in testing scenarios.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
using EnvelopeGenerator.Application.Interfaces.SQLExecutor;
|
using EnvelopeGenerator.Application.Interfaces.SQLExecutor;
|
||||||
using EnvelopeGenerator.Application.Dto;
|
using EnvelopeGenerator.Application.Dto;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.Envelopes.Commands;
|
namespace EnvelopeGenerator.Application.Envelopes.Commands;
|
||||||
|
|
||||||
@ -10,18 +13,22 @@ namespace EnvelopeGenerator.Application.Envelopes.Commands;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeCommand, EnvelopeDto?>
|
public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeCommand, EnvelopeDto?>
|
||||||
{
|
{
|
||||||
private readonly IEnvelopeExecutor _envelopeExecutor;
|
private readonly IServiceProvider _provider;
|
||||||
|
|
||||||
|
private IEnvelopeExecutor Executor => _provider.GetRequiredService<IEnvelopeExecutor>();
|
||||||
|
|
||||||
|
private IRepository<Envelope> Repository => _provider.GetRequiredService<IRepository<Envelope>>();
|
||||||
|
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="envelopeExecutor"></param>
|
/// <param name="provider"></param>
|
||||||
/// <param name="mapper"></param>
|
/// <param name="mapper"></param>
|
||||||
public CreateEnvelopeCommandHandler(IEnvelopeExecutor envelopeExecutor, IMapper mapper)
|
public CreateEnvelopeCommandHandler(IServiceProvider provider, IMapper mapper)
|
||||||
{
|
{
|
||||||
_envelopeExecutor = envelopeExecutor;
|
_provider = provider;
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,12 +36,14 @@ public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeComman
|
|||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancel"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<EnvelopeDto?> Handle(CreateEnvelopeCommand request, CancellationToken cancellationToken)
|
public async Task<EnvelopeDto?> Handle(CreateEnvelopeCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
var envelope = await _envelopeExecutor.CreateEnvelopeAsync(request.UserId, request.Title, request.Message, request.TFAEnabled, cancellationToken);
|
var envelope = request.UseSQLExecutor
|
||||||
|
? await Executor.CreateEnvelopeAsync(request.UserId, request.Title, request.Message, request.TFAEnabled, cancel)
|
||||||
|
: await Repository.CreateAsync(request, cancel);
|
||||||
|
|
||||||
return _mapper.Map<EnvelopeDto>(envelope);
|
return _mapper.Map<EnvelopeDto>(envelope);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user