diff --git a/Core/ModuleRegistry/DbPostgreSqlModule.cs b/Core/ModuleRegistry/DbPostgreSqlModule.cs index a6b4b5a..3207442 100644 --- a/Core/ModuleRegistry/DbPostgreSqlModule.cs +++ b/Core/ModuleRegistry/DbPostgreSqlModule.cs @@ -8,8 +8,9 @@ namespace Core.ModuleRegistry public required string ConnectionString { get; set; } protected override void Load(ContainerBuilder builder) { + Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider(); - builder.Register(c => + builder.Register(c => { IDbConnection connection = new NpgsqlConnection(ConnectionString); return connection; diff --git a/Database/Core/SetupIdentitySystem.cs b/Database/Core/SetupIdentitySystem.cs new file mode 100644 index 0000000..1690b1e --- /dev/null +++ b/Database/Core/SetupIdentitySystem.cs @@ -0,0 +1,148 @@ +using Insight.Database; +using System.Data; + +namespace Database.IdentitySystem +{ + public interface IDbSetup + { + void CreateSystem(string schema = null); + } + + /// + /// This is by purpose not async await + /// + public class DbSetup : IDbSetup + { + readonly IDbConnection _db; + IDbTransaction _transaction = null; + string _schema; + + public DbSetup(IDbConnection db) + { + _db = db; + } + + /// + /// Creates the system tables in the specified schema within a transaction. + /// + /// The schema name where the tables will be created. + public void CreateSystem(string schema = null) + { + + using (_transaction = _db.BeginTransaction()) + { + try + { + CreateUsersTable(); + CreateTenantsTable(); + CreateUserTenantsTable(); + SetupRLS(); + + _transaction.Commit(); + } + catch (Exception ex) + { + _transaction.Rollback(); + throw new InvalidOperationException("Failed to create system tables.", ex); + } + } + } + private void ExecuteSql(string sql) + { + if (string.IsNullOrEmpty(sql)) + throw new ArgumentNullException(nameof(sql)); + + _db.ExecuteSql(sql); + } + + + /// + /// Creates the users table + /// + public void CreateUsersTable() + { + var sql = @" + CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + email VARCHAR(256) NOT NULL UNIQUE, + password_hash VARCHAR(256) NOT NULL, + security_stamp VARCHAR(36) NOT NULL, + email_confirmed BOOLEAN NOT NULL DEFAULT FALSE, + access_failed_count INTEGER NOT NULL DEFAULT 0, + lockout_enabled BOOLEAN NOT NULL DEFAULT TRUE, + lockout_end TIMESTAMPTZ NULL, + is_active BOOLEAN NOT NULL DEFAULT TRUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_login_at TIMESTAMPTZ NULL + );"; + + ExecuteSql(sql); + + } + + /// + /// Creates the tenants table + /// + public void CreateTenantsTable() + { + var sql = @" + CREATE TABLE IF NOT EXISTS tenants ( + id SERIAL PRIMARY KEY, + connection_string VARCHAR(500) NOT NULL, + is_active BOOLEAN NOT NULL DEFAULT TRUE, + created_by INTEGER NOT NULL REFERENCES users(id), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + );"; + + ExecuteSql(sql); + + } + + /// + /// Creates the user_tenants table + /// + public void CreateUserTenantsTable() + { + var sql = @" + CREATE TABLE IF NOT EXISTS user_tenants ( + user_id INTEGER NOT NULL REFERENCES users(id), + tenant_id INTEGER NOT NULL REFERENCES tenants(id), + pin_code VARCHAR(10) NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id, tenant_id) + );"; + + ExecuteSql(sql); + + } + + /// + /// Sets up Row Level Security (RLS) for the tenants and user_tenants tables. + /// + public void SetupRLS() + { + var sql = new[] + { + "ALTER TABLE tenants ENABLE ROW LEVEL SECURITY;", + "ALTER TABLE user_tenants ENABLE ROW LEVEL SECURITY;", + "DROP POLICY IF EXISTS tenant_access ON tenants;", + @"CREATE POLICY tenant_access ON tenants + USING (id IN ( + SELECT tenant_id + FROM user_tenants + WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER + ));", + "DROP POLICY IF EXISTS user_tenant_access ON user_tenants;", + @"CREATE POLICY user_tenant_access ON user_tenants + USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);" + }; + + foreach (var statement in sql) + { + ExecuteSql(statement); + } + } + + + } +} \ No newline at end of file diff --git a/Database/Core/SetupUser.cs b/Database/Core/SetupUser.cs new file mode 100644 index 0000000..ded9ddd --- /dev/null +++ b/Database/Core/SetupUser.cs @@ -0,0 +1,62 @@ +using System; +using System.Data; +using System.Text.RegularExpressions; +using Insight.Database; +namespace Database.Core +{ + + public class SetupUser + { + private readonly IDbConnection _db; + + public SetupUser(IDbConnection db) + { + _db = db; + } + + public async Task CreateTenantInDatabase(string schema, string user, string password) + { + if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$")) + throw new ArgumentException("Invalid schema name"); + + + await CreateUser(user, password); + await CreateSchema(schema); + await GrantSchemaRights(schema, user); + + await CreateNavigationLinkTemplatesTable(schema); + await CreateNavigationLinkTemplateTranslationsTable(schema); + } + + private async Task CreateSchema(string schema) + { + var sql = $"CREATE SCHEMA IF NOT EXISTS {schema}"; + await _db.ExecuteAsync(sql); + } + private async Task CreateUser(string user, string password) + { + var sql = $"CREATE USER {user} WITH PASSWORD '{password}';"; + await _db.ExecuteAsync(sql); + } + + private async Task GrantSchemaRights(string schema, string user) + { + var sql = $"GRANT USAGE ON SCHEMA {schema} TO {user};"; + await _db.ExecuteAsync(sql); + + var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} " + + $"GRANT ALL PRIVILEGES ON TABLES TO {user};"; + await _db.ExecuteAsync(sql1); + + var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {schema} TO {user};"; + await _db.ExecuteAsync(sql2); + + + + } + + + } + + +} diff --git a/Database/IdentitySystem/UserService.cs b/Database/Core/UserService.cs similarity index 100% rename from Database/IdentitySystem/UserService.cs rename to Database/Core/UserService.cs diff --git a/Database/IdentitySystem/Setup.cs b/Database/IdentitySystem/Setup.cs deleted file mode 100644 index 42afa57..0000000 --- a/Database/IdentitySystem/Setup.cs +++ /dev/null @@ -1,147 +0,0 @@ -using Database.Common; -using Insight.Database; -using System; -using System.Data; -using System.Threading.Tasks; - -namespace Database.IdentitySystem -{ - public class DbSetup - { - readonly IDbConnection _db; - IDbTransaction _transaction = null; - string _schema; - - public DbSetup(IDbConnection db) - { - _db = db ?? throw new ArgumentNullException(nameof(db)); - } - - /// - /// Creates the system tables in the specified schema within a transaction. - /// - /// The schema name where the tables will be created. - public async Task CreateSystem(string schema) - { - _schema = schema; - - if (!Validations.IsValidSchemaName(schema)) - throw new ArgumentException("Invalid schema name", nameof(schema)); - - using (_transaction = _db.BeginTransaction()) - { - try - { - await CreateUsersTable().ConfigureAwait(false); - await CreateTenantsTable().ConfigureAwait(false); - await CreateUserTenantsTable().ConfigureAwait(false); - await SetupRLS().ConfigureAwait(false); - - _transaction.Commit(); - } - catch (Exception ex) - { - _transaction.Rollback(); - throw new InvalidOperationException("Failed to create system tables.", ex); - } - } - } - private async Task ExecuteSqlAsync(string sql) - { - if (string.IsNullOrEmpty(sql)) - throw new ArgumentNullException(nameof(sql)); - - await _db.ExecuteAsync(sql).ConfigureAwait(false); - } - - - /// - /// Creates the users table in the ptmain schema. - /// - public async Task CreateUsersTable() - { - var sql = @" - CREATE TABLE IF NOT EXISTS ptmain.users ( - id SERIAL PRIMARY KEY, - email VARCHAR(256) NOT NULL UNIQUE, - password_hash VARCHAR(256) NOT NULL, - security_stamp VARCHAR(36) NOT NULL, - email_confirmed BOOLEAN NOT NULL DEFAULT FALSE, - access_failed_count INTEGER NOT NULL DEFAULT 0, - lockout_enabled BOOLEAN NOT NULL DEFAULT TRUE, - lockout_end TIMESTAMPTZ NULL, - is_active BOOLEAN NOT NULL DEFAULT TRUE, - created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - last_login_at TIMESTAMPTZ NULL - );"; - - await ExecuteSqlAsync(sql).ConfigureAwait(false); - - } - - /// - /// Creates the tenants table in the ptmain schema. - /// - public async Task CreateTenantsTable() - { - var sql = @" - CREATE TABLE IF NOT EXISTS ptmain.tenants ( - id SERIAL PRIMARY KEY, - connection_string VARCHAR(500) NOT NULL, - is_active BOOLEAN NOT NULL DEFAULT TRUE, - created_by INTEGER NOT NULL REFERENCES ptmain.users(id), - created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP - );"; - - await ExecuteSqlAsync(sql).ConfigureAwait(false); - - } - - /// - /// Creates the user_tenants table in the ptmain schema. - /// - public async Task CreateUserTenantsTable() - { - var sql = @" - CREATE TABLE IF NOT EXISTS ptmain.user_tenants ( - user_id INTEGER NOT NULL REFERENCES ptmain.users(id), - tenant_id INTEGER NOT NULL REFERENCES ptmain.tenants(id), - pin_code VARCHAR(10) NULL, - created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (user_id, tenant_id) - );"; - - await ExecuteSqlAsync(sql).ConfigureAwait(false); - - } - - /// - /// Sets up Row Level Security (RLS) for the tenants and user_tenants tables. - /// - public async Task SetupRLS() - { - var sql = new[] - { - "ALTER TABLE ptmain.tenants ENABLE ROW LEVEL SECURITY;", - "ALTER TABLE ptmain.user_tenants ENABLE ROW LEVEL SECURITY;", - "DROP POLICY IF EXISTS tenant_access ON ptmain.tenants;", - @"CREATE POLICY tenant_access ON ptmain.tenants - USING (id IN ( - SELECT tenant_id - FROM ptmain.user_tenants - WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER - ));", - "DROP POLICY IF EXISTS user_tenant_access ON ptmain.user_tenants;", - @"CREATE POLICY user_tenant_access ON ptmain.user_tenants - USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);" - }; - - foreach (var statement in sql) - { - await ExecuteSqlAsync(statement).ConfigureAwait(false); - } - } - - - } -} \ No newline at end of file diff --git a/Database/RolesPermissionSystem/NavigationSystem.cs b/Database/RolesPermissionSystem/NavigationSystem.cs new file mode 100644 index 0000000..b655090 --- /dev/null +++ b/Database/RolesPermissionSystem/NavigationSystem.cs @@ -0,0 +1,49 @@ +using Insight.Database; +using System.Data; + +namespace Database.RolesPermissionSystem +{ + internal class NavigationSystem + { + + private readonly IDbConnection _db; + + public NavigationSystem(IDbConnection db) + { + _db = db; + } + + + private async Task CreateNavigationLinkTemplatesTable(string schema) + { + var sql = $@" + CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates ( + id SERIAL PRIMARY KEY, + parent_id INTEGER NULL, + url VARCHAR(500) NOT NULL, + permission_id INTEGER NULL, + icon VARCHAR(100) NULL, + default_order INTEGER NOT NULL, + FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id), + FOREIGN KEY (parent_id) REFERENCES {schema}.navigation_link_templates(id) + )"; + await _db.ExecuteAsync(sql); + } + + private async Task CreateNavigationLinkTemplateTranslationsTable(string schema) + { + var sql = $@" + CREATE TABLE IF NOT EXISTS {schema}.navigation_link_template_translations ( + id SERIAL PRIMARY KEY, + template_id INTEGER NOT NULL, + language VARCHAR(10) NOT NULL, + display_name VARCHAR(100) NOT NULL, + FOREIGN KEY (template_id) REFERENCES {schema}.navigation_link_templates(id) + )"; + await _db.ExecuteAsync(sql); + } + + + + } +} diff --git a/Database/RolesPermissionSystem/Setup.cs b/Database/RolesPermissionSystem/Setup.cs index 453d8b6..73c6eb5 100644 --- a/Database/RolesPermissionSystem/Setup.cs +++ b/Database/RolesPermissionSystem/Setup.cs @@ -5,87 +5,87 @@ using Insight.Database; namespace Database.RolesPermissionSystem { - public class Setup - { - private readonly IDbConnection _db; + public class Setup + { + private readonly IDbConnection _db; - public Setup(IDbConnection db) - { - _db = db ?? throw new ArgumentNullException(nameof(db)); - } + public Setup(IDbConnection db) + { + _db = db ?? throw new ArgumentNullException(nameof(db)); + } - /// - /// Creates the system tables in the specified schema within a transaction. - /// - /// The schema name where the tables will be created. - public async Task CreateSystem(string schema) - { - if (!Validations.IsValidSchemaName(schema)) - throw new ArgumentException("Invalid schema name", nameof(schema)); + /// + /// Creates the system tables in the specified schema within a transaction. + /// + /// The schema name where the tables will be created. + public void CreateSystem(string schema) + { + if (!Validations.IsValidSchemaName(schema)) + throw new ArgumentException("Invalid schema name", nameof(schema)); - using (var transaction = _db.BeginTransaction()) - { - try - { - await CreateRolesTable(schema, transaction).ConfigureAwait(false); - await CreatePermissionsTable(schema, transaction).ConfigureAwait(false); - await CreatePermissionTypesTable(schema, transaction).ConfigureAwait(false); - await CreateRolePermissionsTable(schema, transaction).ConfigureAwait(false); + using (var transaction = _db.BeginTransaction()) + { + try + { + CreateRolesTable(schema, transaction); + CreatePermissionsTable(schema, transaction); + CreatePermissionTypesTable(schema, transaction); + CreateRolePermissionsTable(schema, transaction); - transaction.Commit(); - } - catch (Exception ex) - { - transaction.Rollback(); - throw new InvalidOperationException("Failed to create system tables.", ex); - } - } - } + transaction.Commit(); + } + catch (Exception ex) + { + transaction.Rollback(); + throw new InvalidOperationException("Failed to create system tables.", ex); + } + } + } - private async Task ExecuteSqlAsync(string sql, IDbTransaction transaction) - { - if (string.IsNullOrEmpty(sql)) - throw new ArgumentNullException(nameof(sql)); + private void ExecuteSql(string sql, IDbTransaction transaction) + { + if (string.IsNullOrEmpty(sql)) + throw new ArgumentNullException(nameof(sql)); - await _db.ExecuteAsync(sql, transaction: transaction).ConfigureAwait(false); - } + _db.Execute(sql, transaction: transaction); + } - private async Task CreatePermissionTypesTable(string schema, IDbTransaction transaction) - { - var sql = $@" + private void CreatePermissionTypesTable(string schema, IDbTransaction transaction) + { + var sql = $@" CREATE TABLE IF NOT EXISTS {schema}.permission_types ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL UNIQUE )"; - await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false); - } + ExecuteSql(sql, transaction); + } - private async Task CreatePermissionsTable(string schema, IDbTransaction transaction) - { - var sql = $@" + private void CreatePermissionsTable(string schema, IDbTransaction transaction) + { + 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) )"; - await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false); - } + ExecuteSql(sql, transaction); + } - private async Task CreateRolesTable(string schema, IDbTransaction transaction) - { - var sql = $@" + private void CreateRolesTable(string schema, IDbTransaction transaction) + { + var sql = $@" CREATE TABLE IF NOT EXISTS {schema}.roles ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL UNIQUE )"; - await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false); - } + ExecuteSql(sql, transaction); + } - private async Task CreateRolePermissionsTable(string schema, IDbTransaction transaction) - { - var sql = $@" + private void CreateRolePermissionsTable(string schema, IDbTransaction transaction) + { + var sql = $@" CREATE TABLE IF NOT EXISTS {schema}.role_permissions ( role_id INTEGER NOT NULL, permission_id INTEGER NOT NULL, @@ -93,7 +93,7 @@ namespace Database.RolesPermissionSystem FOREIGN KEY (role_id) REFERENCES {schema}.roles(id), FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id) )"; - await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false); - } - } + ExecuteSql(sql, transaction); + } + } } \ No newline at end of file diff --git a/Database/Tenants/TenantSetupService.cs b/Database/Tenants/TenantSetupService.cs index 9ea033d..10553fb 100644 --- a/Database/Tenants/TenantSetupService.cs +++ b/Database/Tenants/TenantSetupService.cs @@ -5,105 +5,103 @@ using Insight.Database; namespace Database.Tenants { - public class TenantSetupService - { - private readonly IDbConnection _db; + public class Setup + { + private readonly IDbConnection _db; - public TenantSetupService(IDbConnection db) - { - _db = db; - } + public Setup(IDbConnection db) + { + _db = db; + } - public async Task CreateTenantInDatabase(string schema, string user, string password) - { - if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$")) - throw new ArgumentException("Invalid schema name"); + public async Task CreateTenantInDatabase(string schema, string user, string password) + { + if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$")) + throw new ArgumentException("Invalid schema name"); - await CreateUser(user, password); - await CreateSchema(schema); - await CreateRolesTable(schema); - await CreatePermissionsTable(schema); - await CreateRolePermissionsTable(schema); - await CreateNavigationLinkTemplatesTable(schema); - await CreateNavigationLinkTemplateTranslationsTable(schema); - } + await CreateUser(user, password); + await CreateSchema(schema); + await GrantSchemaRights(schema, user); - private async Task CreateSchema(string schema) - { - var sql = $"CREATE SCHEMA IF NOT EXISTS {schema}"; - await _db.ExecuteAsync(sql); - } - private async Task CreateUser(string user, string password) - { - var sql = $"CREATE USER {user} WITH PASSWORD '{password}';"; - await _db.ExecuteAsync(sql); - } + await CreateNavigationLinkTemplatesTable(schema); + await CreateNavigationLinkTemplateTranslationsTable(schema); + } - private async Task GrantSchemaRights(string schema, string user) - { - var sql = $"GRANT USAGE ON SCHEMA {schema} TO {user};"; - await _db.ExecuteAsync(sql); + private async Task CreateSchema(string schema) + { + var sql = $"CREATE SCHEMA IF NOT EXISTS {schema}"; + await _db.ExecuteAsync(sql); + } + private async Task CreateUser(string user, string password) + { + var sql = $"CREATE USER {user} WITH PASSWORD '{password}';"; + await _db.ExecuteAsync(sql); + } - var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} " + - $"GRANT ALL PRIVILEGES ON TABLES TO {user};"; - await _db.ExecuteAsync(sql1); + private async Task GrantSchemaRights(string schema, string user) + { + var sql = $"GRANT USAGE ON SCHEMA {schema} TO {user};"; + await _db.ExecuteAsync(sql); + var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} " + + $"GRANT ALL PRIVILEGES ON TABLES TO {user};"; + await _db.ExecuteAsync(sql1); - var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {schema} TO {user};"; - await _db.ExecuteAsync(sql2); + var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {schema} TO {user};"; + await _db.ExecuteAsync(sql2); - } - private async Task CreatePermissionTypesTable(string schema) - { - var sql = $@" - CREATE TABLE IF NOT EXISTS {schema}.permission_types ( - id SERIAL PRIMARY KEY, - name VARCHAR(100) NOT NULL UNIQUE - )"; - await _db.ExecuteAsync(sql); - } + } + //private async Task CreatePermissionTypesTable(string schema) + //{ + // var sql = $@" + // CREATE TABLE IF NOT EXISTS {schema}.permission_types ( + // id SERIAL PRIMARY KEY, + // name VARCHAR(100) NOT NULL UNIQUE + // )"; + // await _db.ExecuteAsync(sql); + //} - private async Task CreatePermissionsTable(string schema) - { - 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) - )"; - await _db.ExecuteAsync(sql); - } + //private async Task CreatePermissionsTable(string schema) + //{ + // 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) + // )"; + // await _db.ExecuteAsync(sql); + //} - private async Task CreateRolesTable(string schema) - { - var sql = $@" - CREATE TABLE IF NOT EXISTS {schema}.roles ( - id SERIAL PRIMARY KEY, - name VARCHAR(100) NOT NULL UNIQUE - )"; - await _db.ExecuteAsync(sql); - } + //private async Task CreateRolesTable(string schema) + //{ + // var sql = $@" + // CREATE TABLE IF NOT EXISTS {schema}.roles ( + // id SERIAL PRIMARY KEY, + // name VARCHAR(100) NOT NULL UNIQUE + // )"; + // await _db.ExecuteAsync(sql); + //} - private async Task CreateRolePermissionsTable(string schema) - { - 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) - )"; - await _db.ExecuteAsync(sql); - } + //private async Task CreateRolePermissionsTable(string schema) + //{ + // 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) + // )"; + // await _db.ExecuteAsync(sql); + //} - private async Task CreateNavigationLinkTemplatesTable(string schema) - { - var sql = $@" + private async Task CreateNavigationLinkTemplatesTable(string schema) + { + var sql = $@" CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates ( id SERIAL PRIMARY KEY, parent_id INTEGER NULL, @@ -114,12 +112,12 @@ namespace Database.Tenants FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id), FOREIGN KEY (parent_id) REFERENCES {schema}.navigation_link_templates(id) )"; - await _db.ExecuteAsync(sql); - } + await _db.ExecuteAsync(sql); + } - private async Task CreateNavigationLinkTemplateTranslationsTable(string schema) - { - var sql = $@" + private async Task CreateNavigationLinkTemplateTranslationsTable(string schema) + { + var sql = $@" CREATE TABLE IF NOT EXISTS {schema}.navigation_link_template_translations ( id SERIAL PRIMARY KEY, template_id INTEGER NOT NULL, @@ -127,9 +125,9 @@ namespace Database.Tenants display_name VARCHAR(100) NOT NULL, FOREIGN KEY (template_id) REFERENCES {schema}.navigation_link_templates(id) )"; - await _db.ExecuteAsync(sql); - } - } + await _db.ExecuteAsync(sql); + } + } } diff --git a/SetupInfrastructure/Program.cs b/SetupInfrastructure/Program.cs index 8e5b13b..7df378f 100644 --- a/SetupInfrastructure/Program.cs +++ b/SetupInfrastructure/Program.cs @@ -1,47 +1,33 @@ -using Microsoft.ApplicationInsights; -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.Extensibility; -using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; +using Autofac; namespace SetupInfrastructure { - /// - /// SETUP APPLICATION USER NAMED sathumper - /// - /// This should be handled on the Postgresql db server with a superadmin or similar. - /// - /// Execute SQL CreateRole.txt - /// - /// After that is executed it is time for running this main program - /// Remember to use the newly created sathumper - /// "ConnectionStrings": { - /// "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=;" - /// - internal class Program - { - static async Task Main(string[] args) - { + /// + /// SETUP APPLICATION USER NAMED sathumper + /// + /// This should be handled on the Postgresql db server with a superadmin or similar. + /// + /// Execute SQL CreateRole.txt + /// + /// After that is executed it is time for running this main program + /// Remember to use the newly created sathumper + /// "ConnectionStrings": { + /// "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=;" + /// + internal class Program + { + static async Task Main(string[] args) + { + var container = new Startup().ConfigureContainer(); - var telemetryChannel = new ServerTelemetryChannel(); - var configuration = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault(); - configuration.ConnectionString = "InstrumentationKey=2d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"; - configuration.TelemetryChannel = telemetryChannel; + + container.Resolve - + - telemetryChannel.Initialize(configuration); + } + } - var log = new TelemetryClient(configuration); - log.TrackTrace("Console log med kanal 2"); - - log.Flush(); - - Console.WriteLine("Hello, World!"); - await Task.Delay(5000); - - Console.Read(); - } - } } diff --git a/SetupInfrastructure/Startup.cs b/SetupInfrastructure/Startup.cs new file mode 100644 index 0000000..5847ee1 --- /dev/null +++ b/SetupInfrastructure/Startup.cs @@ -0,0 +1,39 @@ +using Autofac; +using Core.Configurations; +using Core.Configurations.JsonConfigProvider; + + +namespace SetupInfrastructure +{ + public class Startup + { + public virtual IConfigurationRoot Configuration() + { + var configuration = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); + + return configuration; + } + + public IContainer ConfigureContainer() + { + var builder = new ContainerBuilder(); + var configuration = Configuration(); + + + builder.RegisterModule(new Core.ModuleRegistry.DbPostgreSqlModule + { + ConnectionString = configuration.GetConnectionString("DefaultConnection") + }); + + builder.RegisterModule(new Core.ModuleRegistry.TelemetryModule + { + TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject() + }); + + + return builder.Build(); + } + } +} diff --git a/SetupInfrastructure/appconfiguration.json b/SetupInfrastructure/appconfiguration.json index 0e3cd9a..192c71e 100644 --- a/SetupInfrastructure/appconfiguration.json +++ b/SetupInfrastructure/appconfiguration.json @@ -1,7 +1,7 @@ { "AllowedHosts": "*", "ConnectionStrings": { - "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=3911;" + "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=3911;" }, "ApplicationInsights": { "ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/" diff --git a/Tests/TestFixture.cs b/Tests/TestFixture.cs index 859cf38..85ae847 100644 --- a/Tests/TestFixture.cs +++ b/Tests/TestFixture.cs @@ -37,7 +37,7 @@ namespace Tests { CreateContainerBuilder(); Container = ContainerBuilder.Build(); - Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider(); + }