hermes-server/Store/VoiceStore.cs

111 lines
3.9 KiB
C#

using System.Collections.Immutable;
using HermesSocketLibrary.db;
using HermesSocketServer.Models;
using HermesSocketServer.Validators;
namespace HermesSocketServer.Store
{
public class VoiceStore : GroupSaveStore<string, Voice>
{
private readonly VoiceIdValidator _idValidator;
private readonly VoiceNameValidator _nameValidator;
private readonly Database _database;
private readonly Serilog.ILogger _logger;
private readonly GroupSaveSqlGenerator<Voice> _generator;
public VoiceStore(VoiceIdValidator voiceIdValidator, VoiceNameValidator voiceNameValidator, Database database, Serilog.ILogger logger) : base(logger)
{
_idValidator = voiceIdValidator;
_nameValidator = voiceNameValidator;
_database = database;
_logger = logger;
var ctp = new Dictionary<string, string>
{
{ "id", "Id" },
{ "name", "Name" }
};
_generator = new GroupSaveSqlGenerator<Voice>(ctp, _logger);
}
public override async Task Load()
{
string sql = "SELECT id, name FROM \"TtsVoice\";";
await _database.Execute(sql, new Dictionary<string, object>(), (reader) =>
{
string id = reader.GetString(0);
_store.Add(id, new Voice()
{
Id = id,
Name = reader.GetString(1),
});
});
_logger.Information($"Loaded {_store.Count} TTS voices from database.");
}
protected override void OnInitialAdd(string key, Voice value)
{
_idValidator.Check(value.Id);
_nameValidator.Check(value.Name);
}
protected override void OnInitialModify(string key, Voice value)
{
_nameValidator.Check(value.Name);
}
protected override void OnInitialRemove(string key)
{
}
public override async Task Save()
{
int count = 0;
string sql = string.Empty;
ImmutableList<string>? list = null;
if (_added.Any())
{
lock (_lock)
{
list = _added.ToImmutableList();
_added.Clear();
}
count = list.Count;
sql = _generator.GeneratePreparedInsertSql("TtsVoice", count, ["id", "name"]);
_logger.Debug($"User - Adding {count} rows to database: {sql}");
var values = list.Select(id => _store[id]).Where(v => v != null);
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
}
if (_modified.Any())
{
lock (_lock)
{
list = _modified.ToImmutableList();
_modified.Clear();
}
count = list.Count;
sql = _generator.GeneratePreparedUpdateSql("TtsVoice", count, ["id"], ["name"]);
_logger.Debug($"User - Modifying {count} rows in database: {sql}");
var values = list.Select(id => _store[id]).Where(v => v != null);
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
}
if (_deleted.Any())
{
lock (_lock)
{
list = _deleted.ToImmutableList();
_deleted.Clear();
}
count = list.Count;
sql = _generator.GeneratePreparedDeleteSql("TtsVoice", count, ["id"]);
_logger.Debug($"User - Deleting {count} rows from database: {sql}");
await _generator.DoPreparedStatementRaw(_database, sql, list, ["id"]);
}
}
}
}