using MediatR; using Microsoft.AspNetCore.Mvc; using ReC.API.Extensions; using ReC.API.Models; using ReC.Application.RecActions.Commands; using ReC.Application.RecActions.Queries; using System.Text.Json; namespace ReC.API.Controllers; [Route("api/[controller]")] [ApiController] public class RecActionController(IMediator mediator, IConfiguration config) : ControllerBase { /// /// Invokes a batch of RecActions for a given profile. /// /// The ID of the profile. /// A token to cancel the operation. /// An HTTP 202 Accepted response indicating the process has been started. [HttpPost("invoke/{profileId}")] [ProducesResponseType(StatusCodes.Status202Accepted)] public async Task Invoke([FromRoute] int profileId, CancellationToken cancel) { await mediator.InvokeBatchRecActionView(profileId, cancel); return Accepted(); } /// /// Invokes a batch of RecActions for a fake/test profile. /// /// A token to cancel the operation. /// An HTTP 202 Accepted response indicating the process has been started. [HttpPost("invoke/fake")] [ProducesResponseType(StatusCodes.Status202Accepted)] public async Task Invoke(CancellationToken cancel) { await mediator.InvokeBatchRecActionView(config.GetFakeProfileId(), cancel); return Accepted(); } #region CRUD /// /// Gets all RecActions for a given profile. /// /// /// A token to cancel the operation. /// A list of RecActions for the specified profile. [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] public async Task Get([FromQuery] ReadRecActionViewQuery query, CancellationToken cancel) => Ok(await mediator.Send(query, cancel)); /// /// Gets all RecActions for a fake/test profile. /// /// A token to cancel the operation. /// /// A list of RecActions for the fake profile. [HttpGet("fake")] [ProducesResponseType(StatusCodes.Status200OK)] public async Task Get(CancellationToken cancel, [FromQuery] bool invoked = false) => Ok(await mediator.Send(new ReadRecActionViewQuery() { ProfileId = config.GetFakeProfileId(), Invoked = invoked }, cancel)); /// /// Creates a new RecAction. /// /// The command containing the details for the new RecAction. /// A token to cancel the operation. /// An HTTP 201 Created response. [HttpPost] [ProducesResponseType(StatusCodes.Status201Created)] [Obsolete("Use the related procedure.")] public async Task CreateAction([FromBody] CreateRecActionCommand command, CancellationToken cancel) { await mediator.Send(command, cancel); return CreatedAtAction(nameof(CreateAction), null); } /// /// Creates a new fake RecAction for testing purposes. /// /// A token to cancel the operation. /// The optional request body and header for the fake action. /// The target endpoint URI. /// The optional path to append to the endpoint URI. /// The HTTP method type (e.g., GET, POST). /// An HTTP 201 Created response. [HttpPost("fake")] [ProducesResponseType(StatusCodes.Status201Created)] [Obsolete("Use the related procedure.")] public async Task CreateFakeAction( CancellationToken cancel, [FromBody] FakeRequest? request = null, [FromQuery] string endpointUri = "https://jsonplaceholder.typicode.com/posts", [FromQuery] string? endpointPath = "1", [FromQuery] string type = "GET") { if (endpointPath is not null) endpointUri = new Uri(new Uri(endpointUri.TrimEnd('/') + "/"), endpointPath.TrimStart('/')).ToString(); 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 = config.GetFakeProfileId(), EndpointUri = endpointUri, Type = type, BodyQuery = $@"SELECT '{bodyJson ?? "NULL"}' AS REQUEST_BODY;", HeaderQuery = headerJson is not null ? $@"SELECT '{headerJson}' AS REQUEST_HEADER;" : null, Active = true, EndpointAuthId = 4 }, cancel); return CreatedAtAction(nameof(CreateFakeAction), null); } /// /// Deletes all RecActions associated with a specific profile. /// /// /// A token to cancel the operation. /// An HTTP 204 No Content response upon successful deletion. [HttpDelete] [ProducesResponseType(StatusCodes.Status204NoContent)] [Obsolete("Use the related procedure.")] public async Task Delete([FromQuery] DeleteRecActionsCommand cmd, CancellationToken cancel) { await mediator.Send(cmd, cancel); return NoContent(); } /// /// Deletes all RecActions for a fake/test profile. /// /// A token to cancel the operation. /// An HTTP 204 No Content response upon successful deletion. [HttpDelete("fake")] [ProducesResponseType(StatusCodes.Status204NoContent)] [Obsolete("Use the related procedure.")] public async Task Delete(CancellationToken cancel) { await mediator.Send(new DeleteRecActionsCommand() { ProfileId = config.GetFakeProfileId() }, cancel); return NoContent(); } #endregion CRUD }