73 lines
3.1 KiB
C#
73 lines
3.1 KiB
C#
using HermesSocketLibrary.db;
|
|
using HermesSocketLibrary.Requests.Messages;
|
|
using HermesSocketServer.Store.Internal;
|
|
using HermesSocketServer.Validators;
|
|
|
|
namespace HermesSocketServer.Store
|
|
{
|
|
public class VoiceStateStore : ComplexAutoSavedStore<string, TTSVoiceState>
|
|
{
|
|
private readonly string _userId;
|
|
private readonly IStore<string, TTSVoice> _voices;
|
|
private readonly VoiceIdValidator _idValidator;
|
|
private readonly Database _database;
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
|
|
public VoiceStateStore(string userId, DatabaseTable table, IStore<string, TTSVoice> voices, Database database, Serilog.ILogger logger)
|
|
: base(table, database, logger)
|
|
{
|
|
_userId = userId;
|
|
_voices = voices;
|
|
_database = database;
|
|
_logger = logger;
|
|
|
|
_idValidator = new VoiceIdValidator();
|
|
}
|
|
|
|
public override async Task Load()
|
|
{
|
|
var data = new Dictionary<string, object>() { { "user", _userId } };
|
|
string sql = "SELECT \"ttsVoiceId\", state FROM \"TtsVoiceState\" WHERE \"userId\" = @user";
|
|
await _database.Execute(sql, data, (reader) =>
|
|
{
|
|
string id = reader.GetString(0);
|
|
_store.Add(id, new TTSVoiceState()
|
|
{
|
|
Id = id,
|
|
Enabled = reader.GetBoolean(1),
|
|
UserId = _userId,
|
|
});
|
|
});
|
|
_logger.Information($"Loaded {_store.Count} TTS voice states from database.");
|
|
}
|
|
|
|
protected override void OnInitialAdd(string key, TTSVoiceState value)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
|
|
_idValidator.Check(value.Id);
|
|
ArgumentNullException.ThrowIfNull(value, nameof(value));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(value.Id, nameof(value.Id));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
|
|
ArgumentNullException.ThrowIfNull(value.Enabled, nameof(value.Enabled));
|
|
|
|
if (_voices.Get(value.Id) == null)
|
|
throw new ArgumentException("The voice does not exist.");
|
|
}
|
|
|
|
protected override void OnInitialModify(string key, TTSVoiceState oldValue, TTSVoiceState newValue)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Id, nameof(newValue.Id));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
|
|
ArgumentNullException.ThrowIfNull(newValue.Enabled, nameof(newValue.Enabled));
|
|
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(oldValue.Id));
|
|
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
|
|
ArgumentOutOfRangeException.ThrowIfEqual(oldValue.Enabled, newValue.Enabled, nameof(oldValue.Enabled));
|
|
}
|
|
|
|
protected override void OnPostRemove(string key, TTSVoiceState value)
|
|
{
|
|
}
|
|
}
|
|
} |