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 MediatR;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using ReC.API.Models;
|
||||||
using ReC.Application.RecActions.Commands;
|
using ReC.Application.RecActions.Commands;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
@ -27,25 +28,24 @@ public class ActionController(IMediator mediator) : ControllerBase
|
|||||||
|
|
||||||
[HttpPost("fake")]
|
[HttpPost("fake")]
|
||||||
public async Task<IActionResult> CreateFakeAction(
|
public async Task<IActionResult> CreateFakeAction(
|
||||||
string endpointUri = "https://jsonplaceholder.typicode.com/posts",
|
[FromBody] FakeRequest? request = null,
|
||||||
string? endpointPath = null,
|
[FromQuery] string endpointUri = "https://jsonplaceholder.typicode.com/posts",
|
||||||
string type = "GET",
|
[FromQuery] string? endpointPath = null,
|
||||||
Dictionary<string, object>? body = null,
|
[FromQuery] string type = "GET")
|
||||||
Dictionary<string, object>? header = null)
|
|
||||||
{
|
{
|
||||||
if (endpointPath is not null)
|
if (endpointPath is not null)
|
||||||
endpointUri = new Uri(new Uri(endpointUri.TrimEnd('/') + "/"), endpointPath.TrimStart('/')).ToString();
|
endpointUri = new Uri(new Uri(endpointUri.TrimEnd('/') + "/"), endpointPath.TrimStart('/')).ToString();
|
||||||
|
|
||||||
var bodyJson = JsonSerializer.Serialize(body, options: new() { WriteIndented = false });
|
var bodyJson = request?.Body is not null ? JsonSerializer.Serialize(request.Body, options: new() { WriteIndented = false }) : null;
|
||||||
var headerJson = JsonSerializer.Serialize(header, options: new() { WriteIndented = false });
|
var headerJson = request?.Header is not null ? JsonSerializer.Serialize(request.Header, options: new() { WriteIndented = false }) : null;
|
||||||
|
|
||||||
await mediator.Send(new CreateRecActionCommand()
|
await mediator.Send(new CreateRecActionCommand()
|
||||||
{
|
{
|
||||||
ProfileId = 2,
|
ProfileId = 2,
|
||||||
EndpointUri = endpointUri,
|
EndpointUri = endpointUri,
|
||||||
Type = type,
|
Type = type,
|
||||||
BodyQuery = $@"SELECT '{bodyJson}' AS REQUEST_BODY;",
|
BodyQuery = $@"SELECT '{bodyJson ?? "NULL"}' AS REQUEST_BODY;",
|
||||||
HeaderQuery = $@"SELECT '{headerJson}' AS REQUEST_HEADER;",
|
HeaderQuery = headerJson is not null ? $@"SELECT '{headerJson}' AS REQUEST_HEADER;" : null,
|
||||||
Active = true
|
Active = true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user