From 2f3a685e7d1c76eb9eed5eede3400fbbec8d56c5 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 11:26:46 +0100 Subject: [PATCH] Refactor Rec Actions handling with MediatR support Refactored the handling of "Rec Actions" by introducing the `InvokeRecActionCommandHandler` class and leveraging the MediatR library for command handling. Simplified the logic in `InvokeBatchRecActionsCommand.cs` by delegating HTTP request handling to the `sender.Send` method, which now uses `InvokeRecActionCommand`. Updated `InvokeRecActionCommand` to implement the `IRequest` interface, enabling MediatR pipeline compatibility. Added `InvokeRecActionCommandExtensions` for converting `RecActionDto` to `InvokeRecActionCommand`, improving readability. Centralized HTTP request logic in the new `InvokeRecActionCommandHandler`, which validates `RestType`, sends HTTP requests, and logs warnings for invalid actions. Removed unused `using` directives and added necessary ones for MediatR and logging. This refactoring improves modularity, testability, and maintainability by separating concerns and streamlining the code structure. --- .../Commands/InvokeBatchRecActionsCommand.cs | 17 +------- .../Commands/InvokeRecActionCommand.cs | 39 ++++++++++++++++--- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs index e993fea..b9c4d3e 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs @@ -27,22 +27,7 @@ public class InvokeRecActionsCommandHandler(ISender sender, IHttpClientFactory c await semaphore.WaitAsync(cancel); try { - if (action.RestType is null) - { - logger?.LogWarning( - "Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}", - action.ProfileId, - action.ActionId - ); - return; - } - - var method = new HttpMethod(action.RestType.ToUpper()); - var msg = new HttpRequestMessage(method, action.EndpointUri); - - var response = await http.SendAsync(msg, cancel); - var body = await response.Content.ReadAsStringAsync(cancel); - var headers = response.Headers.ToDictionary(); + await sender.Send(action.ToInvokeCommand(), cancel); } catch(Exception ex) { diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 2821435..bd56113 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -1,13 +1,12 @@ -using ReC.Application.Common.Dto; +using MediatR; +using Microsoft.Extensions.Logging; +using ReC.Application.Common.Dto; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using static System.Net.WebRequestMethods; namespace ReC.Application.RecActions.Commands; -public record InvokeRecActionCommand : RecActionDto +public record InvokeRecActionCommand : RecActionDto, IRequest { public InvokeRecActionCommand(RecActionDto root) : base(root) { } @@ -18,3 +17,31 @@ public static class InvokeRecActionCommandExtensions { public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new(dto); } + +public class InvokeRecActionCommandHandler( + IHttpClientFactory clientFactory, + ILogger? logger = null + ) : IRequestHandler +{ + public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) + { + var http = clientFactory.CreateClient(); + + if (request.RestType is null) + { + logger?.LogWarning( + "Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}", + request.ProfileId, + request.ActionId + ); + return; + } + + var method = new HttpMethod(request.RestType.ToUpper()); + var msg = new HttpRequestMessage(method, request.EndpointUri); + + var response = await http.SendAsync(msg, cancel); + var body = await response.Content.ReadAsStringAsync(cancel); + var headers = response.Headers.ToDictionary(); + } +} \ No newline at end of file