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 13:49:19 +00:00
|
|
|
|
#region DeepL Mode
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
|
// 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)
|
2022-07-30 14:09:58 +00:00
|
|
|
|
return (SettingDeepLMode) existingSetting.IntegerValue;
|
2022-07-26 17:07:10 +00:00
|
|
|
|
|
|
|
|
|
// Does not exist, so create it:
|
|
|
|
|
var setting = new Setting
|
|
|
|
|
{
|
|
|
|
|
Code = SettingNames.DEEPL_MODE,
|
2022-07-30 14:09:58 +00:00
|
|
|
|
IntegerValue = (int)SettingDeepLMode.DISABLED,
|
2022-07-26 17:07:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.Settings.AddAsync(setting);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
2022-07-30 14:09:58 +00:00
|
|
|
|
return (SettingDeepLMode) setting.IntegerValue;
|
2022-07-26 17:07:10 +00:00
|
|
|
|
}
|
2022-07-30 13:49:19 +00:00
|
|
|
|
|
|
|
|
|
#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
|
2022-07-30 14:38:38 +00:00
|
|
|
|
|
|
|
|
|
#region DeepL Action
|
|
|
|
|
|
|
|
|
|
public static async Task SetDeepLAction(SettingDeepLAction action)
|
|
|
|
|
{
|
|
|
|
|
// Convert the enum to its int value:
|
|
|
|
|
var intValue = (int)action;
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
return (SettingDeepLAction) setting.IntegerValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-07-26 17:07:10 +00:00
|
|
|
|
}
|