diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs new file mode 100644 index 00000000..e147218f --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs @@ -0,0 +1,35 @@ +namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; + +/// +/// Defines methods for executing raw SQL queries and mapping the results to objects. +/// +/// The entity type to which the SQL query results will be mapped. +public interface ISQLExecutor +{ + /// + /// Executes a raw SQL query and returns the first result or null if no result is found. + /// + /// The raw SQL query to execute. + /// Optional cancellation token. + /// Optional parameters for the SQL query. + /// The first result, or null if none found. + Task ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters); + + /// + /// Executes a raw SQL query and expects a single result, or null if no result is found. + /// + /// The raw SQL query to execute. + /// Optional cancellation token. + /// Optional parameters for the SQL query. + /// The single result, or null if none found. + Task ExecuteSingleAsync(string sql, CancellationToken cancellation = default, params object[] parameters); + + /// + /// Executes a raw SQL query and returns all results as a collection of . + /// + /// The raw SQL query to execute. + /// Optional cancellation token. + /// Optional parameters for the SQL query. + /// An containing all matching results. + Task> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters); +} diff --git a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj index 73ba45fe..0406d46a 100644 --- a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj +++ b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj @@ -80,4 +80,8 @@ + + + + diff --git a/EnvelopeGenerator.Infrastructure/SQLExecutor.cs b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs new file mode 100644 index 00000000..fdb69dd4 --- /dev/null +++ b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs @@ -0,0 +1,32 @@ +using EnvelopeGenerator.Application.Contracts.SQLExecutor.cs; +using Microsoft.EntityFrameworkCore; + +namespace EnvelopeGenerator.Infrastructure; + +public sealed class SQLExecutor : ISQLExecutor where T : class +{ + private readonly EGDbContext _context; + + public SQLExecutor(EGDbContext context) + { + _context = context; + } + + public async Task ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters) + => await _context + .Set() + .FromSqlRaw(sql, parameters) + .FirstOrDefaultAsync(cancellation); + + public async Task ExecuteSingleAsync(string sql, CancellationToken cancellation = default, params object[] parameters) + => await _context + .Set() + .FromSqlRaw(sql, parameters) + .SingleOrDefaultAsync(cancellation); + + public async Task> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters) + => await _context + .Set() + .FromSqlRaw(sql, parameters) + .ToListAsync(cancellation); +}