using MediatR;
using Microsoft.AspNetCore.Mvc;
using ReC.Application.Common.Procedures.DeleteProcedure;
using ReC.Application.Common.Procedures.InsertProcedure;
using ReC.Application.Common.Procedures.UpdateProcedure;
using ReC.Application.RecActions.Commands;
using ReC.Application.RecActions.Queries;
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();
}
#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));
///
/// 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)]
public async Task Create([FromBody] InsertActionProcedure command, CancellationToken cancel)
{
await mediator.ExecuteInsertProcedure(command, config["AddedWho"], cancel);
return StatusCode(StatusCodes.Status201Created);
}
///
/// Updates a RecAction via the ACTION update procedure.
///
/// RecAction identifier to update.
/// UpdateActionProcedure payload.
/// A token to cancel the operation.
/// No content on success.
[HttpPut("{id:long}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task Update([FromRoute] long id, [FromBody] UpdateActionProcedure procedure, CancellationToken cancel)
{
await mediator.ExecuteUpdateProcedure(procedure, id, cancel: cancel);
return NoContent();
}
///
/// Deletes RecActions via the ACTION delete procedure for the specified id range.
///
/// DeleteActionProcedure payload (Start, End, Force).
/// A token to cancel the operation.
/// An HTTP 204 No Content response upon successful deletion.
[HttpDelete]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task Delete([FromBody] DeleteActionProcedure procedure, CancellationToken cancel)
{
await mediator.ExecuteDeleteProcedure(procedure, cancel);
return NoContent();
}
#endregion CRUD
}