Refactor CreateFakeAction to use FakeRequest model
Refactored the `CreateFakeAction` method in `ActionController`: - Replaced individual parameters with a strongly-typed `FakeRequest` model for the request body. - Added `[FromQuery]` attributes for `endpointUri`, `endpointPath`, and `type` to allow query parameter overrides. - Updated serialization logic for `Body` and `Header` to handle null values more explicitly. - Adjusted `BodyQuery` and `HeaderQuery` assignments to improve null handling. Also updated `using` directives to include `ReC.API.Models` and `ReC.Application.RecActions.Commands`.
This commit is contained in:
parent
81aca90c39
commit
b5b1f53e21
@ -1,5 +1,6 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ReC.API.Models;
|
||||
using ReC.Application.RecActions.Commands;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
@ -27,25 +28,24 @@ public class ActionController(IMediator mediator) : ControllerBase
|
||||
|
||||
[HttpPost("fake")]
|
||||
public async Task<IActionResult> CreateFakeAction(
|
||||
string endpointUri = "https://jsonplaceholder.typicode.com/posts",
|
||||
string? endpointPath = null,
|
||||
string type = "GET",
|
||||
Dictionary<string, object>? body = null,
|
||||
Dictionary<string, object>? header = null)
|
||||
[FromBody] FakeRequest? request = null,
|
||||
[FromQuery] string endpointUri = "https://jsonplaceholder.typicode.com/posts",
|
||||
[FromQuery] string? endpointPath = null,
|
||||
[FromQuery] string type = "GET")
|
||||
{
|
||||
if (endpointPath is not null)
|
||||
endpointUri = new Uri(new Uri(endpointUri.TrimEnd('/') + "/"), endpointPath.TrimStart('/')).ToString();
|
||||
|
||||
var bodyJson = JsonSerializer.Serialize(body, options: new() { WriteIndented = false });
|
||||
var headerJson = JsonSerializer.Serialize(header, options: new() { WriteIndented = false });
|
||||
var bodyJson = request?.Body is not null ? JsonSerializer.Serialize(request.Body, options: new() { WriteIndented = false }) : null;
|
||||
var headerJson = request?.Header is not null ? JsonSerializer.Serialize(request.Header, options: new() { WriteIndented = false }) : null;
|
||||
|
||||
await mediator.Send(new CreateRecActionCommand()
|
||||
{
|
||||
ProfileId = 2,
|
||||
EndpointUri = endpointUri,
|
||||
Type = type,
|
||||
BodyQuery = $@"SELECT '{bodyJson}' AS REQUEST_BODY;",
|
||||
HeaderQuery = $@"SELECT '{headerJson}' AS REQUEST_HEADER;",
|
||||
BodyQuery = $@"SELECT '{bodyJson ?? "NULL"}' AS REQUEST_BODY;",
|
||||
HeaderQuery = headerJson is not null ? $@"SELECT '{headerJson}' AS REQUEST_HEADER;" : null,
|
||||
Active = true
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user