Added DeepL API key setting

This commit is contained in:
tsommer committed 2022-07-30 15:59:09 +02:00
1 parent d97b5cd0c1
commit 87c12de066
3 files changed
+83 -2

No files matched your search

+56
View File
@@ -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
}