2022-07-26 17:07:10 +00:00
|
|
|
|
using DataModel.Database;
|
|
|
|
|
using DataModel.Database.Common;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Processor;
|
|
|
|
|
|
|
|
|
|
public static class AppSettings
|
|
|
|
|
{
|
2022-07-30 21:07:04 +00:00
|
|
|
|
#region DeepL Settings
|
|
|
|
|
|
2022-07-30 13:49:19 +00:00
|
|
|
|
#region DeepL Mode
|
|
|
|
|
|
2022-07-30 15:04:32 +00:00
|
|
|
|
private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED;
|
|
|
|
|
private static bool CACHE_DEEPL_MODE_IS_LOADED = false;
|
|
|
|
|
|
2022-07-30 14:09:58 +00:00
|
|
|
|
public static async Task SetDeepLMode(SettingDeepLMode mode)
|
2022-07-26 17:07:10 +00:00
|
|
|
|
{
|
|
|
|
|
// Convert the enum to its int value:
|
|
|
|
|
var intValue = (int)mode;
|
|
|
|
|
|
2022-07-30 15:04:32 +00:00
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_DEEPL_MODE = mode;
|
|
|
|
|
CACHE_DEEPL_MODE_IS_LOADED = true;
|
|
|
|
|
|
2022-07-26 17:07:10 +00:00
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 14:09:58 +00:00
|
|
|
|
public static async Task<SettingDeepLMode> GetDeepLMode()
|
2022-07-26 17:07:10 +00:00
|
|
|
|
{
|
2022-07-30 15:04:32 +00:00
|
|
|
|
if (CACHE_DEEPL_MODE_IS_LOADED)
|
|
|
|
|
return CACHE_DEEPL_MODE;
|
2022-07-26 17:07:10 +00:00
|
|
|
|
|
2022-10-03 15:53:49 +00:00
|
|
|
|
var mode = SettingDeepLMode.DISABLED;
|
|
|
|
|
try
|
2022-07-26 17:07:10 +00:00
|
|
|
|
{
|
2022-10-03 15:53:49 +00:00
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
mode = (SettingDeepLMode) existingSetting.IntegerValue;
|
|
|
|
|
return mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.DEEPL_MODE,
|
|
|
|
|
IntegerValue = (int) mode,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return mode;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CACHE_DEEPL_MODE = mode;
|
|
|
|
|
CACHE_DEEPL_MODE_IS_LOADED = true;
|
|
|
|
|
}
|
2022-07-26 17:07:10 +00:00
|
|
|
|
}
|
2022-07-30 13:49:19 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region DeepL API Key
|
|
|
|
|
|
2022-07-30 15:04:32 +00:00
|
|
|
|
private static string CACHE_DEEPL_API_KEY = string.Empty;
|
|
|
|
|
private static bool CACHE_DEEPL_API_KEY_IS_LOADED = false;
|
|
|
|
|
|
2022-07-30 13:49:19 +00:00
|
|
|
|
public static async Task SetDeepLAPIKey(string apiKey)
|
|
|
|
|
{
|
2022-07-30 15:04:32 +00:00
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_DEEPL_API_KEY = apiKey;
|
|
|
|
|
CACHE_DEEPL_API_KEY_IS_LOADED = true;
|
|
|
|
|
|
2022-07-30 13:49:19 +00:00
|
|
|
|
// 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()
|
|
|
|
|
{
|
2022-07-30 21:38:01 +00:00
|
|
|
|
// Check the cache:
|
|
|
|
|
if (CACHE_DEEPL_API_KEY_IS_LOADED)
|
|
|
|
|
return CACHE_DEEPL_API_KEY;
|
2022-10-03 15:53:49 +00:00
|
|
|
|
|
|
|
|
|
var key = string.Empty;
|
|
|
|
|
try
|
2022-07-30 13:49:19 +00:00
|
|
|
|
{
|
2022-10-03 15:53:49 +00:00
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
key = existingSetting.TextValue;
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.DEEPL_API_KEY,
|
|
|
|
|
TextValue = key,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CACHE_DEEPL_API_KEY = key;
|
|
|
|
|
CACHE_DEEPL_API_KEY_IS_LOADED = true;
|
|
|
|
|
}
|
2022-07-30 13:49:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-07-30 14:38:38 +00:00
|
|
|
|
|
|
|
|
|
#region DeepL Action
|
|
|
|
|
|
2022-07-30 15:04:32 +00:00
|
|
|
|
private static SettingDeepLAction CACHE_DEEPL_ACTION = SettingDeepLAction.MANUAL;
|
|
|
|
|
private static bool CACHE_DEEPL_ACTION_IS_LOADED = false;
|
|
|
|
|
|
2022-07-30 14:38:38 +00:00
|
|
|
|
public static async Task SetDeepLAction(SettingDeepLAction action)
|
|
|
|
|
{
|
|
|
|
|
// Convert the enum to its int value:
|
|
|
|
|
var intValue = (int)action;
|
|
|
|
|
|
2022-07-30 15:04:32 +00:00
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_DEEPL_ACTION = action;
|
|
|
|
|
CACHE_DEEPL_ACTION_IS_LOADED = true;
|
|
|
|
|
|
2022-07-30 14:38:38 +00:00
|
|
|
|
// 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()
|
|
|
|
|
{
|
2022-07-30 21:38:01 +00:00
|
|
|
|
// Check the cache:
|
|
|
|
|
if (CACHE_DEEPL_ACTION_IS_LOADED)
|
|
|
|
|
return CACHE_DEEPL_ACTION;
|
|
|
|
|
|
2022-10-03 15:53:49 +00:00
|
|
|
|
var action = SettingDeepLAction.MANUAL;
|
|
|
|
|
try
|
2022-07-30 14:38:38 +00:00
|
|
|
|
{
|
2022-10-03 15:53:49 +00:00
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
action = (SettingDeepLAction) existingSetting.IntegerValue;
|
|
|
|
|
return action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.DEEPL_ACTION,
|
|
|
|
|
IntegerValue = (int) action,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return action;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CACHE_DEEPL_ACTION = action;
|
|
|
|
|
CACHE_DEEPL_ACTION_IS_LOADED = true;
|
|
|
|
|
}
|
2022-07-30 14:38:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-07-30 21:07:04 +00:00
|
|
|
|
|
2022-08-01 19:04:47 +00:00
|
|
|
|
#region DeepL Source Culture
|
|
|
|
|
|
|
|
|
|
private static int CACHE_DEEPL_SOURCE_CULTURE = -1;
|
|
|
|
|
private static bool CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED = false;
|
|
|
|
|
|
2022-08-05 19:33:33 +00:00
|
|
|
|
public static async Task SetDeepLSourceCultureIndex(int cultureIndex)
|
2022-08-01 19:04:47 +00:00
|
|
|
|
{
|
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_DEEPL_SOURCE_CULTURE = cultureIndex;
|
|
|
|
|
CACHE_DEEPL_SOURCE_CULTURE_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_SOURCE_CULTURE) is {} existingSetting)
|
|
|
|
|
{
|
|
|
|
|
existingSetting.IntegerValue = cultureIndex;
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.DEEPL_SOURCE_CULTURE,
|
|
|
|
|
IntegerValue = cultureIndex,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<int> GetDeepLSourceCultureIndex()
|
|
|
|
|
{
|
|
|
|
|
// Check the cache:
|
|
|
|
|
if (CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED)
|
|
|
|
|
return CACHE_DEEPL_SOURCE_CULTURE;
|
|
|
|
|
|
2022-10-03 15:53:49 +00:00
|
|
|
|
var cultureIndex = -1;
|
|
|
|
|
try
|
2022-08-01 19:04:47 +00:00
|
|
|
|
{
|
2022-10-03 15:53:49 +00:00
|
|
|
|
// 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_SOURCE_CULTURE) is { } existingSetting)
|
|
|
|
|
{
|
|
|
|
|
cultureIndex = existingSetting.IntegerValue;
|
|
|
|
|
return cultureIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.DEEPL_SOURCE_CULTURE,
|
|
|
|
|
IntegerValue = cultureIndex,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return cultureIndex;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CACHE_DEEPL_SOURCE_CULTURE = cultureIndex;
|
|
|
|
|
CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED = true;
|
|
|
|
|
}
|
2022-08-01 19:04:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-07-30 21:07:04 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Translation Settings
|
|
|
|
|
|
2022-08-05 19:11:02 +00:00
|
|
|
|
#region List of culture indices
|
2022-07-30 21:17:57 +00:00
|
|
|
|
|
2022-08-02 18:41:55 +00:00
|
|
|
|
private static List<int> CACHE_CULTURES_INDICES = new();
|
2022-07-30 21:07:04 +00:00
|
|
|
|
|
2022-08-02 18:41:55 +00:00
|
|
|
|
public static async Task<List<int>> GetCultureIndices()
|
2022-07-30 21:07:04 +00:00
|
|
|
|
{
|
2022-07-30 21:17:57 +00:00
|
|
|
|
// When possible, use the cache:
|
2022-08-02 18:41:55 +00:00
|
|
|
|
if (CACHE_CULTURES_INDICES.Count > 0)
|
|
|
|
|
return CACHE_CULTURES_INDICES;
|
2022-07-30 21:17:57 +00:00
|
|
|
|
|
2022-07-30 21:07:04 +00:00
|
|
|
|
// Get the database:
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
|
|
|
|
|
// Count the number of cultures:
|
2022-08-02 18:41:55 +00:00
|
|
|
|
var list = await db.Settings.Where(n => n.Code.StartsWith(SettingNames.CULTURE)).OrderBy(n => n.IntegerValue).Select(n => n.IntegerValue).ToListAsync();
|
2022-07-30 21:07:04 +00:00
|
|
|
|
|
|
|
|
|
// We have at least one default culture:
|
2022-08-02 18:41:55 +00:00
|
|
|
|
if(list.Count == 0)
|
2022-07-30 21:17:57 +00:00
|
|
|
|
{
|
2022-08-02 18:41:55 +00:00
|
|
|
|
// Add the default culture, which is en-US:
|
|
|
|
|
await AppSettings.SetCultureCode(1, "en-US");
|
2022-07-30 21:17:57 +00:00
|
|
|
|
}
|
2022-08-02 18:41:55 +00:00
|
|
|
|
else
|
|
|
|
|
CACHE_CULTURES_INDICES = list;
|
|
|
|
|
|
|
|
|
|
return CACHE_CULTURES_INDICES;
|
2022-07-30 21:07:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Get a culture
|
|
|
|
|
|
2022-07-30 21:14:39 +00:00
|
|
|
|
// Cache the cultures:
|
|
|
|
|
private static readonly Dictionary<int, string> CACHE_CULTURES = new();
|
|
|
|
|
|
2022-07-30 21:07:04 +00:00
|
|
|
|
public static async Task<string> GetCultureCode(int index)
|
|
|
|
|
{
|
2022-07-30 21:14:39 +00:00
|
|
|
|
// Check the cache:
|
|
|
|
|
if (CACHE_CULTURES.TryGetValue(index, out var cultureCode))
|
|
|
|
|
return cultureCode;
|
|
|
|
|
|
2022-07-30 21:07:04 +00:00
|
|
|
|
// Get the database:
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
|
|
|
|
|
// Get the culture code:
|
2022-07-31 19:19:13 +00:00
|
|
|
|
var code = await db.Settings.FirstOrDefaultAsync(n => n.Code.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index);
|
2022-07-30 21:07:04 +00:00
|
|
|
|
|
2022-07-30 21:14:39 +00:00
|
|
|
|
// When the culture code is not set & index = 1, use the default culture en-US:
|
|
|
|
|
if(code is null && index == 1)
|
|
|
|
|
{
|
|
|
|
|
CACHE_CULTURES.Add(index, "en-US");
|
2022-07-30 21:07:04 +00:00
|
|
|
|
return "en-US";
|
2022-07-30 21:14:39 +00:00
|
|
|
|
}
|
2022-07-30 21:07:04 +00:00
|
|
|
|
|
2022-07-30 21:14:39 +00:00
|
|
|
|
// Update the cache & return the code:
|
|
|
|
|
var codeText = code?.TextValue ?? string.Empty;
|
|
|
|
|
CACHE_CULTURES[index] = codeText;
|
|
|
|
|
return codeText;
|
2022-07-30 21:07:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Set a culture
|
|
|
|
|
|
|
|
|
|
public static async Task SetCultureCode(int index, string code)
|
|
|
|
|
{
|
2022-07-30 21:14:39 +00:00
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_CULTURES[index] = code;
|
|
|
|
|
|
2022-07-30 21:07:04 +00:00
|
|
|
|
// Get the database:
|
|
|
|
|
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
|
|
|
|
|
|
|
|
|
// Check, if the setting is already set:
|
2022-07-31 19:19:13 +00:00
|
|
|
|
if (await db.Settings.FirstOrDefaultAsync(n => n.Code.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index) is {} existingSetting)
|
2022-07-30 21:07:04 +00:00
|
|
|
|
{
|
|
|
|
|
existingSetting.TextValue = code;
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
2022-07-31 19:19:13 +00:00
|
|
|
|
Code = $"{SettingNames.CULTURE} {index}",
|
2022-07-30 21:07:04 +00:00
|
|
|
|
IntegerValue = index,
|
|
|
|
|
TextValue = code,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
2022-07-30 21:17:57 +00:00
|
|
|
|
|
2022-08-02 18:41:55 +00:00
|
|
|
|
// Update the list of cultures indices:
|
|
|
|
|
CACHE_CULTURES_INDICES.Add(index);
|
2022-07-30 21:07:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-08-02 18:43:58 +00:00
|
|
|
|
#region Delete a culture
|
|
|
|
|
|
|
|
|
|
public static async Task DeleteCulture(int index)
|
|
|
|
|
{
|
|
|
|
|
// 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.StartsWith(SettingNames.CULTURE) && n.IntegerValue == index) is {} existingSetting)
|
|
|
|
|
{
|
|
|
|
|
db.Settings.Remove(existingSetting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the list of cultures indices:
|
|
|
|
|
CACHE_CULTURES_INDICES.Remove(index);
|
|
|
|
|
|
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_CULTURES.Remove(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-08-01 19:04:47 +00:00
|
|
|
|
#region Get a list of cultures
|
|
|
|
|
|
|
|
|
|
public readonly record struct CultureInfo(string Code, int Index);
|
|
|
|
|
|
|
|
|
|
public static async Task<List<CultureInfo>> GetCultureInfos()
|
|
|
|
|
{
|
|
|
|
|
// Get the number of cultures:
|
2022-08-02 18:41:55 +00:00
|
|
|
|
var cultureIndices = await AppSettings.GetCultureIndices();
|
2022-08-01 19:04:47 +00:00
|
|
|
|
|
|
|
|
|
// Get the culture codes:
|
|
|
|
|
var cultureInfos = new List<CultureInfo>();
|
2022-08-02 18:41:55 +00:00
|
|
|
|
foreach (var cultureIndex in cultureIndices)
|
2022-08-01 19:04:47 +00:00
|
|
|
|
cultureInfos.Add(new CultureInfo
|
|
|
|
|
{
|
2022-08-02 18:41:55 +00:00
|
|
|
|
Code = await AppSettings.GetCultureCode(cultureIndex),
|
|
|
|
|
Index = cultureIndex,
|
2022-08-01 19:04:47 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return cultureInfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-07-30 21:07:04 +00:00
|
|
|
|
#endregion
|
2022-10-03 16:13:07 +00:00
|
|
|
|
|
|
|
|
|
#region Generator Settings
|
|
|
|
|
|
|
|
|
|
#region Generator Mode
|
|
|
|
|
|
|
|
|
|
private static SettingGeneratorMode CACHE_GENERATOR_MODE = SettingGeneratorMode.MANUAL;
|
|
|
|
|
private static bool CACHE_GENERATOR_MODE_IS_LOADED = false;
|
|
|
|
|
|
|
|
|
|
public static async Task<SettingGeneratorMode> GetGeneratorMode()
|
|
|
|
|
{
|
|
|
|
|
// When possible, use the cache:
|
|
|
|
|
if (CACHE_GENERATOR_MODE_IS_LOADED)
|
|
|
|
|
return CACHE_GENERATOR_MODE;
|
|
|
|
|
|
|
|
|
|
var generatorMode = SettingGeneratorMode.MANUAL;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 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.GENERATOR_MODE) is { } existingSetting)
|
|
|
|
|
{
|
|
|
|
|
generatorMode = (SettingGeneratorMode) existingSetting.IntegerValue;
|
|
|
|
|
return generatorMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.GENERATOR_MODE,
|
|
|
|
|
IntegerValue = (int) generatorMode,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
return generatorMode;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CACHE_GENERATOR_MODE_IS_LOADED = true;
|
|
|
|
|
CACHE_GENERATOR_MODE = generatorMode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task SetGeneratorMode(SettingGeneratorMode mode)
|
|
|
|
|
{
|
|
|
|
|
// Update the cache:
|
|
|
|
|
CACHE_GENERATOR_MODE = mode;
|
|
|
|
|
CACHE_GENERATOR_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.GENERATOR_MODE) is { } existingSetting)
|
|
|
|
|
{
|
|
|
|
|
existingSetting.IntegerValue = (int) mode;
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.GENERATOR_MODE,
|
|
|
|
|
IntegerValue = (int) mode,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-07-26 17:07:10 +00:00
|
|
|
|
}
|