From ee7a6fd7b24ef7c006755552598d4fc1ccbef8dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=E2=96=88=E2=96=88=E2=96=88=E2=96=88=E2=96=88?= Date: Mon, 21 Oct 2024 18:19:54 -0400 Subject: [PATCH] Adding interfaces for database --- .../Middleware/BasicAuthenticationHandler.cs | 4 +- .../INullinsideContext.cs | 71 +++++++++++++++++++ src/Nullinside.Api.Model/NullinsideContext.cs | 2 +- .../Shared/UserHelpers.cs | 2 +- .../Controllers/DatabaseController.cs | 4 +- .../Controllers/DockerController.cs | 4 +- .../Controllers/FeatureToggleController.cs | 4 +- .../Controllers/TwitchBotController.cs | 4 +- .../Controllers/UserController.cs | 4 +- src/Nullinside.Api/Program.cs | 2 +- 10 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 src/Nullinside.Api.Model/INullinsideContext.cs diff --git a/src/Nullinside.Api.Common.AspNetCore/Middleware/BasicAuthenticationHandler.cs b/src/Nullinside.Api.Common.AspNetCore/Middleware/BasicAuthenticationHandler.cs index 34f30da..9d1a321 100644 --- a/src/Nullinside.Api.Common.AspNetCore/Middleware/BasicAuthenticationHandler.cs +++ b/src/Nullinside.Api.Common.AspNetCore/Middleware/BasicAuthenticationHandler.cs @@ -20,7 +20,7 @@ public class BasicAuthenticationHandler : AuthenticationHandler /// The nullinside database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -35,7 +35,7 @@ public class BasicAuthenticationHandler : AuthenticationHandlerThe url encoder. /// The database. public BasicAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, - UrlEncoder encoder, NullinsideContext dbContext) : base(options, logger, encoder) { + UrlEncoder encoder, INullinsideContext dbContext) : base(options, logger, encoder) { _dbContext = dbContext; } diff --git a/src/Nullinside.Api.Model/INullinsideContext.cs b/src/Nullinside.Api.Model/INullinsideContext.cs new file mode 100644 index 0000000..cc0798d --- /dev/null +++ b/src/Nullinside.Api.Model/INullinsideContext.cs @@ -0,0 +1,71 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +using Nullinside.Api.Model.Ddl; + +namespace Nullinside.Api.Model; + +/// +/// Represents the nullinside database. +/// +public interface INullinsideContext { + /// + /// The users table which contains all of the users that have ever authenticated with the site. + /// + public DbSet Users { get; set; } + + /// + /// The user's roles table which contains all of the "roles" the user has in the application. + /// + public DbSet UserRoles { get; set; } + + /// + /// The docker deployments that are configurable in the applications. + /// + public DbSet DockerDeployments { get; set; } + + /// + /// The docker deployments that are configurable in the applications. + /// + public DbSet TwitchUser { get; set; } + + /// + /// The docker deployments that are configurable in the applications. + /// + public DbSet TwitchBan { get; set; } + + /// + /// The feature toggles. + /// + public DbSet FeatureToggle { get; set; } + + /// + /// The twitch user configuration. + /// + public DbSet TwitchUserConfig { get; set; } + + /// + /// The twitch logs of users banned outside the bot. + /// + public DbSet TwitchUserBannedOutsideOfBotLogs { get; set; } + + /// + /// The twitch logs of the user's chat. + /// + public DbSet TwitchUserChatLogs { get; set; } + + /// + /// Provides access to database related information and operations for this context. + /// + public DatabaseFacade Database { get; } + + /// + /// Saves all changes made in this context to the database. + /// + /// A to observe while waiting for the task to complete. + /// + /// A task that represents the asynchronous save operation. The task result contains the + /// number of state entries written to the database. + /// + public Task SaveChangesAsync(CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/Nullinside.Api.Model/NullinsideContext.cs b/src/Nullinside.Api.Model/NullinsideContext.cs index 17c3e39..d582574 100644 --- a/src/Nullinside.Api.Model/NullinsideContext.cs +++ b/src/Nullinside.Api.Model/NullinsideContext.cs @@ -7,7 +7,7 @@ namespace Nullinside.Api.Model; /// /// The nullinside database. /// -public class NullinsideContext : DbContext { +public class NullinsideContext : DbContext, INullinsideContext { /// /// Initializes a new instance of /// diff --git a/src/Nullinside.Api.Model/Shared/UserHelpers.cs b/src/Nullinside.Api.Model/Shared/UserHelpers.cs index 826df58..4712112 100644 --- a/src/Nullinside.Api.Model/Shared/UserHelpers.cs +++ b/src/Nullinside.Api.Model/Shared/UserHelpers.cs @@ -23,7 +23,7 @@ public static class UserHelpers { /// The username of the user on twitch. /// The id of the user on twitch. /// The bearer token if successful, null otherwise. - public static async Task GetTokenAndSaveToDatabase(NullinsideContext dbContext, string email, + public static async Task GetTokenAndSaveToDatabase(INullinsideContext dbContext, string email, CancellationToken token = new(), string? authToken = null, string? refreshToken = null, DateTime? expires = null, string? twitchUsername = null, string? twitchId = null) { string bearerToken = GenerateBearerToken(); diff --git a/src/Nullinside.Api/Controllers/DatabaseController.cs b/src/Nullinside.Api/Controllers/DatabaseController.cs index 119ecf8..fade3f1 100644 --- a/src/Nullinside.Api/Controllers/DatabaseController.cs +++ b/src/Nullinside.Api/Controllers/DatabaseController.cs @@ -17,7 +17,7 @@ public class DatabaseController : ControllerBase { /// /// The nullinside database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -28,7 +28,7 @@ public class DatabaseController : ControllerBase { /// Initializes a new instance of the class. /// /// The nullinside database. - public DatabaseController(NullinsideContext dbContext) { + public DatabaseController(INullinsideContext dbContext) { _dbContext = dbContext; } diff --git a/src/Nullinside.Api/Controllers/DockerController.cs b/src/Nullinside.Api/Controllers/DockerController.cs index 82ca9dd..3b9b380 100644 --- a/src/Nullinside.Api/Controllers/DockerController.cs +++ b/src/Nullinside.Api/Controllers/DockerController.cs @@ -23,7 +23,7 @@ public class DockerController : ControllerBase { /// /// The nullinside database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The docker proxy. @@ -40,7 +40,7 @@ public class DockerController : ControllerBase { /// /// The nullinside database. /// The docker proxy. - public DockerController(NullinsideContext dbContext, IDockerProxy dockerProxy) { + public DockerController(INullinsideContext dbContext, IDockerProxy dockerProxy) { _dbContext = dbContext; _docker = dockerProxy; } diff --git a/src/Nullinside.Api/Controllers/FeatureToggleController.cs b/src/Nullinside.Api/Controllers/FeatureToggleController.cs index 39f2ffa..b181bcb 100644 --- a/src/Nullinside.Api/Controllers/FeatureToggleController.cs +++ b/src/Nullinside.Api/Controllers/FeatureToggleController.cs @@ -17,7 +17,7 @@ public class FeatureToggleController : ControllerBase { /// /// The nullinside database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -28,7 +28,7 @@ public class FeatureToggleController : ControllerBase { /// Initializes a new instance of the class. /// /// The nullinside database. - public FeatureToggleController(NullinsideContext dbContext) { + public FeatureToggleController(INullinsideContext dbContext) { _dbContext = dbContext; } diff --git a/src/Nullinside.Api/Controllers/TwitchBotController.cs b/src/Nullinside.Api/Controllers/TwitchBotController.cs index 5f89da0..8219839 100644 --- a/src/Nullinside.Api/Controllers/TwitchBotController.cs +++ b/src/Nullinside.Api/Controllers/TwitchBotController.cs @@ -24,7 +24,7 @@ public class TwitchBotController : ControllerBase { /// /// The nullinside database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -36,7 +36,7 @@ public class TwitchBotController : ControllerBase { /// /// The application's configuration file. /// The nullinside database. - public TwitchBotController(IConfiguration configuration, NullinsideContext dbContext) { + public TwitchBotController(IConfiguration configuration, INullinsideContext dbContext) { _configuration = configuration; _dbContext = dbContext; } diff --git a/src/Nullinside.Api/Controllers/UserController.cs b/src/Nullinside.Api/Controllers/UserController.cs index 9dbb255..b38fcc2 100644 --- a/src/Nullinside.Api/Controllers/UserController.cs +++ b/src/Nullinside.Api/Controllers/UserController.cs @@ -30,7 +30,7 @@ public class UserController : ControllerBase { /// /// The nullinside database. /// - private readonly NullinsideContext _dbContext; + private readonly INullinsideContext _dbContext; /// /// The logger. @@ -42,7 +42,7 @@ public class UserController : ControllerBase { /// /// The application's configuration file. /// The nullinside database. - public UserController(IConfiguration configuration, NullinsideContext dbContext) { + public UserController(IConfiguration configuration, INullinsideContext dbContext) { _configuration = configuration; _dbContext = dbContext; } diff --git a/src/Nullinside.Api/Program.cs b/src/Nullinside.Api/Program.cs index dfd4071..cc9da87 100644 --- a/src/Nullinside.Api/Program.cs +++ b/src/Nullinside.Api/Program.cs @@ -22,7 +22,7 @@ string? server = Environment.GetEnvironmentVariable("MYSQL_SERVER"); string? username = Environment.GetEnvironmentVariable("MYSQL_USERNAME"); string? password = Environment.GetEnvironmentVariable("MYSQL_PASSWORD"); -builder.Services.AddDbContext(optionsBuilder => +builder.Services.AddDbContext(optionsBuilder => optionsBuilder.UseMySQL( $"server={server};database=nullinside;user={username};password={password};AllowUserVariables=true;", builder => {