diff --git a/Server/BiskAcdbContext.cs b/Server/BiskAcdbContext.cs index 0583fb5..d0ba890 100644 --- a/Server/BiskAcdbContext.cs +++ b/Server/BiskAcdbContext.cs @@ -1,17 +1,45 @@ -using Biskilog_Accounting.Shared.POSModels; +using Azure.Core; +using Biskilog_Accounting.Shared.Enums; +using Biskilog_Accounting.Shared.Interfaces; +using Biskilog_Accounting.Shared.POSModels; using Microsoft.EntityFrameworkCore; +using Microsoft.Net.Http.Headers; +using System.Security.Claims; namespace Biskilog_Accounting.Server.POSModels; public partial class BiskAcdbContext : DbContext { + private readonly HttpContext m_httpContext; + private readonly IConnectionService m_connection; + private readonly ITokenService m_tokenService; public BiskAcdbContext() { } + public BiskAcdbContext(DbContextOptions options, ITokenService tokenService, IConnectionService connection, IHttpContextAccessor a_httpContextAccessor = null) + : base(options) + { + m_tokenService = tokenService; + m_connection = connection; + m_httpContext = a_httpContextAccessor?.HttpContext; + } - public BiskAcdbContext(DbContextOptions options) - : base(options) + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + if (!optionsBuilder.IsConfigured) + { + string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; + if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + { + int? databaseId = m_tokenService.GetDatabaseIdFromToken(token); + string connectionString = m_connection.GetClientConnectionString(databaseId!.Value); + optionsBuilder.UseMySql(connectionString, new MariaDbServerVersion(new Version())); + } + else + { + m_httpContext.Abort(); + } + } } public virtual DbSet Creditpurchases { get; set; } diff --git a/Server/Program.cs b/Server/Program.cs index da1c2bf..7e20b15 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -22,10 +22,12 @@ builder.Services.AddEntityFrameworkMySql().AddDbContext(options { options.UseMySql(builder.Configuration.GetConnectionString("Connection"), new MariaDbServerVersion(new Version())); }); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddDbContext(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddCors(options => { diff --git a/Server/Services/ConnectionService.cs b/Server/Services/ConnectionService.cs index 54bd806..6fa3e8c 100644 --- a/Server/Services/ConnectionService.cs +++ b/Server/Services/ConnectionService.cs @@ -10,13 +10,11 @@ namespace Biskilog_Accounting.Server.Services public class ConnectionService : IConnectionService { private readonly BiskilogContext m_context; - private readonly ITokenService m_tokenService; private readonly IConfiguration m_configuration; - public ConnectionService(BiskilogContext a_context, ITokenService a_tokenService, IConfiguration configuration) + public ConnectionService(BiskilogContext a_context, IConfiguration configuration) { m_context = a_context; - m_tokenService = a_tokenService; m_configuration = configuration; } /// @@ -42,11 +40,11 @@ namespace Biskilog_Accounting.Server.Services /// A configured BiskAcdbContext public object PrepareDBContext(string a_connectionString) { - - DbContextOptionsBuilder acdbContext = new DbContextOptionsBuilder(); - acdbContext.UseMySql(a_connectionString, new MariaDbServerVersion(new Version())); + throw new NotImplementedException(); + //DbContextOptionsBuilder acdbContext = new DbContextOptionsBuilder(); + //acdbContext.UseMySql(a_connectionString, new MariaDbServerVersion(new Version())); - return new BiskAcdbContext(acdbContext.Options); + //return new BiskAcdbContext(acdbContext.Options); } } }