Added database table data into configuration. Store saves is auto-handled. Added Action & Redemption stores.
This commit is contained in:
@@ -1,33 +1,22 @@
|
||||
using System.Collections.Immutable;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store.Internal;
|
||||
|
||||
namespace HermesSocketServer.Store
|
||||
{
|
||||
public class PolicyStore : GroupSaveStore<string, PolicyMessage>
|
||||
public class PolicyStore : AutoSavedStore<string, PolicyMessage>
|
||||
{
|
||||
private readonly string _userId;
|
||||
private readonly Database _database;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
private readonly GroupSaveSqlGenerator<PolicyMessage> _generator;
|
||||
|
||||
|
||||
public PolicyStore(string userId, Database database, Serilog.ILogger logger) : base(logger)
|
||||
public PolicyStore(string userId, DatabaseTable table, Database database, Serilog.ILogger logger)
|
||||
: base(table, database, logger)
|
||||
{
|
||||
_userId = userId;
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
|
||||
var ctp = new Dictionary<string, string>
|
||||
{
|
||||
{ "id", "Id" },
|
||||
{ "userId", "UserId" },
|
||||
{ "groupId", "GroupId" },
|
||||
{ "path", "Path" },
|
||||
{ "count", "Usage" },
|
||||
{ "timespan", "Span" },
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<PolicyMessage>(ctp, _logger);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
@@ -61,54 +50,5 @@ namespace HermesSocketServer.Store
|
||||
protected override void OnInitialRemove(string key)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
ImmutableList<string>? list = null;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _added.ToImmutableList();
|
||||
_added.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedInsertSql("GroupPermissionPolicy", count, ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Adding {count} rows to database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _modified.ToImmutableList();
|
||||
_modified.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedUpdateSql("GroupPermissionPolicy", count, ["id"], ["userId", "groupId", "path", "count", "timespan"]);
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Modifying {count} rows in database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _deleted.ToImmutableList();
|
||||
_deleted.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedDeleteSql("GroupPermissionPolicy", count, ["id"]);
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Deleting {count} rows from database: {sql}");
|
||||
await _generator.DoPreparedStatementRaw(_database, sql, list.Select(id => new Guid(id)), ["id"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user