Files
Tranga/API/Controllers/ActionsController.cs
glax 53276e858b
Some checks are pending
Docker Image CI / build (push) Waiting to run
Actions initial commit
2025-10-16 02:53:02 +02:00

63 lines
2.6 KiB
C#

using API.Schema.ActionsContext;
using Asp.Versioning;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace API.Controllers;
[ApiVersion(2)]
[ApiController]
[Route("v{v:apiVersion}/[controller]")]
public class ActionsController(ActionsContext context) : Controller
{
/// <summary>
/// Returns the available Action Types (<see cref="ActionRecord.Action"/>) performed by Tranga
/// </summary>
/// <response code="200">List of performed action-types</response>
/// <response code="500">Database error</response>
[HttpGet("Types")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
public async Task<Results<Ok<List<string>>, InternalServerError>> GetAvailableActions()
{
if (await context.Actions.Select(a => a.Action).Distinct().ToListAsync(HttpContext.RequestAborted) is not
{ } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions);
}
public sealed record Interval(DateTime Start, DateTime End);
/// <summary>
/// Returns <see cref="ActionRecord"/> performed in <see cref="Interval"/>
/// </summary>
/// <response code="200">List of performed actions</response>
/// <response code="500">Database error</response>
[HttpPost("Interval")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsInterval([FromBody]Interval interval)
{
if (await context.Actions.Where(a => a.PerformedAt >= interval.Start && a.PerformedAt <= interval.End)
.ToListAsync(HttpContext.RequestAborted) is not { } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions);
}
/// <summary>
/// Returns <see cref="ActionRecord"/> with type <paramref name="Type"/>
/// </summary>
/// <response code="200">List of performed actions</response>
/// <response code="500">Database error</response>
[HttpGet("Type/{Type}")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsWithType(string Type)
{
if (await context.Actions.Where(a => a.Action == Type)
.ToListAsync(HttpContext.RequestAborted) is not { } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions);
}
}