diff --git a/I18N Commander/DataModel/Database/SettingDeepL.cs b/I18N Commander/DataModel/Database/SettingDeepL.cs new file mode 100644 index 0000000..7101b11 --- /dev/null +++ b/I18N Commander/DataModel/Database/SettingDeepL.cs @@ -0,0 +1,9 @@ +namespace DataModel.Database; + +public enum SettingDeepL +{ + DISABLED, + + USE_FREE_ACCOUNT, + USE_PRO_ACCOUNT, +} \ No newline at end of file diff --git a/I18N Commander/DataModel/Database/SettingNames.cs b/I18N Commander/DataModel/Database/SettingNames.cs new file mode 100644 index 0000000..b9fe29f --- /dev/null +++ b/I18N Commander/DataModel/Database/SettingNames.cs @@ -0,0 +1,6 @@ +namespace DataModel.Database; + +public static class SettingNames +{ + public static readonly string DEEPL_MODE = "DeepL Mode"; +} \ No newline at end of file diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs new file mode 100644 index 0000000..ae6b7e9 --- /dev/null +++ b/I18N Commander/Processor/AppSettings.cs @@ -0,0 +1,60 @@ +using DataModel.Database; +using DataModel.Database.Common; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace Processor; + +public static class AppSettings +{ + public static async Task SetDeepLMode(SettingDeepL mode) + { + // Convert the enum to its int value: + var intValue = (int)mode; + + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_MODE) is {} existingSetting) + { + existingSetting.IntegerValue = intValue; + await db.SaveChangesAsync(); + } + + // Does not exist, so create it: + else + { + var setting = new Setting + { + Code = SettingNames.DEEPL_MODE, + IntegerValue = intValue, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + } + } + + public static async Task GetDeepLMode() + { + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_MODE) is { } existingSetting) + return (SettingDeepL) existingSetting.IntegerValue; + + // Does not exist, so create it: + var setting = new Setting + { + Code = SettingNames.DEEPL_MODE, + IntegerValue = (int)SettingDeepL.DISABLED, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + + return (SettingDeepL) setting.IntegerValue; + } +} \ No newline at end of file