diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs new file mode 100644 index 00000000..00c0cd8d --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs @@ -0,0 +1,20 @@ +namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; + +/// +/// Represents a raw SQL query contract. +/// +public interface ISQL +{ + /// + /// Gets the raw SQL query string. + /// + string Raw { get; } +} + +/// +/// Represents a typed SQL query contract for a specific entity. +/// +/// The type of the entity associated with the SQL query. +public interface ISQL : ISQL +{ +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs index e147218f..245657e9 100644 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs @@ -32,4 +32,10 @@ public interface ISQLExecutor /// Optional parameters for the SQL query. /// An containing all matching results. Task> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters); + + Task ExecuteFirstAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL; + + Task ExecuteSingleAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL; + + Task> ExecuteAllAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL; } diff --git a/EnvelopeGenerator.Infrastructure/SQLExecutor.cs b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs index fdb69dd4..050beb28 100644 --- a/EnvelopeGenerator.Infrastructure/SQLExecutor.cs +++ b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs @@ -1,5 +1,6 @@ -using EnvelopeGenerator.Application.Contracts.SQLExecutor.cs; +using EnvelopeGenerator.Application.Contracts.SQLExecutor; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; namespace EnvelopeGenerator.Infrastructure; @@ -7,6 +8,8 @@ public sealed class SQLExecutor : ISQLExecutor where T : class { private readonly EGDbContext _context; + private readonly IServiceProvider _provider; + public SQLExecutor(EGDbContext context) { _context = context; @@ -29,4 +32,22 @@ public sealed class SQLExecutor : ISQLExecutor where T : class .Set() .FromSqlRaw(sql, parameters) .ToListAsync(cancellation); + + public Task ExecuteFirstAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL + { + var sql = _provider.GetRequiredService(); + return ExecuteFirstAsync(sql.Raw); + } + + public Task ExecuteSingleAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL + { + var sql = _provider.GetRequiredService(); + return ExecuteSingleAsync(sql.Raw); + } + + public Task> ExecuteAllAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL + { + var sql = _provider.GetRequiredService(); + return ExecuteAllAsync(sql.Raw); + } }