diff --git a/Configuration.cs b/Configuration.cs index efcc2cc..04f01ed 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -52,7 +52,7 @@ namespace BirthdayBot var sqlcs = jc["SqlConnectionString"]?.Value(); if (string.IsNullOrWhiteSpace(sqlcs)) throw new Exception("'SqlConnectionString' must be specified."); - DatabaseSettings = new Database(sqlcs); + Database.DBConnectionString = sqlcs; int? sc = jc["ShardCount"]?.Value(); if (!sc.HasValue) ShardCount = 1; diff --git a/Data/Database.cs b/Data/Database.cs index 90a82af..2581669 100644 --- a/Data/Database.cs +++ b/Data/Database.cs @@ -1,42 +1,31 @@ using Npgsql; +using System; using System.Threading.Tasks; namespace BirthdayBot.Data { /// - /// Some database abstractions. + /// Database access and some abstractions. /// - class Database + internal static class Database { - /* - * Database storage in this project, explained: - * Each guild gets a row in the settings table. This table is referred to when doing most things. - * Within each guild, each known user gets a row in the users table with specific information specified. - * Users can override certain settings in global, such as time zone. - */ + public static string DBConnectionString { get; set; } - private string DBConnectionString { get; } - - public Database(string connString) - { - DBConnectionString = connString; - - // Database initialization happens here as well. - SetupTables(); - } - - public async Task OpenConnectionAsync() + public static async Task OpenConnectionAsync() { + if (DBConnectionString == null) throw new Exception("Database connection string not set"); var db = new NpgsqlConnection(DBConnectionString); await db.OpenAsync(); return db; } - private void SetupTables() + public static async Task DoInitialDatabaseSetupAsync() { - using var db = OpenConnectionAsync().GetAwaiter().GetResult(); - GuildStateInformation.SetUpDatabaseTable(db); // Note: Call this first. (Foreign reference constraints.) - GuildUserSettings.SetUpDatabaseTable(db); + using var db = await OpenConnectionAsync(); + + // Refer to the methods being called for information on how the database is set up. + await GuildConfiguration.DatabaseSetupAsync(db); // Note: Call this first. (Foreign reference constraints.) + await GuildUserConfiguration.DatabaseSetupAsync(db); } } }