I18NCommander/I18N Commander/Processor/AppSettings.cs

207 lines
6.6 KiB
C#

using DataModel.Database;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Processor;
public static class AppSettings
{
#region DeepL Mode
private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED;
private static bool CACHE_DEEPL_MODE_IS_LOADED = false;
public static async Task SetDeepLMode(SettingDeepLMode mode)
{
// Convert the enum to its int value:
var intValue = (int)mode;
// Update the cache:
CACHE_DEEPL_MODE = mode;
CACHE_DEEPL_MODE_IS_LOADED = true;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// 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<SettingDeepLMode> GetDeepLMode()
{
if (CACHE_DEEPL_MODE_IS_LOADED)
return CACHE_DEEPL_MODE;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_MODE) is { } existingSetting)
return (SettingDeepLMode) existingSetting.IntegerValue;
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.DEEPL_MODE,
IntegerValue = (int)SettingDeepLMode.DISABLED,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
var mode = (SettingDeepLMode) setting.IntegerValue;
CACHE_DEEPL_MODE = mode;
CACHE_DEEPL_MODE_IS_LOADED = true;
return mode;
}
#endregion
#region DeepL API Key
private static string CACHE_DEEPL_API_KEY = string.Empty;
private static bool CACHE_DEEPL_API_KEY_IS_LOADED = false;
public static async Task SetDeepLAPIKey(string apiKey)
{
// Update the cache:
CACHE_DEEPL_API_KEY = apiKey;
CACHE_DEEPL_API_KEY_IS_LOADED = true;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_API_KEY) is {} existingSetting)
{
existingSetting.TextValue = apiKey;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.DEEPL_API_KEY,
TextValue = apiKey,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
public static async Task<string> GetDeepLAPIKey()
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_API_KEY) is { } existingSetting)
return existingSetting.TextValue;
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.DEEPL_API_KEY,
TextValue = string.Empty,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
var key = setting.TextValue;
CACHE_DEEPL_API_KEY = key;
CACHE_DEEPL_API_KEY_IS_LOADED = true;
return key;
}
#endregion
#region DeepL Action
private static SettingDeepLAction CACHE_DEEPL_ACTION = SettingDeepLAction.MANUAL;
private static bool CACHE_DEEPL_ACTION_IS_LOADED = false;
public static async Task SetDeepLAction(SettingDeepLAction action)
{
// Convert the enum to its int value:
var intValue = (int)action;
// Update the cache:
CACHE_DEEPL_ACTION = action;
CACHE_DEEPL_ACTION_IS_LOADED = true;
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_ACTION) is {} existingSetting)
{
existingSetting.IntegerValue = intValue;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.DEEPL_ACTION,
IntegerValue = intValue,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
public static async Task<SettingDeepLAction> GetDeepLAction()
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Check, if the setting is already set:
if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_ACTION) is { } existingSetting)
return (SettingDeepLAction) existingSetting.IntegerValue;
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.DEEPL_ACTION,
IntegerValue = (int)SettingDeepLAction.MANUAL,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
var action = (SettingDeepLAction) setting.IntegerValue;
CACHE_DEEPL_ACTION = action;
CACHE_DEEPL_ACTION_IS_LOADED = true;
return action;
}
#endregion
}