Files
hermes-server/Store/ChatterGroupStore.cs

66 lines
2.9 KiB
C#

using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Store.Internal;
namespace HermesSocketServer.Store
{
public class ChatterGroupStore : ComplexAutoSavedStore<string, GroupChatter>
{
private readonly string _userId;
private readonly string _groupId;
private readonly Database _database;
private readonly Serilog.ILogger _logger;
public string GroupId { get => _groupId; }
public ChatterGroupStore(string userId, string groupId, DatabaseTable table, Database database, Serilog.ILogger logger)
: base(table, database, logger)
{
_userId = userId;
_groupId = groupId;
_database = database;
_logger = logger;
}
public override async Task Load()
{
var data = new Dictionary<string, object>() { { "user", _userId }, { "group", new Guid(_groupId) } };
string sql = $"SELECT \"chatterId\", \"chatterLabel\" FROM \"ChatterGroup\" WHERE \"userId\" = @user AND \"groupId\" = @group";
await _database.Execute(sql, data, (reader) =>
{
var chatterId = reader.GetInt32(0).ToString();
lock (_lock)
{
_store.Add(chatterId, new GroupChatter()
{
UserId = _userId,
GroupId = _groupId,
ChatterId = reader.GetInt32(0),
ChatterLabel = reader.GetString(1),
});
}
});
_logger.Information($"Loaded {_store.Count} group chatters from database [group id: {_groupId}]");
}
protected override void OnInitialAdd(string key, GroupChatter value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.GroupId, nameof(value.GroupId));
ArgumentNullException.ThrowIfNull(value.ChatterId, nameof(value.ChatterId));
}
protected override void OnInitialModify(string key, GroupChatter oldValue, GroupChatter newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.GroupId, nameof(newValue.GroupId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.GroupId, nameof(newValue.GroupId));
ArgumentNullException.ThrowIfNull(newValue.ChatterId, nameof(newValue.ChatterId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.GroupId, newValue.GroupId, nameof(oldValue.GroupId));
}
}
}