2024-10-29 08:13:38 -04:00
|
|
|
using System.Collections.Immutable;
|
2024-10-18 21:50:46 -04:00
|
|
|
using HermesSocketLibrary.db;
|
|
|
|
using HermesSocketServer.Models;
|
|
|
|
|
|
|
|
namespace HermesSocketServer.Store
|
|
|
|
{
|
|
|
|
public class UserStore : GroupSaveStore<string, User>
|
|
|
|
{
|
|
|
|
private readonly Database _database;
|
|
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
private readonly GroupSaveSqlGenerator<User> _generator;
|
|
|
|
|
|
|
|
|
|
|
|
public UserStore(Database database, Serilog.ILogger logger) : base(logger)
|
|
|
|
{
|
|
|
|
_database = database;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
var ctp = new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{ "id", "Id" },
|
|
|
|
{ "name", "Name" },
|
|
|
|
{ "email", "Email" },
|
|
|
|
{ "role", "Role" },
|
|
|
|
{ "ttsDefaultVoice", "DefaultVoice" }
|
|
|
|
};
|
2024-10-29 08:13:38 -04:00
|
|
|
_generator = new GroupSaveSqlGenerator<User>(ctp, _logger);
|
2024-10-18 21:50:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task Load()
|
|
|
|
{
|
|
|
|
string sql = "SELECT id, name, email, role, \"ttsDefaultVoice\" FROM \"User\";";
|
|
|
|
await _database.Execute(sql, new Dictionary<string, object>(), (reader) =>
|
|
|
|
{
|
|
|
|
string id = reader.GetString(0);
|
|
|
|
_store.Add(id, new User()
|
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
Name = reader.GetString(1),
|
|
|
|
Email = reader.GetString(2),
|
|
|
|
Role = reader.GetString(3),
|
|
|
|
DefaultVoice = reader.GetString(4),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
_logger.Information($"Loaded {_store.Count} users from database.");
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:07:18 -04:00
|
|
|
protected override void OnInitialAdd(string key, User value)
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:07:18 -04:00
|
|
|
protected override void OnInitialModify(string key, User value)
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:07:18 -04:00
|
|
|
protected override void OnInitialRemove(string key)
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-10-29 08:13:38 -04:00
|
|
|
public override async Task Save()
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
2024-10-20 15:28:16 -04:00
|
|
|
int count = 0;
|
|
|
|
string sql = string.Empty;
|
2024-10-29 08:13:38 -04:00
|
|
|
ImmutableList<string>? list = null;
|
2024-10-20 15:28:16 -04:00
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
if (_added.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2024-10-29 08:13:38 -04:00
|
|
|
list = _added.ToImmutableList();
|
2024-10-18 21:50:46 -04:00
|
|
|
_added.Clear();
|
|
|
|
}
|
2024-10-29 08:13:38 -04:00
|
|
|
count = list.Count;
|
|
|
|
sql = _generator.GeneratePreparedInsertSql("User", count, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
|
|
|
|
2024-10-20 15:28:16 -04:00
|
|
|
_logger.Debug($"User - Adding {count} rows to database: {sql}");
|
2024-10-29 08:13:38 -04:00
|
|
|
var values = list.Select(id => _store[id]).Where(v => v != null);
|
|
|
|
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
2024-10-18 21:50:46 -04:00
|
|
|
}
|
|
|
|
if (_modified.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2024-10-29 08:13:38 -04:00
|
|
|
list = _modified.ToImmutableList();
|
2024-10-18 21:50:46 -04:00
|
|
|
_modified.Clear();
|
|
|
|
}
|
2024-10-29 08:13:38 -04:00
|
|
|
count = list.Count;
|
|
|
|
sql = _generator.GeneratePreparedUpdateSql("User", count, ["id"], ["name", "email", "role", "ttsDefaultVoice"]);
|
|
|
|
|
2024-10-20 15:28:16 -04:00
|
|
|
_logger.Debug($"User - Modifying {count} rows in database: {sql}");
|
2024-10-29 08:13:38 -04:00
|
|
|
var values = list.Select(id => _store[id]).Where(v => v != null);
|
|
|
|
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
2024-10-18 21:50:46 -04:00
|
|
|
}
|
|
|
|
if (_deleted.Any())
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2024-10-29 08:13:38 -04:00
|
|
|
list = _deleted.ToImmutableList();
|
2024-10-18 21:50:46 -04:00
|
|
|
_deleted.Clear();
|
|
|
|
}
|
2024-10-29 08:13:38 -04:00
|
|
|
count = list.Count;
|
|
|
|
sql = _generator.GeneratePreparedDeleteSql("User", count, ["id"]);
|
|
|
|
|
2024-10-20 15:28:16 -04:00
|
|
|
_logger.Debug($"User - Deleting {count} rows from database: {sql}");
|
2024-10-31 11:42:43 -04:00
|
|
|
await _generator.DoPreparedStatementRaw(_database, sql, list, ["id"]);
|
2024-10-18 21:50:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|