Files
hermes-server/Store/ConnectionStore.cs

80 lines
4.3 KiB
C#

using HermesSocketLibrary.db;
using HermesSocketLibrary.Socket.Data;
using HermesSocketServer.Store.Internal;
namespace HermesSocketServer.Store
{
public class ConnectionStore : ComplexAutoSavedStore<string, Connection>
{
private readonly string _userId;
private readonly Database _database;
private readonly Serilog.ILogger _logger;
public ConnectionStore(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<string, object>() { { "user", _userId } };
string sql = $"SELECT name, \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" FROM \"Connection\" WHERE \"userId\" = @user";
await _database.Execute(sql, data, (reader) =>
{
var name = reader.GetString(0);
_store.Add(name, new Connection()
{
Name = name,
UserId = _userId,
Type = reader.GetString(1),
ClientId = reader.GetString(2),
AccessToken = reader.GetString(3),
GrantType = reader.GetString(4),
Scope = reader.GetString(5),
ExpiresAt = reader.GetDateTime(6),
Default = reader.GetBoolean(7),
});
});
_logger.Information($"Loaded {_store.Count} connections from database.");
}
protected override void OnInitialAdd(string key, Connection value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Name, nameof(value.Name));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Type, nameof(value.Type));
ArgumentException.ThrowIfNullOrWhiteSpace(value.ClientId, nameof(value.ClientId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.AccessToken, nameof(value.AccessToken));
ArgumentException.ThrowIfNullOrWhiteSpace(value.GrantType, nameof(value.GrantType));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Scope, nameof(value.Scope));
ArgumentNullException.ThrowIfNull(value.ExpiresAt, nameof(value.ExpiresAt));
ArgumentNullException.ThrowIfNull(value.Default, nameof(value.Default));
if (value.Name.Length > 36)
throw new ArgumentException("Action name cannot be longer than 36 characters.");
}
protected override void OnInitialModify(string key, Connection oldValue, Connection newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Name, nameof(newValue.Name));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Type, nameof(newValue.Type));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.ClientId, nameof(newValue.ClientId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.AccessToken, nameof(newValue.AccessToken));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.GrantType, nameof(newValue.GrantType));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Scope, nameof(newValue.Scope));
ArgumentNullException.ThrowIfNull(newValue.ExpiresAt, nameof(newValue.ExpiresAt));
ArgumentNullException.ThrowIfNull(newValue.Default, nameof(newValue.Default));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Name, newValue.Name, nameof(oldValue.Name));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Type, newValue.Type, nameof(oldValue.Type));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.ClientId, newValue.ClientId, nameof(oldValue.ClientId));
}
}
}