Refactor InvokeRecActionCommand to use composition

Refactored `InvokeRecActionCommand` to remove inheritance from
`RecActionDto` and introduced a new `Action` property to
encapsulate the data. Updated the `ToInvokeCommand` extension
method to align with this change.

Modified `InvokeRecActionCommandHandler` to reference the
`Action` property instead of directly accessing inherited
properties. Updated HTTP request creation logic and the
`CreateOutResCommand` instantiation to use the `Action` object.

This change improves code clarity and adheres to composition
over inheritance principles.
This commit is contained in:
tekh 2025-12-01 12:30:24 +01:00
parent 9c028c5e66
commit dc777a04f6

View File

@ -8,16 +8,14 @@ using System.Text.Json;
namespace ReC.Application.RecActions.Commands; namespace ReC.Application.RecActions.Commands;
public record InvokeRecActionCommand : RecActionDto, IRequest public record InvokeRecActionCommand : IRequest
{ {
public InvokeRecActionCommand(RecActionDto root) : base(root) { } public RecActionDto Action { get; set; }
public InvokeRecActionCommand() { }
} }
public static class InvokeRecActionCommandExtensions public static class InvokeRecActionCommandExtensions
{ {
public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new(dto); public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new() { Action = dto };
} }
public class InvokeRecActionCommandHandler( public class InvokeRecActionCommandHandler(
@ -29,30 +27,31 @@ public class InvokeRecActionCommandHandler(
{ {
public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel)
{ {
var action = request.Action;
using var http = clientFactory.CreateClient(); using var http = clientFactory.CreateClient();
if (request.RestType is null) if (action.RestType is null)
{ {
logger?.LogWarning( logger?.LogWarning(
"Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}", "Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}",
request.ProfileId, action.ProfileId,
request.ActionId action.ActionId
); );
return; return;
} }
using var httpReq = request.RestType using var httpReq = action.RestType
.ToHttpMethod() .ToHttpMethod()
.ToHttpRequestMessage(request.EndpointUri); .ToHttpRequestMessage(action.EndpointUri);
if(request.Body is not null) if(action.Body is not null)
{ {
using var reqBody = new StringContent(request.Body); using var reqBody = new StringContent(action.Body);
httpReq.Content = reqBody; httpReq.Content = reqBody;
} }
if (request.Headers is not null) if (action.Headers is not null)
foreach (var header in request.Headers) foreach (var header in action.Headers)
httpReq.Headers.Add(header.Key, header.Value); httpReq.Headers.Add(header.Key, header.Value);
using var response = await http.SendAsync(httpReq, cancel); using var response = await http.SendAsync(httpReq, cancel);
@ -61,7 +60,7 @@ public class InvokeRecActionCommandHandler(
await sender.Send(new CreateOutResCommand await sender.Send(new CreateOutResCommand
{ {
ActionId = request.ActionId, ActionId = action.ActionId,
Header = JsonSerializer.Serialize(resHeaders, options: new() { WriteIndented = false }), Header = JsonSerializer.Serialize(resHeaders, options: new() { WriteIndented = false }),
Body = resBody, Body = resBody,
AddedWho = config?["AddedWho"] AddedWho = config?["AddedWho"]