PlanTempusApp/Database/RolesPermissionSystem/Setup.cs

98 lines
2.8 KiB
C#
Raw Normal View History

2025-01-28 14:51:09 +01:00
using System.Data;
using Database.Common;
2025-01-28 14:51:09 +01:00
using Insight.Database;
namespace Database.RolesPermissionSystem
2025-01-28 14:51:09 +01:00
{
2025-02-06 23:46:55 +01:00
public class Setup
{
IDbConnection _db;
string _schema;
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
public Setup(IDbConnection db)
{
_db = db;
}
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
/// <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;
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
if (!Validations.IsValidSchemaName(_schema))
throw new ArgumentException("Invalid schema name", _schema);
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
using (var transaction = _db.BeginTransaction())
{
try
{
CreateRolesTable();
CreatePermissionsTable();
CreatePermissionTypesTable();
CreateRolePermissionsTable();
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw new InvalidOperationException("Failed to create system tables.", ex);
}
}
}
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
private void ExecuteSql(string sql)
{
_db.ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
private void CreatePermissionTypesTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.permission_types (
2025-01-28 14:51:09 +01:00
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)";
2025-02-06 23:46:55 +01:00
ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
private void CreatePermissionsTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.permissions (
2025-01-28 14:51:09 +01:00
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
type_id INTEGER NOT NULL,
2025-02-06 23:46:55 +01:00
FOREIGN KEY (type_id) REFERENCES {_schema}.permission_types(id)
2025-01-28 14:51:09 +01:00
)";
2025-02-06 23:46:55 +01:00
ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
private void CreateRolesTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.roles (
2025-01-28 14:51:09 +01:00
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)";
2025-02-06 23:46:55 +01:00
ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-06 23:46:55 +01:00
private void CreateRolePermissionsTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS {_schema}.role_permissions (
2025-01-28 14:51:09 +01:00
role_id INTEGER NOT NULL,
permission_id INTEGER NOT NULL,
PRIMARY KEY (role_id, permission_id),
2025-02-06 23:46:55 +01:00
FOREIGN KEY (role_id) REFERENCES {_schema}.roles(id),
FOREIGN KEY (permission_id) REFERENCES {_schema}.permissions(id)
2025-01-28 14:51:09 +01:00
)";
2025-02-06 23:46:55 +01:00
ExecuteSql(sql);
}
}
2025-01-28 14:51:09 +01:00
}