Actions use enum

This commit is contained in:
2025-10-16 19:13:44 +02:00
parent 53276e858b
commit 6561ba3bc3
16 changed files with 140 additions and 84 deletions

View File

@@ -1,4 +1,5 @@
using API.Schema.ActionsContext;
using API.Schema.ActionsContext.Actions;
using Asp.Versioning;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
@@ -13,19 +14,14 @@ namespace API.Controllers;
public class ActionsController(ActionsContext context) : Controller
{
/// <summary>
/// Returns the available Action Types (<see cref="ActionRecord.Action"/>) performed by Tranga
/// Returns the available Action Types (<see cref="ActionsEnum"/>)
/// </summary>
/// <response code="200">List of performed action-types</response>
/// <response code="500">Database error</response>
/// <response code="200">List of action-types</response>
[HttpGet("Types")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
public async Task<Results<Ok<List<string>>, InternalServerError>> GetAvailableActions()
[ProducesResponseType<ActionsEnum[]>(Status200OK, "application/json")]
public Ok<ActionsEnum[]> GetAvailableActions()
{
if (await context.Actions.Select(a => a.Action).Distinct().ToListAsync(HttpContext.RequestAborted) is not
{ } actions)
return TypedResults.InternalServerError();
return TypedResults.Ok(actions);
return TypedResults.Ok(Enum.GetValues<ActionsEnum>());
}
public sealed record Interval(DateTime Start, DateTime End);
@@ -35,7 +31,7 @@ public class ActionsController(ActionsContext context) : Controller
/// <response code="200">List of performed actions</response>
/// <response code="500">Database error</response>
[HttpPost("Interval")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType<List<ActionRecord>>(Status200OK, "application/json")]
[ProducesResponseType(Status500InternalServerError)]
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsInterval([FromBody]Interval interval)
{
@@ -46,14 +42,14 @@ public class ActionsController(ActionsContext context) : Controller
}
/// <summary>
/// Returns <see cref="ActionRecord"/> with type <paramref name="Type"/>
/// Returns <see cref="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(Status200OK)]
[ProducesResponseType<List<ActionRecord>>(Status200OK, "application/json")]
[ProducesResponseType(Status500InternalServerError)]
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsWithType(string Type)
public async Task<Results<Ok<List<ActionRecord>>, InternalServerError>> GetActionsWithType(ActionsEnum Type)
{
if (await context.Actions.Where(a => a.Action == Type)
.ToListAsync(HttpContext.RequestAborted) is not { } actions)

View File

@@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace API.Migrations.Actions
{
[DbContext(typeof(ActionsContext))]
[Migration("20251016005257_Actions")]
[Migration("20251016170924_Actions")]
partial class Actions
{
/// <inheritdoc />
@@ -31,10 +31,9 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("Action")
.IsRequired()
b.Property<int>("Action")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
.HasColumnType("integer");
b.Property<DateTime>("PerformedAt")
.HasColumnType("timestamp with time zone");
@@ -43,7 +42,7 @@ namespace API.Migrations.Actions
b.ToTable("Actions");
b.HasDiscriminator<string>("Action").HasValue("ActionRecord");
b.HasDiscriminator<int>("Action");
b.UseTphMappingStrategy();
});
@@ -57,7 +56,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Chapter.Downloaded");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.ChaptersRetrievedActionRecord", b =>
@@ -70,7 +69,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Manga.ChaptersRetrieved");
b.HasDiscriminator().HasValue(2);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.CoverDownloadedActionRecord", b =>
@@ -88,7 +87,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Manga.CoverDownloaded");
b.HasDiscriminator().HasValue(3);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.DataMovedActionRecord", b =>
@@ -105,7 +104,7 @@ namespace API.Migrations.Actions
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.HasDiscriminator().HasValue("Tranga.DataMoved");
b.HasDiscriminator().HasValue(4);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.LibraryMovedActionRecord", b =>
@@ -123,7 +122,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Manga.LibraryMoved");
b.HasDiscriminator().HasValue(5);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.MetadataUpdatedActionRecord", b =>
@@ -141,14 +140,14 @@ namespace API.Migrations.Actions
.HasMaxLength(1024)
.HasColumnType("character varying(1024)");
b.HasDiscriminator().HasValue("Manga.MetadataUpdated");
b.HasDiscriminator().HasValue(6);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.StartupActionRecord", b =>
{
b.HasBaseType("API.Schema.ActionsContext.ActionRecord");
b.HasDiscriminator().HasValue("Tranga.Started");
b.HasDiscriminator().HasValue(0);
});
#pragma warning restore 612, 618
}

View File

@@ -16,7 +16,7 @@ namespace API.Migrations.Actions
columns: table => new
{
Key = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
Action = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
Action = table.Column<int>(type: "integer", maxLength: 128, nullable: false),
PerformedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
ChapterId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
MangaId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),

View File

@@ -28,10 +28,9 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("Action")
.IsRequired()
b.Property<int>("Action")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
.HasColumnType("integer");
b.Property<DateTime>("PerformedAt")
.HasColumnType("timestamp with time zone");
@@ -40,7 +39,7 @@ namespace API.Migrations.Actions
b.ToTable("Actions");
b.HasDiscriminator<string>("Action").HasValue("ActionRecord");
b.HasDiscriminator<int>("Action");
b.UseTphMappingStrategy();
});
@@ -54,7 +53,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Chapter.Downloaded");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.ChaptersRetrievedActionRecord", b =>
@@ -67,7 +66,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Manga.ChaptersRetrieved");
b.HasDiscriminator().HasValue(2);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.CoverDownloadedActionRecord", b =>
@@ -85,7 +84,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Manga.CoverDownloaded");
b.HasDiscriminator().HasValue(3);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.DataMovedActionRecord", b =>
@@ -102,7 +101,7 @@ namespace API.Migrations.Actions
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.HasDiscriminator().HasValue("Tranga.DataMoved");
b.HasDiscriminator().HasValue(4);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.LibraryMovedActionRecord", b =>
@@ -120,7 +119,7 @@ namespace API.Migrations.Actions
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasDiscriminator().HasValue("Manga.LibraryMoved");
b.HasDiscriminator().HasValue(5);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.MetadataUpdatedActionRecord", b =>
@@ -138,14 +137,14 @@ namespace API.Migrations.Actions
.HasMaxLength(1024)
.HasColumnType("character varying(1024)");
b.HasDiscriminator().HasValue("Manga.MetadataUpdated");
b.HasDiscriminator().HasValue(6);
});
modelBuilder.Entity("API.Schema.ActionsContext.Actions.StartupActionRecord", b =>
{
b.HasBaseType("API.Schema.ActionsContext.ActionRecord");
b.HasDiscriminator().HasValue("Tranga.Started");
b.HasDiscriminator().HasValue(0);
});
#pragma warning restore 612, 618
}

