Added DeepL API key setting
This commit is contained in:
3 files changed
+83
-2
No files matched your search
@@ -7,6 +7,8 @@ namespace Processor;
|
||||
|
||||
public static class AppSettings
|
||||
{
|
||||
#region DeepL Mode
|
||||
|
||||
public static async Task SetDeepLMode(SettingDeepL mode)
|
||||
{
|
||||
// Convert the enum to its int value:
|
||||
@@ -57,4 +59,58 @@ public static class AppSettings
|
||||
|
||||
return (SettingDeepL) setting.IntegerValue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DeepL API Key
|
||||
|
||||
public static async Task SetDeepLAPIKey(string apiKey)
|
||||
{
|
||||
// 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();
|
||||
|
||||
return setting.TextValue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user