hermes-server/Store/VoiceStore.cs

111 lines
3.9 KiB
C#
Raw Normal View History

using System.Collections.Immutable;
2024-10-17 15:06:22 -04:00
using HermesSocketLibrary.db;
using HermesSocketServer.Models;
2024-10-17 15:06:22 -04:00
using HermesSocketServer.Validators;
namespace HermesSocketServer.Store
{
public class VoiceStore : GroupSaveStore<string, Voice>
2024-10-17 15:06:22 -04:00
{
private readonly VoiceIdValidator _idValidator;
private readonly VoiceNameValidator _nameValidator;
2024-10-17 15:06:22 -04:00
private readonly Database _database;
private readonly Serilog.ILogger _logger;
private readonly GroupSaveSqlGenerator<Voice> _generator;
2024-10-17 15:06:22 -04:00
public VoiceStore(VoiceIdValidator voiceIdValidator, VoiceNameValidator voiceNameValidator, Database database, Serilog.ILogger logger) : base(logger)
2024-10-17 15:06:22 -04:00
{
_idValidator = voiceIdValidator;
_nameValidator = voiceNameValidator;
2024-10-17 15:06:22 -04:00
_database = database;
_logger = logger;
var ctp = new Dictionary<string, string>
{
{ "id", "Id" },
{ "name", "Name" }
};
_generator = new GroupSaveSqlGenerator<Voice>(ctp, _logger);
2024-10-17 15:06:22 -04:00
}
public override async Task Load()
2024-10-17 15:06:22 -04:00
{
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.");
2024-10-17 15:06:22 -04:00
}
protected override void OnInitialAdd(string key, Voice value)
2024-10-17 15:06:22 -04:00
{
_idValidator.Check(value.Id);
_nameValidator.Check(value.Name);
2024-10-17 15:06:22 -04:00
}
protected override void OnInitialModify(string key, Voice value)
2024-10-17 15:06:22 -04:00
{
_nameValidator.Check(value.Name);
2024-10-17 15:06:22 -04:00
}
protected override void OnInitialRemove(string key)
2024-10-17 15:06:22 -04:00
{
}
public override async Task Save()
2024-10-17 15:06:22 -04:00
{
int count = 0;
string sql = string.Empty;
ImmutableList<string>? list = null;
2024-10-17 15:06:22 -04:00
if (_added.Any())
{
lock (_lock)
{
list = _added.ToImmutableList();
2024-10-17 15:06:22 -04:00
_added.Clear();
}
count = list.Count;
sql = _generator.GeneratePreparedInsertSql("TtsVoice", count, ["id", "name"]);
2024-10-17 15:06:22 -04:00
_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"]);
2024-10-17 15:06:22 -04:00
}
if (_modified.Any())
{
lock (_lock)
{
list = _modified.ToImmutableList();
2024-10-17 15:06:22 -04:00
_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"]);
2024-10-17 15:06:22 -04:00
}
if (_deleted.Any())
{
lock (_lock)
{
list = _deleted.ToImmutableList();
2024-10-17 15:06:22 -04:00
_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"]);
2024-10-17 15:06:22 -04:00
}
}
}
}