using MediatR; using Microsoft.AspNetCore.Mvc; using ReC.API.Extensions; 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(); } /// /// 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)] 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(); } /// /// Deletes RecActions for a fake/test profile via the ACTION delete procedure. /// /// A token to cancel the operation. /// An HTTP 204 No Content response upon successful deletion. [HttpDelete("fake")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task Delete(CancellationToken cancel) { await mediator.ExecuteDeleteProcedure(new DeleteActionProcedure { Start = config.GetFakeProfileId(), End = config.GetFakeProfileId(), Force = false }, cancel); return NoContent(); } #endregion CRUD }