From 55a8b09310af85197f9b90378d958b44b28990e2 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 21 Sep 2025 16:59:12 +0200 Subject: [PATCH] Add Controller --- .../Requests/PatchLibraryRefreshRecord.cs | 22 +++++++++++++++++++ API/Controllers/SettingsController.cs | 18 ++++++++++++++- .../LibraryConnectors/LibraryConnector.cs | 10 +++++++++ API/Schema/MangaContext/Manga.cs | 8 +++---- .../NotificationsContext/Notification.cs | 3 +++ API/TrangaSettings.cs | 5 +++-- API/Workers/BaseWorker.cs | 3 +++ API/Workers/RefreshLibrariesWorker.cs | 15 +++++++++++++ 8 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 API/Controllers/Requests/PatchLibraryRefreshRecord.cs diff --git a/API/Controllers/Requests/PatchLibraryRefreshRecord.cs b/API/Controllers/Requests/PatchLibraryRefreshRecord.cs new file mode 100644 index 0000000..d1fe149 --- /dev/null +++ b/API/Controllers/Requests/PatchLibraryRefreshRecord.cs @@ -0,0 +1,22 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using API.Workers; + +namespace API.Controllers.Requests; + +public record PatchLibraryRefreshRecord +{ + /// + /// When to refresh the Library + /// + [Required] + [Description("When to refresh the Library")] + public required LibraryRefreshSetting Setting { get; init; } + + /// + /// When is selected, update the time between refreshes + /// + [Required] + [Description("When WhileDownloadingis selected, update the time between refreshes")] + public required int? RefreshLibraryWhileDownloadingEveryMinutes { get; init; } +} \ No newline at end of file diff --git a/API/Controllers/SettingsController.cs b/API/Controllers/SettingsController.cs index c7c9d05..fce6d47 100644 --- a/API/Controllers/SettingsController.cs +++ b/API/Controllers/SettingsController.cs @@ -1,4 +1,5 @@ -using API.MangaDownloadClients; +using API.Controllers.Requests; +using API.MangaDownloadClients; using Asp.Versioning; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; @@ -290,4 +291,19 @@ public class SettingsController() : Controller Tranga.Settings.SetDownloadLanguage(Language); return TypedResults.Ok(); } + + + /// + /// Sets the time when Libraries are refreshed + /// + /// + [HttpPatch("LibraryRefresh")] + [ProducesResponseType(Status200OK)] + public Ok SetLibraryRefresh([FromBody]PatchLibraryRefreshRecord requestData) + { + Tranga.Settings.SetLibraryRefreshSetting(requestData.Setting); + if(requestData.RefreshLibraryWhileDownloadingEveryMinutes is { } value) + Tranga.Settings.SetRefreshLibraryWhileDownloadingEveryMinutes(value); + return TypedResults.Ok(); + } } \ No newline at end of file diff --git a/API/Schema/LibraryContext/LibraryConnectors/LibraryConnector.cs b/API/Schema/LibraryContext/LibraryConnectors/LibraryConnector.cs index f25e551..f332bfa 100644 --- a/API/Schema/LibraryContext/LibraryConnectors/LibraryConnector.cs +++ b/API/Schema/LibraryContext/LibraryConnectors/LibraryConnector.cs @@ -2,6 +2,8 @@ using System.ComponentModel.DataAnnotations.Schema; using log4net; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace API.Schema.LibraryContext.LibraryConnectors; @@ -40,8 +42,16 @@ public abstract class LibraryConnector : Identifiable internal abstract Task Test(CancellationToken ct); } +[JsonConverter(typeof(StringEnumConverter))] public enum LibraryType : byte { + /// + /// + /// Komga = 0, + + /// + /// + /// Kavita = 1 } \ No newline at end of file diff --git a/API/Schema/MangaContext/Manga.cs b/API/Schema/MangaContext/Manga.cs index 8a694de..753e569 100644 --- a/API/Schema/MangaContext/Manga.cs +++ b/API/Schema/MangaContext/Manga.cs @@ -1,13 +1,10 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Runtime.InteropServices; -using System.Text; using API.Workers; using Microsoft.EntityFrameworkCore; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Formats.Jpeg; -using SixLabors.ImageSharp.Processing; -using SixLabors.ImageSharp.Processing.Processors.Transforms; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using static System.IO.UnixFileMode; namespace API.Schema.MangaContext; @@ -138,6 +135,7 @@ public class Manga : Identifiable public override string ToString() => $"{base.ToString()} {Name}"; } +[JsonConverter(typeof(StringEnumConverter))] public enum MangaReleaseStatus : byte { Continuing = 0, diff --git a/API/Schema/NotificationsContext/Notification.cs b/API/Schema/NotificationsContext/Notification.cs index 0ea5a75..ae4e84e 100644 --- a/API/Schema/NotificationsContext/Notification.cs +++ b/API/Schema/NotificationsContext/Notification.cs @@ -1,5 +1,7 @@ using System.ComponentModel.DataAnnotations; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace API.Schema.NotificationsContext; @@ -48,6 +50,7 @@ public class Notification : Identifiable public override string ToString() => $"{base.ToString()} {Urgency} {Title} {Message}"; } +[JsonConverter(typeof(StringEnumConverter))] public enum NotificationUrgency : byte { Low = 1, diff --git a/API/TrangaSettings.cs b/API/TrangaSettings.cs index 81656f1..7b205e4 100644 --- a/API/TrangaSettings.cs +++ b/API/TrangaSettings.cs @@ -2,6 +2,7 @@ using API.MangaDownloadClients; using API.Workers; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace API; @@ -63,12 +64,12 @@ public struct TrangaSettings() { if (!File.Exists(SettingsFilePath)) new TrangaSettings().Save(); - return JsonConvert.DeserializeObject(File.ReadAllText(SettingsFilePath)); + return JsonConvert.DeserializeObject(File.ReadAllText(SettingsFilePath), new StringEnumConverter()); } public void Save() { - File.WriteAllText(SettingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented)); + File.WriteAllText(SettingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented, new StringEnumConverter())); } public void SetUserAgent(string value) diff --git a/API/Workers/BaseWorker.cs b/API/Workers/BaseWorker.cs index 58a2d5b..1208e84 100644 --- a/API/Workers/BaseWorker.cs +++ b/API/Workers/BaseWorker.cs @@ -1,5 +1,7 @@ using API.Schema; using log4net; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace API.Workers; @@ -116,6 +118,7 @@ public abstract class BaseWorker : Identifiable } } +[JsonConverter(typeof(StringEnumConverter))] public enum WorkerExecutionState { Failed = 0, diff --git a/API/Workers/RefreshLibrariesWorker.cs b/API/Workers/RefreshLibrariesWorker.cs index 1e67bc9..0167afb 100644 --- a/API/Workers/RefreshLibrariesWorker.cs +++ b/API/Workers/RefreshLibrariesWorker.cs @@ -1,6 +1,8 @@ using API.Schema.LibraryContext; using API.Schema.LibraryContext.LibraryConnectors; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace API.Workers; @@ -20,10 +22,23 @@ public class RefreshLibrariesWorker(IEnumerable? dependsOn = null) : } } +[JsonConverter(typeof(StringEnumConverter))] public enum LibraryRefreshSetting : byte { + /// + /// Refresh Libraries after all Manga are downloaded + /// AfterAllFinished = 0, + /// + /// Refresh Libraries after a Manga is downloaded + /// AfterMangaFinished = 1, + /// + /// Refresh Libraries after every download + /// AfterEveryChapter = 2, + /// + /// Refresh Libraries while downloading chapters, every x minutes + /// WhileDownloading = 3 } \ No newline at end of file