Conforming all these db calls

This commit is contained in:
Janus C. H. Knudsen 2025-02-06 23:46:55 +01:00
parent 5ca874abe9
commit 05d6977a76
17 changed files with 337 additions and 414 deletions

View file

@ -1,99 +1,98 @@
using System.Data;
using System.Text.RegularExpressions;
using Database.Common;
using Insight.Database;
namespace Database.RolesPermissionSystem
{
public class Setup
{
private readonly IDbConnection _db;
public class Setup
{
IDbConnection _db;
string _schema;
public Setup(IDbConnection db)
{
_db = db ?? throw new ArgumentNullException(nameof(db));
}
public Setup(IDbConnection db)
{
_db = db;
}
/// <summary>
/// Creates the system tables in the specified schema within a transaction.
/// </summary>
/// <param name="schema">The schema name where the tables will be created.</param>
public void CreateSystem(string schema)
{
if (!Validations.IsValidSchemaName(schema))
throw new ArgumentException("Invalid schema name", nameof(schema));
/// <summary>
/// Creates the system tables in the specified schema within a transaction.
/// </summary>
/// <param name="schema">The schema name where the tables will be created.</param>
public void CreateSystem(string schema)
{
_schema = schema;
using (var transaction = _db.BeginTransaction())
{
try
{
CreateRolesTable(schema, transaction);
CreatePermissionsTable(schema, transaction);
CreatePermissionTypesTable(schema, transaction);
CreateRolePermissionsTable(schema, transaction);
if (!Validations.IsValidSchemaName(_schema))
throw new ArgumentException("Invalid schema name", _schema);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw new InvalidOperationException("Failed to create system tables.", ex);
}
}
}
using (var transaction = _db.BeginTransaction())
{
try
{
CreateRolesTable();
CreatePermissionsTable();
CreatePermissionTypesTable();
CreateRolePermissionsTable();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw new InvalidOperationException("Failed to create system tables.", ex);
}
}
}
private void ExecuteSql(string sql, IDbTransaction transaction)
{
if (string.IsNullOrEmpty(sql))
throw new ArgumentNullException(nameof(sql));
private void ExecuteSql(string sql)
{
_db.ExecuteSql(sql);
}
_db.Execute(sql, transaction: transaction);
}
private void CreatePermissionTypesTable(string schema, IDbTransaction transaction)
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {schema}.permission_types (
private void CreatePermissionTypesTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.permission_types (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)";
ExecuteSql(sql, transaction);
}
ExecuteSql(sql);
}
private void CreatePermissionsTable(string schema, IDbTransaction transaction)
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {schema}.permissions (
private void CreatePermissionsTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.permissions (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
type_id INTEGER NOT NULL,
FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
FOREIGN KEY (type_id) REFERENCES {_schema}.permission_types(id)
)";
ExecuteSql(sql, transaction);
}
ExecuteSql(sql);
}
private void CreateRolesTable(string schema, IDbTransaction transaction)
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {schema}.roles (
private void CreateRolesTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.roles (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)";
ExecuteSql(sql, transaction);
}
ExecuteSql(sql);
}
private void CreateRolePermissionsTable(string schema, IDbTransaction transaction)
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
private void CreateRolePermissionsTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.role_permissions (
role_id INTEGER NOT NULL,
permission_id INTEGER NOT NULL,
PRIMARY KEY (role_id, permission_id),
FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
FOREIGN KEY (role_id) REFERENCES {_schema}.roles(id),
FOREIGN KEY (permission_id) REFERENCES {_schema}.permissions(id)
)";
ExecuteSql(sql, transaction);
}
}
ExecuteSql(sql);
}
}
}