Refactor SQL execution and enhance envelope creation
- Updated `ISQLExecutor<TEntity>` to inherit from new `ISQLExecutor` interface for improved SQL execution flexibility. - Added package references for `Dapper` and `DigitalData.Core` in project files. - Modified `CreateEnvelopeCommand` to include `[BindNever]` on `UserId` for better model binding control. - Refactored `CreateEnvelopeCommandHandler` to use `DynamicParameters` for SQL parameter handling. - Updated `CreateEnvelopeSQL` to select only the top record for performance. - Introduced `GetIdOrDefault` method in `ControllerExtensions` for user ID retrieval with fallback. - Added `CreateAsync` method in `EnvelopeController` for envelope creation using `IMediator`. - Ensured infrastructure project has necessary package references. - Refactored `SQLExecutor` to implement new interface and simplified constructor. - Introduced `SQLExecutorBaseEntity` for entity-specific SQL command execution.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
/// Provides abstraction for raw SQL execution as well as mapping the results to <typeparamref name="TEntity"/> objects.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The entity type to which the SQL query results will be mapped.</typeparam>
|
||||
public interface ISQLExecutor<TEntity>
|
||||
public interface ISQLExecutor<TEntity>: ISQLExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes a raw SQL query and returns an <see cref="IQuery{TEntity}"/> for further querying operations on the result.
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using Dapper;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Contracts.SQLExecutor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface ISQLExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes a raw SQL query and returns an <see cref="IQuery{TEntity}"/> for further querying operations on the result.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The entity type to which the SQL query results will be mapped.</typeparam>
|
||||
/// <param name="sql">The raw SQL query to execute.</param>
|
||||
/// <param name="parameters">Parameters for the SQL query.</param>
|
||||
/// <param name="cancellation">Optional cancellation token for the operation.</param>
|
||||
/// <returns>An <see cref="IQuery{TEntity}"/> instance for further query operations on the result.</returns>
|
||||
Task<IEnumerable<TEntity>> Execute<TEntity>(string sql, DynamicParameters parameters, CancellationToken cancellation = default);
|
||||
|
||||
/// <summary>
|
||||
/// Executes a custom SQL query defined by a class that implements <see cref="ISQL{TEntity}"/> and returns an <see cref="IQuery{TEntity}"/> for further querying operations on the result.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The entity type to which the SQL query results will be mapped.</typeparam>
|
||||
/// <typeparam name="TSQL">The type of the custom SQL query class implementing <see cref="ISQL{TEntity}"/>.</typeparam>
|
||||
/// <param name="parameters">Parameters for the SQL query.</param>
|
||||
/// <param name="cancellation">Optional cancellation token for the operation.</param>
|
||||
/// <returns>An <see cref="IQuery{TEntity}"/> instance for further query operations on the result.</returns>
|
||||
Task<IEnumerable<TEntity>> Execute<TEntity, TSQL>(DynamicParameters parameters, CancellationToken cancellation = default) where TSQL : ISQL;
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="DigitalData.Core.Abstractions" Version="3.6.0" />
|
||||
<PackageReference Include="DigitalData.Core.Application" Version="3.2.1" />
|
||||
<PackageReference Include="DigitalData.Core.Client" Version="2.0.3" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
@@ -20,5 +21,6 @@ public record CreateEnvelopeCommand(
|
||||
/// Id of receiver
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
[BindNever]
|
||||
public int? UserId { get; set; }
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using AutoMapper;
|
||||
using Dapper;
|
||||
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
@@ -34,16 +35,14 @@ public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeComman
|
||||
/// <returns></returns>
|
||||
public async Task<CreateEnvelopeResponse?> Handle(CreateEnvelopeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
object[] parameters = new object[]
|
||||
{
|
||||
new SqlParameter("@UserId", request.UserId),
|
||||
new SqlParameter("@Title", request.Title),
|
||||
new SqlParameter("@TfaEnabled", request.TFAEnabled ? 1 : 0),
|
||||
new SqlParameter("@Message", request.Message)
|
||||
};
|
||||
var parameters = new DynamicParameters();
|
||||
parameters.Add("@UserId", request.UserId);
|
||||
parameters.Add("@Title", request.Title);
|
||||
parameters.Add("@TfaEnabled", request.TFAEnabled ? 1 : 0);
|
||||
parameters.Add("@Message", request.Message);
|
||||
|
||||
var envelope = await _sqlExecutor.Execute<CreateEnvelopeSQL>(cancellationToken, parameters).FirstOrDefaultAsync();
|
||||
var envelopes = await _sqlExecutor.Execute<Envelope, CreateEnvelopeSQL>(parameters, cancellationToken);
|
||||
|
||||
return _mapper.Map<CreateEnvelopeResponse>(envelope);
|
||||
return _mapper.Map<CreateEnvelopeResponse>(envelopes.FirstOrDefault());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class CreateEnvelopeSQL : ISQL<Envelope>
|
||||
@MESSAGE = @Message,
|
||||
@OUT_UID = @OUT_UID OUTPUT;
|
||||
|
||||
SELECT *
|
||||
SELECT TOP(1) *
|
||||
FROM [dbo].[TBSIG_ENVELOPE]
|
||||
WHERE [ENVELOPE_UUID] = @OUT_UID;
|
||||
";
|
||||
|
||||
Reference in New Issue
Block a user