using MediatR;
using Microsoft.AspNetCore.Mvc;
using ReC.API.Extensions;
using ReC.Application.OutResults.Commands;
using ReC.Application.OutResults.Queries;
namespace ReC.API.Controllers;
[Route("api/[controller]")]
[ApiController]
public class OutResController(IMediator mediator, IConfiguration config) : ControllerBase
{
///
/// Gets output results based on the provided query parameters.
///
/// The query to filter output results.
/// A token to cancel the operation.
/// A list of output results matching the query.
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task Get([FromQuery] ReadOutResQuery query, CancellationToken cancel) => Ok(await mediator.Send(query, cancel));
///
/// Gets output results for a fake/test profile.
///
/// A token to cancel the operation.
/// A list of output results for the fake profile.
[HttpGet("fake")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task Get(CancellationToken cancel) => Ok(await mediator.Send(new ReadOutResQuery()
{
ProfileId = config.GetFakeProfileId()
}, cancel));
///
/// Gets a specific output result for a fake/test profile and action.
///
/// The ID of the action to retrieve the result for.
/// A token to cancel the operation.
/// Specifies which part of the result to return (Full, Header, or Body).
/// The requested output result or a part of it (header/body).
[HttpGet("fake/{actionId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task Get([FromRoute] long actionId, CancellationToken cancel, ResultType resultType = ResultType.Full)
{
var res = (await mediator.Send(new ReadOutResQuery()
{
ProfileId = config.GetFakeProfileId(),
ActionId = actionId
}, cancel)).First();
return resultType switch
{
ResultType.Body => res.Body is null ? Ok(new object { }) : Ok(res.Body.JsonToDynamic()),
ResultType.Header => res.Header is null ? Ok(new object { }) : Ok(res.Header.JsonToDynamic()),
_ => Ok(res),
};
}
///
/// Deletes output results based on the provided criteria.
///
/// The command containing the deletion criteria, such as ActionId or ProfileId.
/// A token to cancel the operation.
/// An empty response indicating success.
[HttpDelete]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task Delete([FromQuery] DeleteOutResCommand command, CancellationToken cancel)
{
await mediator.Send(command, cancel);
return NoContent();
}
///
/// Deletes all output results for a fake/test profile.
///
/// A token to cancel the operation.
/// An empty response indicating success.
[HttpDelete("fake")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task Delete(CancellationToken cancel)
{
await mediator.Send(new DeleteOutResCommand() { ProfileId = config.GetFakeProfileId() }, cancel);
return NoContent();
}
}
///
/// Defines the type of result to be returned from an output result query.
///
public enum ResultType
{
///
/// Return the full result object.
///
Full,
///
/// Return only the header part of the result.
///
Header,
///
/// Return only the body part of the result.
///
Body
}