Updated HeaderQueryBehavior to support two generic type parameters, TRequest and TResponse, for improved flexibility. Replaced the single TRecAction type parameter and Unit return type with a more generic implementation. Updated where constraints to reflect the new generic types. Modified the Handle method signature and logic to align with the updated generic parameters.
44 lines
1.6 KiB
C#
44 lines
1.6 KiB
C#
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<TRequest, TResponse>(IRecDbContext dbContext, ILogger<HeaderQueryBehavior<TRequest, TResponse>>? logger = null) : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : RecActionDto
|
|
where TResponse : notnull
|
|
{
|
|
public async Task<TResponse> Handle(TRequest action, RequestHandlerDelegate<TResponse> next, CancellationToken cancel)
|
|
{
|
|
if (action.HeaderQuery is null)
|
|
return await next(cancel);
|
|
|
|
var result = await dbContext.HeaderQueryResults.FromSqlRaw(action.HeaderQuery).SingleOrDefaultAsync(cancel);
|
|
|
|
if (result?.RawHeader is null)
|
|
{
|
|
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);
|
|
|
|
return await next(cancel);
|
|
}
|
|
|
|
var headerDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(result.RawHeader);
|
|
|
|
if(headerDict is null)
|
|
{
|
|
logger?.LogWarning(
|
|
"Header JSON deserialization returned null. RawHeader: {RawHeader}, ProfileId: {ProfileId}, Id: {Id}",
|
|
result.RawHeader, action.ProfileId, action.Id);
|
|
|
|
return await next(cancel);
|
|
}
|
|
|
|
action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString());
|
|
|
|
return await next(cancel);
|
|
}
|
|
}
|