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:
parent
9c028c5e66
commit
dc777a04f6
@ -8,16 +8,14 @@ using System.Text.Json;
|
||||
|
||||
namespace ReC.Application.RecActions.Commands;
|
||||
|
||||
public record InvokeRecActionCommand : RecActionDto, IRequest
|
||||
public record InvokeRecActionCommand : IRequest
|
||||
{
|
||||
public InvokeRecActionCommand(RecActionDto root) : base(root) { }
|
||||
|
||||
public InvokeRecActionCommand() { }
|
||||
public RecActionDto Action { get; set; }
|
||||
}
|
||||
|
||||
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(
|
||||
@ -29,30 +27,31 @@ public class InvokeRecActionCommandHandler(
|
||||
{
|
||||
public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel)
|
||||
{
|
||||
var action = request.Action;
|
||||
using var http = clientFactory.CreateClient();
|
||||
|
||||
if (request.RestType is null)
|
||||
if (action.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
|
||||
action.ProfileId,
|
||||
action.ActionId
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
using var httpReq = request.RestType
|
||||
using var httpReq = action.RestType
|
||||
.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;
|
||||
}
|
||||
|
||||
if (request.Headers is not null)
|
||||
foreach (var header in request.Headers)
|
||||
if (action.Headers is not null)
|
||||
foreach (var header in action.Headers)
|
||||
httpReq.Headers.Add(header.Key, header.Value);
|
||||
|
||||
using var response = await http.SendAsync(httpReq, cancel);
|
||||
@ -61,7 +60,7 @@ public class InvokeRecActionCommandHandler(
|
||||
|
||||
await sender.Send(new CreateOutResCommand
|
||||
{
|
||||
ActionId = request.ActionId,
|
||||
ActionId = action.ActionId,
|
||||
Header = JsonSerializer.Serialize(resHeaders, options: new() { WriteIndented = false }),
|
||||
Body = resBody,
|
||||
AddedWho = config?["AddedWho"]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user