diff --git a/src/ReC.Application/Common/Behaviors/Action/BodyQueryBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/BodyQueryBehavior.cs index 693b89c..26b27cc 100644 --- a/src/ReC.Application/Common/Behaviors/Action/BodyQueryBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/Action/BodyQueryBehavior.cs @@ -1,23 +1,41 @@ -using MediatR; +using MediatR; +using Microsoft.EntityFrameworkCore; using ReC.Application.Common.Dto; using ReC.Application.Common.Interfaces; -using Microsoft.EntityFrameworkCore; namespace ReC.Application.Common.Behaviors.Action; public class BodyQueryBehavior(IRecDbContext dbContext) : IPipelineBehavior - where TRequest : RecActionViewDto - where TResponse : notnull + where TRequest : notnull + where TResponse : IEnumerable { - public async Task Handle(TRequest action, RequestHandlerDelegate next, CancellationToken cancel) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancel) { - if (action.BodyQuery is null) - return await next(cancel); + var actions = await next(cancel); - var result = await dbContext.BodyQueryResults.FromSqlRaw(action.BodyQuery).SingleOrDefaultAsync(cancel); + foreach (var action in actions) + await SetBody(action, cancel); - action.Body = result?.RawBody; + return actions; + } - return await next(cancel); + private async Task SetBody(RecActionViewDto action, CancellationToken cancel) + { + if (action.BodyQuery is not string bodyQuery) + return; + + await using var command = dbContext.Database.GetDbConnection().CreateCommand(); + command.CommandText = bodyQuery; + + await dbContext.Database.OpenConnectionAsync(cancel); + try + { + var scalar = await command.ExecuteScalarAsync(cancel); + action.Body = scalar as string; + } + finally + { + await dbContext.Database.CloseConnectionAsync(); + } } } diff --git a/src/ReC.Application/Common/Behaviors/Action/HeaderQueryBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/HeaderQueryBehavior.cs index 889d0cb..1a2ec24 100644 --- a/src/ReC.Application/Common/Behaviors/Action/HeaderQueryBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/Action/HeaderQueryBehavior.cs @@ -1,4 +1,4 @@ -using MediatR; +using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using ReC.Application.Common.Dto; @@ -8,36 +8,53 @@ using System.Text.Json; namespace ReC.Application.Common.Behaviors.Action; public class HeaderQueryBehavior(IRecDbContext dbContext, ILogger>? logger = null) : IPipelineBehavior - where TRequest : RecActionViewDto - where TResponse : notnull + where TRequest : notnull + where TResponse : IEnumerable { - public async Task Handle(TRequest action, RequestHandlerDelegate next, CancellationToken cancel) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancel) { - if (action.HeaderQuery is null) - return await next(cancel); + var actions = await next(cancel); - var result = await dbContext.HeaderQueryResults.FromSqlRaw(action.HeaderQuery).SingleOrDefaultAsync(cancel); + foreach (var action in actions) + await SetHeader(action, cancel); - if (result?.RawHeader is null) + return actions; + } + + private async Task SetHeader(RecActionViewDto action, CancellationToken cancel) + { + if (action.HeaderQuery is not string headerQuery) + return; + + await using var command = dbContext.Database.GetDbConnection().CreateCommand(); + command.CommandText = headerQuery; + + await dbContext.Database.OpenConnectionAsync(cancel); + try { - logger?.LogWarning("Header query did not return a result or returned a null REQUEST_HEADER. Profile ID: {ProfileId}, Action ID: {Id}", action.ProfileId, action.Id); + var scalar = await command.ExecuteScalarAsync(cancel); - return await next(cancel); + if (scalar is not string rawHeader) + { + logger?.LogWarning("Header query did not return a result or returned a null value. Profile ID: {ProfileId}, Action ID: {Id}", action.ProfileId, action.Id); + return; + } + + var headerDict = JsonSerializer.Deserialize>(rawHeader); + + if (headerDict is null) + { + logger?.LogWarning( + "Header JSON deserialization returned null. RawHeader: {RawHeader}, ProfileId: {ProfileId}, Id: {Id}", + rawHeader, action.ProfileId, action.Id); + return; + } + + action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString()); } - - var headerDict = JsonSerializer.Deserialize>(result.RawHeader); - - if(headerDict is null) + finally { - logger?.LogWarning( - "Header JSON deserialization returned null. RawHeader: {RawHeader}, ProfileId: {ProfileId}, Id: {Id}", - result.RawHeader, action.ProfileId, action.Id); - - return await next(cancel); + await dbContext.Database.CloseConnectionAsync(); } - - action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString()); - - return await next(cancel); } }