2024-10-17 23:21:16 -04:00
|
|
|
using HermesSocketLibrary.db;
|
2024-10-18 21:50:46 -04:00
|
|
|
using HermesSocketServer.Models;
|
2024-10-17 23:21:16 -04:00
|
|
|
|
|
|
|
namespace HermesSocketServer.Store
|
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
public class ChatterStore : GroupSaveStore<string, ChatterVoice>
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
private readonly string _userId;
|
2024-10-17 23:21:16 -04:00
|
|
|
private readonly Database _database;
|
|
|
|
private readonly Serilog.ILogger _logger;
|
2024-10-18 21:50:46 -04:00
|
|
|
private readonly GroupSaveSqlGenerator<ChatterVoice> _generator;
|
2024-10-17 23:21:16 -04:00
|
|
|
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
public ChatterStore(string userId, Database database, Serilog.ILogger logger) : base(logger)
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
_userId = userId;
|
2024-10-17 23:21:16 -04:00
|
|
|
_database = database;
|
|
|
|
_logger = logger;
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
var ctp = new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{ "chatterId", "ChatterId" },
|
|
|
|
{ "ttsVoiceId", "VoiceId" },
|
|
|
|
{ "userId", "UserId" },
|
|
|
|
};
|
|
|
|
_generator = new GroupSaveSqlGenerator<ChatterVoice>(ctp);
|
2024-10-17 23:21:16 -04:00
|
|
|
}
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
public override async Task Load()
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
var data = new Dictionary<string, object>() { { "user", _userId } };
|
|
|
|
string sql = $"SELECT \"chatterId\", \"ttsVoiceId\" FROM \"TtsChatVoice\" WHERE \"userId\" = @user";
|
|
|
|
await _database.Execute(sql, data, (reader) =>
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
string chatterId = reader.GetInt64(0).ToString();
|
|
|
|
_store.Add(chatterId, new ChatterVoice()
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
UserId = _userId,
|
|
|
|
ChatterId = chatterId,
|
|
|
|
VoiceId = reader.GetString(1)
|
|
|
|
});
|
2024-10-17 23:21:16 -04:00
|
|
|
});
|
2024-10-18 21:50:46 -04:00
|
|
|
_logger.Information($"Loaded {_store.Count} TTS chatter voices from database.");
|
2024-10-17 23:21:16 -04:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:07:18 -04:00
|
|
|
protected override void OnInitialAdd(string key, ChatterVoice value)
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
}
|
2024-10-17 23:21:16 -04:00
|
|
|
|
2024-10-20 14:07:18 -04:00
|
|
|
protected override void OnInitialModify(string key, ChatterVoice value)
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
2024-10-17 23:21:16 -04:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:07:18 -04:00
|
|
|
protected override void OnInitialRemove(string key)
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
public override async Task<bool> Save()
|
2024-10-17 23:21:16 -04:00
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
int count = 0;
|
|
|
|
string sql = string.Empty;
|
2024-10-17 23:21:16 -04:00
|
|
|
|
|
|
|
if (_added.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
count = _added.Count;
|
|
|
|
sql = _generator.GenerateInsertSql("TtsChatVoice", _added.Select(a => _store[a]), ["userId", "chatterId", "ttsVoiceId"]);
|
2024-10-17 23:21:16 -04:00
|
|
|
_added.Clear();
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
_logger.Debug($"ADD {count} " + sql);
|
|
|
|
await _database.ExecuteScalar(sql);
|
2024-10-17 23:21:16 -04:00
|
|
|
}
|
|
|
|
if (_modified.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
count = _modified.Count;
|
|
|
|
sql = _generator.GenerateUpdateSql("TtsChatVoice", _modified.Select(m => _store[m]), ["userId", "chatterId"], ["ttsVoiceId"]);
|
2024-10-17 23:21:16 -04:00
|
|
|
_modified.Clear();
|
|
|
|
}
|
2024-10-18 21:50:46 -04:00
|
|
|
_logger.Debug($"MOD {count} " + sql);
|
|
|
|
await _database.ExecuteScalar(sql);
|
2024-10-17 23:21:16 -04:00
|
|
|
}
|
|
|
|
if (_deleted.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2024-10-18 21:50:46 -04:00
|
|
|
count = _deleted.Count;
|
|
|
|
sql = _generator.GenerateDeleteSql("TtsChatVoice", _deleted, ["userId", "chatterId"]);
|
2024-10-17 23:21:16 -04:00
|
|
|
_deleted.Clear();
|
|
|
|
}
|
2024-10-18 21:50:46 -04:00
|
|
|
_logger.Debug($"DEL {count} " + sql);
|
|
|
|
await _database.ExecuteScalar(sql);
|
2024-10-17 23:21:16 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|