mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-17 10:50:45 +02:00
Add ActionRecord DTO
Include all Data in ActionRecord return Add endpoints for returning actions related to manga and chapter Fix wrong syncs for ActionsContext
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using API.Controllers.DTOs;
|
||||
using API.Schema.ActionsContext;
|
||||
using API.Schema.ActionsContext.Actions;
|
||||
using Asp.Versioning;
|
||||
@@ -5,6 +6,7 @@ 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;
|
||||
|
||||
@@ -26,34 +28,66 @@ public class ActionsController(ActionsContext context) : Controller
|
||||
|
||||
public sealed record Interval(DateTime Start, DateTime End);
|
||||
/// <summary>
|
||||
/// Returns <see cref="ActionRecord"/> performed in <see cref="Interval"/>
|
||||
/// Returns <see cref="Schema.ActionsContext.ActionRecord"/> performed in <see cref="Interval"/>
|
||||
/// </summary>
|
||||
/// <response code="200">List of performed actions</response>
|
||||
/// <response code="500">Database error</response>
|
||||
[HttpPost("Interval")]
|
||||
[ProducesResponseType<List<ActionRecord>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType<IEnumerable<ActionRecord>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsInterval([FromBody]Interval interval)
|
||||
public async Task<Results<Ok<IEnumerable<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);
|
||||
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see cref="ActionRecord"/> with <paramref name="Type"/> <see cref="ActionsEnum"/>
|
||||
/// Returns <see cref="Schema.ActionsContext.ActionRecord"/> with <paramref name="Type"/> <see cref="ActionsEnum"/>
|
||||
/// </summary>
|
||||
/// <response code="200">List of performed actions</response>
|
||||
/// <response code="500">Database error</response>
|
||||
[HttpGet("Type/{Type}")]
|
||||
[ProducesResponseType<List<ActionRecord>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType<IEnumerable<ActionRecord>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsWithType(ActionsEnum Type)
|
||||
public async Task<Results<Ok<IEnumerable<ActionRecord>>, 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);
|
||||
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see cref="Schema.ActionsContext.ActionRecord"/> related to <see cref="Manga"/>
|
||||
/// </summary>
|
||||
/// <response code="200">List of performed actions</response>
|
||||
/// <response code="500">Database error</response>
|
||||
[HttpGet("RelatedTo/Manga/{MangaId}")]
|
||||
[ProducesResponseType<IEnumerable<ActionRecord>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public async Task<Results<Ok<IEnumerable<ActionRecord>>, InternalServerError>> GetActionsRelatedToManga(string MangaId)
|
||||
{
|
||||
if(await context.Actions.FromSqlInterpolated($"""SELECT * FROM public."Actions" WHERE "MangaId" = {MangaId}""").ToListAsync() is not { } actions)
|
||||
return TypedResults.InternalServerError();
|
||||
|
||||
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see cref="Schema.ActionsContext.ActionRecord"/> related to <see cref="Chapter"/>
|
||||
/// </summary>
|
||||
/// <response code="200">List of performed actions</response>
|
||||
/// <response code="500">Database error</response>
|
||||
[HttpGet("RelatedTo/Chapter/{ChapterId}")]
|
||||
[ProducesResponseType<IEnumerable<ActionRecord>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public async Task<Results<Ok<IEnumerable<ActionRecord>>, InternalServerError>> GetActionsRelatedToChapter(string ChapterId)
|
||||
{
|
||||
if(await context.Actions.FromSqlInterpolated($"""SELECT * FROM public."Actions" WHERE "ChapterId" = {ChapterId}""").ToListAsync() is not { } actions)
|
||||
return TypedResults.InternalServerError();
|
||||
|
||||
return TypedResults.Ok(actions.Select(a => new ActionRecord(a)));
|
||||
}
|
||||
}
|
41
API/Controllers/DTOs/ActionRecord.cs
Normal file
41
API/Controllers/DTOs/ActionRecord.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
using API.Schema.ActionsContext.Actions;
|
||||
using API.Schema.ActionsContext.Actions.Generic;
|
||||
|
||||
namespace API.Controllers.DTOs;
|
||||
|
||||
public sealed record ActionRecord : Identifiable
|
||||
{
|
||||
public ActionRecord(Schema.ActionsContext.ActionRecord actionRecord) : base(actionRecord.Key)
|
||||
{
|
||||
Action = actionRecord.Action;
|
||||
PerformedAt = actionRecord.PerformedAt;
|
||||
MangaId = actionRecord is ActionWithMangaRecord m ? m.MangaId : null;
|
||||
ChapterId = actionRecord is ActionWithChapterRecord c ? c.ChapterId : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="Schema.ActionsContext.ActionRecord.Action" />
|
||||
/// </summary>
|
||||
[Required]
|
||||
public ActionsEnum Action { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="Schema.ActionsContext.ActionRecord.PerformedAt" />
|
||||
/// </summary>
|
||||
[Required]
|
||||
public DateTime PerformedAt { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// MangaId if Record is <see cref="ActionWithMangaRecord"/>
|
||||
/// </summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? MangaId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// ChapterId if Record is <see cref="ActionWithMangaRecord"/>
|
||||
/// </summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? ChapterId { get; init; }
|
||||
}
|
Reference in New Issue
Block a user