View File

@@ -1,16 +1,17 @@
using System.ComponentModel.DataAnnotations;
using API.Schema.ActionsContext.Actions;
using Microsoft.EntityFrameworkCore;
namespace API.Schema.ActionsContext;
[PrimaryKey("Key")]
public abstract class ActionRecord(string action, DateTime performedAt) : Identifiable
public abstract class ActionRecord(ActionsEnum action, DateTime performedAt) : Identifiable
{
/// <summary>
/// Constant string that describes the performed Action
/// </summary>
[StringLength(128)]
public string Action { get; init; } = action;
public ActionsEnum Action { get; init; } = action;
/// <summary>
/// UTC Time when Action was performed

View File

@@ -0,0 +1,12 @@
namespace API.Schema.ActionsContext.Actions;
public enum ActionsEnum
{
Startup = 0,
ChapterDownloaded = 1,
ChaptersRetrieved = 2,
CoverDownloaded = 3,
DataMoved = 4,
LibraryMoved = 5,
MetadataUpdated = 6
}

View File

@@ -3,15 +3,13 @@ using API.Schema.MangaContext;
namespace API.Schema.ActionsContext.Actions;
public sealed class ChapterDownloadedActionRecord(string action, DateTime performedAt, string chapterId) : ActionRecord(action, performedAt)
public sealed class ChapterDownloadedActionRecord(ActionsEnum action, DateTime performedAt, string chapterId) : ActionRecord(action, performedAt)
{
public ChapterDownloadedActionRecord(Chapter chapter) : this(ChapterDownloadedAction, DateTime.UtcNow, chapter.Key) { }
public ChapterDownloadedActionRecord(Chapter chapter) : this(ActionsEnum.ChapterDownloaded, DateTime.UtcNow, chapter.Key) { }
/// <summary>
/// Chapter that was downloaded
/// </summary>
[StringLength(64)]
public string ChapterId { get; init; } = chapterId;
public const string ChapterDownloadedAction = "Chapter.Downloaded";
}

View File

@@ -3,10 +3,10 @@ using API.Schema.MangaContext;
namespace API.Schema.ActionsContext.Actions;
public sealed class ChaptersRetrievedActionRecord(string action, DateTime performedAt, string mangaId)
public sealed class ChaptersRetrievedActionRecord(ActionsEnum action, DateTime performedAt, string mangaId)
: ActionWithMangaRecord(action, performedAt, mangaId)
{
public ChaptersRetrievedActionRecord(Manga manga) : this(ChaptersRetrievedAction, DateTime.UtcNow, manga.Key) { }
public ChaptersRetrievedActionRecord(Manga manga) : this(ActionsEnum.ChaptersRetrieved, DateTime.UtcNow, manga.Key) { }
public const string ChaptersRetrievedAction = "Manga.ChaptersRetrieved";
}

View File

@@ -4,16 +4,14 @@ using API.Schema.MangaContext;
namespace API.Schema.ActionsContext.Actions;
public sealed class CoverDownloadedActionRecord(string action, DateTime performedAt, string mangaId, string filename)
public sealed class CoverDownloadedActionRecord(ActionsEnum action, DateTime performedAt, string mangaId, string filename)
: ActionWithMangaRecord(action, performedAt, mangaId)
{
public CoverDownloadedActionRecord(Manga manga, string filename) : this(CoverDownloadedAction, DateTime.UtcNow, manga.Key, filename) { }
public CoverDownloadedActionRecord(Manga manga, string filename) : this(ActionsEnum.CoverDownloaded, DateTime.UtcNow, manga.Key, filename) { }
/// <summary>
/// Filename on disk
/// </summary>
[StringLength(1024)]
public string Filename { get; init; } = filename;
public const string CoverDownloadedAction = "Manga.CoverDownloaded";
}

View File

@@ -2,9 +2,9 @@ using System.ComponentModel.DataAnnotations;
namespace API.Schema.ActionsContext.Actions;
public sealed class DataMovedActionRecord(string action, DateTime performedAt, string from, string to) : ActionRecord(action, performedAt)
public sealed class DataMovedActionRecord(ActionsEnum action, DateTime performedAt, string from, string to) : ActionRecord(action, performedAt)
{
public DataMovedActionRecord(string from, string to) : this(DataMovedAction, DateTime.UtcNow, from, to) { }
public DataMovedActionRecord(string from, string to) : this(ActionsEnum.DataMoved, DateTime.UtcNow, from, to) { }
/// <summary>
/// From path
@@ -17,6 +17,4 @@ public sealed class DataMovedActionRecord(string action, DateTime performedAt, s
/// </summary>
[StringLength(2048)]
public string To { get; init; } = to;
public const string DataMovedAction = "Tranga.DataMoved";
}

View File

@@ -3,9 +3,9 @@ using API.Schema.MangaContext;
namespace API.Schema.ActionsContext.Actions.Generic;
public abstract class ActionWithMangaRecord(string action, DateTime performedAt, string mangaId) : ActionRecord(action, performedAt)
public abstract class ActionWithMangaRecord(ActionsEnum action, DateTime performedAt, string mangaId) : ActionRecord(action, performedAt)
{
protected ActionWithMangaRecord(string action, DateTime performedAt, Manga manga) : this(action, performedAt, manga.Key) { }
protected ActionWithMangaRecord(ActionsEnum action, DateTime performedAt, Manga manga) : this(action, performedAt, manga.Key) { }
/// <summary>
/// <see cref="Schema.MangaContext.Manga"/> for which the cover was downloaded

View File

@@ -4,15 +4,13 @@ using API.Schema.MangaContext;
namespace API.Schema.ActionsContext.Actions;
public sealed class LibraryMovedActionRecord(string action, DateTime performedAt, string mangaId, string fileLibraryId) : ActionWithMangaRecord(action, performedAt, mangaId)
public sealed class LibraryMovedActionRecord(ActionsEnum action, DateTime performedAt, string mangaId, string fileLibraryId) : ActionWithMangaRecord(action, performedAt, mangaId)
{
public LibraryMovedActionRecord(Manga manga, FileLibrary library) : this(LibraryMovedAction, DateTime.UtcNow, manga.Key, library.Key) { }
public LibraryMovedActionRecord(Manga manga, FileLibrary library) : this(ActionsEnum.LibraryMoved, DateTime.UtcNow, manga.Key, library.Key) { }
/// <summary>
/// <see cref="Schema.MangaContext.FileLibrary"/> for which the cover was downloaded
/// </summary>
[StringLength(64)]
public string FileLibraryId { get; init; } = fileLibraryId;
public const string LibraryMovedAction = "Manga.LibraryMoved";
}

View File

@@ -5,16 +5,14 @@ using API.Schema.MangaContext.MetadataFetchers;
namespace API.Schema.ActionsContext.Actions;
public sealed class MetadataUpdatedActionRecord(string action, DateTime performedAt, string mangaId, string metadataFetcher)
public sealed class MetadataUpdatedActionRecord(ActionsEnum action, DateTime performedAt, string mangaId, string metadataFetcher)
: ActionWithMangaRecord(action, performedAt, mangaId)
{
public MetadataUpdatedActionRecord(Manga manga, MetadataFetcher fetcher) : this(MetadataUpdatedAction, DateTime.UtcNow, manga.Key, fetcher.Name) { }
public MetadataUpdatedActionRecord(Manga manga, MetadataFetcher fetcher) : this(ActionsEnum.MetadataUpdated, DateTime.UtcNow, manga.Key, fetcher.Name) { }
/// <summary>
/// Filename on disk
/// </summary>
[StringLength(1024)]
public string MetadataFetcher { get; init; } = metadataFetcher;
public const string MetadataUpdatedAction = "Manga.MetadataUpdated";
}

View File

@@ -1,8 +1,6 @@
namespace API.Schema.ActionsContext.Actions;
public sealed class StartupActionRecord(string action, DateTime performedAt) : ActionRecord(action, performedAt)
public sealed class StartupActionRecord(ActionsEnum action, DateTime performedAt) : ActionRecord(action, performedAt)
{
public StartupActionRecord() : this(StartupAction, DateTime.UtcNow) { }
public const string StartupAction = "Tranga.Started";
public StartupActionRecord() : this(ActionsEnum.Startup, DateTime.UtcNow) { }
}

View File

@@ -11,12 +11,12 @@ public class ActionsContext(DbContextOptions<ActionsContext> options) : TrangaBa
{
modelBuilder.Entity<ActionRecord>()
.HasDiscriminator(a => a.Action)
.HasValue<ChapterDownloadedActionRecord>(ChapterDownloadedActionRecord.ChapterDownloadedAction)
.HasValue<CoverDownloadedActionRecord>(CoverDownloadedActionRecord.CoverDownloadedAction)
.HasValue<ChaptersRetrievedActionRecord>(ChaptersRetrievedActionRecord.ChaptersRetrievedAction)
.HasValue<MetadataUpdatedActionRecord>(MetadataUpdatedActionRecord.MetadataUpdatedAction)
.HasValue<DataMovedActionRecord>(DataMovedActionRecord.DataMovedAction)
.HasValue<LibraryMovedActionRecord>(LibraryMovedActionRecord.LibraryMovedAction)
.HasValue<StartupActionRecord>(StartupActionRecord.StartupAction);
.HasValue<ChapterDownloadedActionRecord>(ActionsEnum.ChapterDownloaded)
.HasValue<CoverDownloadedActionRecord>(ActionsEnum.CoverDownloaded)
.HasValue<ChaptersRetrievedActionRecord>(ActionsEnum.ChaptersRetrieved)
.HasValue<MetadataUpdatedActionRecord>(ActionsEnum.MetadataUpdated)
.HasValue<DataMovedActionRecord>(ActionsEnum.DataMoved)
.HasValue<LibraryMovedActionRecord>(ActionsEnum.LibraryMoved)
.HasValue<StartupActionRecord>(ActionsEnum.Startup);
}
}

View File

@@ -10,13 +10,20 @@
"tags": [
"Actions"
],
"summary": "Returns the available Action Types (API.Schema.ActionsContext.ActionRecord.Action) performed by Tranga",
"summary": "Returns the available Action Types (API.Schema.ActionsContext.Actions.ActionsEnum)",
"responses": {
"200": {
"description": "List of performed action-types"
},
"500": {
"description": "Database error"
"description": "List of action-types",
"content": {
"application/json; x-version=2.0": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ActionsEnum"
}
}
}
}
}
}
}
@@ -53,7 +60,17 @@
},
"responses": {
"200": {
"description": "List of performed actions"
"description": "List of performed actions",
"content": {
"application/json; x-version=2.0": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ActionRecord"
}
}
}
}
},
"500": {
"description": "Database error"
@@ -66,20 +83,30 @@
"tags": [
"Actions"
],
"summary": "Returns API.Schema.ActionsContext.ActionRecord with type Type",
"summary": "Returns API.Schema.ActionsContext.ActionRecord with TypeAPI.Schema.ActionsContext.Actions.ActionsEnum",
"parameters": [
{
"name": "Type",
"in": "path",
"required": true,
"schema": {
"type": "string"
"$ref": "#/components/schemas/ActionsEnum"
}
}
],
"responses": {
"200": {
"description": "List of performed actions"
"description": "List of performed actions",
"content": {
"application/json; x-version=2.0": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ActionRecord"
}
}
}
}
},
"500": {
"description": "Database error"
@@ -3160,6 +3187,40 @@
},
"components": {
"schemas": {
"ActionRecord": {
"required": [
"key"
],
"type": "object",
"properties": {
"action": {
"$ref": "#/components/schemas/ActionsEnum"
},
"performedAt": {
"type": "string",
"description": "UTC Time when Action was performed",
"format": "date-time"
},
"key": {
"maxLength": 64,
"minLength": 16,
"type": "string"
}
},
"additionalProperties": false
},
"ActionsEnum": {
"enum": [
"Startup",
"ChapterDownloaded",
"ChaptersRetrieved",
"CoverDownloaded",
"DataMoved",
"LibraryMoved",
"MetadataUpdated"
],
"type": "string"
},
"AltTitle": {
"required": [
"language",