285 lines
8.8 KiB
C#
285 lines
8.8 KiB
C#
using DataModel.Database;
|
|
using DataModel.Database.Common;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Processor;
|
|
|
|
public static class AppSettings
|
|
{
|
|
#region DeepL Settings
|
|
|
|
#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
|
|
|
|
#endregion
|
|
|
|
#region Translation Settings
|
|
|
|
#region Number of cultures
|
|
|
|
public static async Task<int> GetNumberCultures()
|
|
{
|
|
// Get the database:
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
// Count the number of cultures:
|
|
var count = await db.Settings.CountAsync(n => n.Code == SettingNames.CULTURE);
|
|
|
|
// We have at least one default culture:
|
|
if(count == 0)
|
|
return 1;
|
|
|
|
return count;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Get a culture
|
|
|
|
public static async Task<string> GetCultureCode(int index)
|
|
{
|
|
// Get the database:
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
// Get the culture code:
|
|
var code = await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.CULTURE && n.IntegerValue == index);
|
|
|
|
// When the culture code is not set, fake the default culture as en-US:
|
|
if(code == null)
|
|
return "en-US";
|
|
|
|
// Return the code:
|
|
return code.TextValue;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Set a culture
|
|
|
|
public static async Task SetCultureCode(int index, string code)
|
|
{
|
|
// 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.CULTURE && n.IntegerValue == index) is {} existingSetting)
|
|
{
|
|
existingSetting.TextValue = code;
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
// Does not exist, so create it:
|
|
else
|
|
{
|
|
var setting = new Setting
|
|
{
|
|
Code = SettingNames.CULTURE,
|
|
IntegerValue = index,
|
|
TextValue = code,
|
|
};
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
} |