From fdae4d26be3b2dcad29e2ca72c548ed41ed1f2db Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 24 Mar 2026 14:40:52 +0100 Subject: [PATCH] Add PostprocessingBehavior for action result logging Introduced PostprocessingBehavior as an IPipelineBehavior for InvokeRecActionViewCommand. This behavior handles post-processing by executing optional queries, serializing results, and logging outcomes via InsertResultCommand. It also manages error handling based on the action's ErrorAction setting, ensuring consistent result tracking after command execution. --- .../Action/PostprocessingBehavior.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs diff --git a/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs new file mode 100644 index 0000000..88f7d43 --- /dev/null +++ b/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs @@ -0,0 +1,49 @@ +using MediatR; +using ReC.Application.Common.Interfaces; +using ReC.Application.RecActions.Commands; +using ReC.Application.Results.Commands; +using ReC.Domain.Constants; +using System.Text.Json; + +namespace ReC.Application.Common.Behaviors.Action; + +public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior +{ + public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate next, CancellationToken cancel) + { + var response = false; + try + { + response = await next(cancel); + } + catch + { + if (request.Action.ErrorAction == ErrorAction.Stop) + return false; + } + + string? info = null, error = null; + + try + { + if (request.Action.PostprocessingQuery is string query) + { + var result = await context.ExecuteDynamicSqlAsync(query, cancel); + info = JsonSerializer.Serialize(result); + } + } + catch (Exception ex) + { + error = ex.ToString(); + } + + await sender.Send(new InsertResultCommand() + { + ActionId = request.Action.Id, + Info = info, + Error = error + }, cancel); + + return response; + } +} \ No newline at end of file