diff --git a/src/ReC.Application/Common/Behaviors/HeaderQueryBehavior.cs b/src/ReC.Application/Common/Behaviors/HeaderQueryBehavior.cs index 9199da0..8149416 100644 --- a/src/ReC.Application/Common/Behaviors/HeaderQueryBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/HeaderQueryBehavior.cs @@ -1,14 +1,35 @@ using MediatR; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using ReC.Application.Common.Dto; +using ReC.Application.Common.Interfaces; +using System.Text.Json; namespace ReC.Application.Common.Behaviors; -public class HeaderQueryBehavior : IPipelineBehavior +public class HeaderQueryBehavior(IRecDbContext dbContext, ILogger>? logger = null) : IPipelineBehavior where TRecAction : RecActionDto { - public Task Handle(TRecAction action, RequestHandlerDelegate next, CancellationToken cancel) + public async Task Handle(TRecAction action, RequestHandlerDelegate next, CancellationToken cancel) { - // Logic to process the header query can be added here - throw new NotImplementedException("HeaderQueryBehavior is not implemented yet."); + if (action.HeaderQuery is null) + return await next(cancel); + + var result = await dbContext.HeaderQueryResults.FromSqlRaw(action.HeaderQuery).FirstOrDefaultAsync(cancel); + + if(result?.RawHeader is null) + return await next(cancel); + + var headerDict = JsonSerializer.Deserialize>(result.RawHeader); + + if(headerDict is null) + { + logger?.LogWarning("Failed to deserialize header query result: {RawHeader}. Profile ID: {ProfileId}, Action ID: {ActionId}", result.RawHeader, action.ProfileId, action.ActionId); + return await next(cancel); + } + + action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString()); + + return await next(cancel); } }