using HermesSocketLibrary.db; using HermesSocketServer.Models; using HermesSocketServer.Store.Internal; namespace HermesSocketServer.Store { public class PolicyStore : AutoSavedStore { private readonly string _userId; private readonly Database _database; private readonly Serilog.ILogger _logger; public PolicyStore(string userId, DatabaseTable table, Database database, Serilog.ILogger logger) : base(table, database, logger) { _userId = userId; _database = database; _logger = logger; } public override async Task Load() { var data = new Dictionary() { { "user", _userId } }; string sql = $"SELECT id, \"groupId\", path, count, timespan FROM \"GroupPermissionPolicy\" WHERE \"userId\" = @user"; await _database.Execute(sql, data, (reader) => { var id = reader.GetGuid(0); _store.Add(id.ToString(), new PolicyMessage() { Id = id, UserId = _userId, GroupId = reader.GetGuid(1), Path = reader.GetString(2), Usage = reader.GetInt32(3), Span = reader.GetInt32(4), }); }); _logger.Information($"Loaded {_store.Count} policies from database."); } protected override void OnInitialAdd(string key, PolicyMessage value) { } protected override void OnInitialModify(string key, PolicyMessage value) { } protected override void OnInitialRemove(string key) { } } }