Implement HeaderQueryBehavior with database support
Updated `HeaderQueryBehavior` to include dependency injection for `IRecDbContext` and `ILogger`, enabling database interaction and logging. Replaced the placeholder `NotImplementedException` in the `Handle` method with functionality to process `HeaderQuery` SQL results, deserialize JSON headers, and assign them to the `Headers` property of the action. Added error handling and logging for deserialization failures. Updated constructor and method signatures to support new dependencies and asynchronous behavior. Added necessary `using` directives for new features.
This commit is contained in:
parent
03498275d7
commit
6f5409f528
@ -1,14 +1,35 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ReC.Application.Common.Dto;
|
using ReC.Application.Common.Dto;
|
||||||
|
using ReC.Application.Common.Interfaces;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace ReC.Application.Common.Behaviors;
|
namespace ReC.Application.Common.Behaviors;
|
||||||
|
|
||||||
public class HeaderQueryBehavior<TRecAction> : IPipelineBehavior<TRecAction, Unit>
|
public class HeaderQueryBehavior<TRecAction>(IRecDbContext dbContext, ILogger<HeaderQueryBehavior<TRecAction>>? logger = null) : IPipelineBehavior<TRecAction, Unit>
|
||||||
where TRecAction : RecActionDto
|
where TRecAction : RecActionDto
|
||||||
{
|
{
|
||||||
public Task<Unit> Handle(TRecAction action, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
|
public async Task<Unit> Handle(TRecAction action, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
// Logic to process the header query can be added here
|
if (action.HeaderQuery is null)
|
||||||
throw new NotImplementedException("HeaderQueryBehavior is not implemented yet.");
|
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<Dictionary<string, JsonElement>>(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user