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 { /// /// Returns the available Action Types () performed by Tranga /// /// List of performed action-types /// Database error [HttpGet("Types")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status500InternalServerError)] public async Task>, 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); /// /// Returns performed in /// /// List of performed actions /// Database error [HttpPost("Interval")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status500InternalServerError)] public async Task>, 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); } /// /// Returns with type /// /// List of performed actions /// Database error [HttpGet("Type/{Type}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status500InternalServerError)] public async Task>, 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); } }