using API.Controllers.DTOs;
using API.Schema.ActionsContext;
using API.Schema.ActionsContext.Actions;
using Asp.Versioning;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using static Microsoft.AspNetCore.Http.StatusCodes;
using ActionRecord = API.Controllers.DTOs.ActionRecord;
namespace API.Controllers;
[ApiVersion(2)]
[ApiController]
[Route("v{v:apiVersion}/[controller]")]
public class ActionsController(ActionsContext context) : Controller
{
///
/// Returns the available Action Types ()
///
/// List of action-types
[HttpGet("Types")]
[ProducesResponseType(Status200OK, "application/json")]
public Ok GetAvailableActions()
{
return TypedResults.Ok(Enum.GetValues());
}
public sealed record Filter(DateTime? Start, DateTime? End, string? MangaId, string? ChapterId, ActionsEnum? Action);
///
/// Returns performed in
///
/// List of performed actions
/// Database error
[HttpPost("Filter")]
[ProducesResponseType>(Status200OK, "application/json")]
[ProducesResponseType(Status500InternalServerError)]
public async Task>, InternalServerError>> GetActionsInterval([FromBody]Filter filter)
{
if (await context.FilterActions(filter.MangaId, filter.ChapterId)
.Where(a => filter.Start == null || a.PerformedAt >= filter.Start.Value.ToUniversalTime())
.Where(a => filter.End == null || a.PerformedAt <= filter.End.Value.ToUniversalTime())
.Where(a => filter.Action == null || a.Action == filter.Action)
.ToListAsync(HttpContext.RequestAborted) is not { } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
}
///
/// Returns with
///
/// List of performed actions
/// Database error
[HttpGet("Type/{Type}")]
[ProducesResponseType>(Status200OK, "application/json")]
[ProducesResponseType(Status500InternalServerError)]
public async Task>, InternalServerError>> GetActionsWithType(ActionsEnum Type)
{
if (await context.Actions.Where(a => a.Action == Type)
.ToListAsync(HttpContext.RequestAborted) is not { } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
}
///
/// Returns related to
///
/// List of performed actions
/// Database error
[HttpGet("RelatedTo/Manga/{MangaId}")]
[ProducesResponseType>(Status200OK, "application/json")]
[ProducesResponseType(Status500InternalServerError)]
public async Task>, InternalServerError>> GetActionsRelatedToManga(string MangaId)
{
if(await context.FilterActionsManga(MangaId).ToListAsync(HttpContext.RequestAborted) is not { } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
}
///
/// Returns related to
///
/// List of performed actions
/// Database error
[HttpGet("RelatedTo/Chapter/{ChapterId}")]
[ProducesResponseType>(Status200OK, "application/json")]
[ProducesResponseType(Status500InternalServerError)]
public async Task>, InternalServerError>> GetActionsRelatedToChapter(string ChapterId)
{
if(await context.FilterActionsChapter(ChapterId).ToListAsync(HttpContext.RequestAborted) is not { } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
}
}