Added database handling of DeepL setting
This commit is contained in:
parent
da1bb941f0
commit
fa5d39318f
9
I18N Commander/DataModel/Database/SettingDeepL.cs
Normal file
9
I18N Commander/DataModel/Database/SettingDeepL.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace DataModel.Database;
|
||||||
|
|
||||||
|
public enum SettingDeepL
|
||||||
|
{
|
||||||
|
DISABLED,
|
||||||
|
|
||||||
|
USE_FREE_ACCOUNT,
|
||||||
|
USE_PRO_ACCOUNT,
|
||||||
|
}
|
6
I18N Commander/DataModel/Database/SettingNames.cs
Normal file
6
I18N Commander/DataModel/Database/SettingNames.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace DataModel.Database;
|
||||||
|
|
||||||
|
public static class SettingNames
|
||||||
|
{
|
||||||
|
public static readonly string DEEPL_MODE = "DeepL Mode";
|
||||||
|
}
|
60
I18N Commander/Processor/AppSettings.cs
Normal file
60
I18N Commander/Processor/AppSettings.cs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
using DataModel.Database;
|
||||||
|
using DataModel.Database.Common;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Processor;
|
||||||
|
|
||||||
|
public static class AppSettings
|
||||||
|
{
|
||||||
|
public static async Task SetDeepLMode(SettingDeepL mode)
|
||||||
|
{
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<SettingDeepL> GetDeepLMode()
|
||||||
|
{
|
||||||
|
// 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 (SettingDeepL) existingSetting.IntegerValue;
|
||||||
|
|
||||||
|
// Does not exist, so create it:
|
||||||
|
var setting = new Setting
|
||||||
|
{
|
||||||
|
Code = SettingNames.DEEPL_MODE,
|
||||||
|
IntegerValue = (int)SettingDeepL.DISABLED,
|
||||||
|
};
|
||||||
|
|
||||||
|
await db.Settings.AddAsync(setting);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
return (SettingDeepL) setting.IntegerValue;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user