Browse Source

Reworked on-the-fly dbcontext creation and db connection

pull/4/head
Benjamin Arhen 2 years ago
parent
commit
781cb2ad95
  1. 34
      Server/BiskAcdbContext.cs
  2. 4
      Server/Program.cs
  3. 12
      Server/Services/ConnectionService.cs

34
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.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
using System.Security.Claims;
namespace Biskilog_Accounting.Server.POSModels; namespace Biskilog_Accounting.Server.POSModels;
public partial class BiskAcdbContext : DbContext public partial class BiskAcdbContext : DbContext
{ {
private readonly HttpContext m_httpContext;
private readonly IConnectionService m_connection;
private readonly ITokenService m_tokenService;
public BiskAcdbContext() public BiskAcdbContext()
{ {
} }
public BiskAcdbContext(DbContextOptions<BiskAcdbContext> 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<BiskAcdbContext> options) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
: base(options)
{ {
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<Creditpurchase> Creditpurchases { get; set; } public virtual DbSet<Creditpurchase> Creditpurchases { get; set; }

4
Server/Program.cs

@ -22,10 +22,12 @@ builder.Services.AddEntityFrameworkMySql().AddDbContext<BiskilogContext>(options
{ {
options.UseMySql(builder.Configuration.GetConnectionString("Connection"), new MariaDbServerVersion(new Version())); options.UseMySql(builder.Configuration.GetConnectionString("Connection"), new MariaDbServerVersion(new Version()));
}); });
builder.Services.AddScoped<BiskAcdbContext>(); builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDbContext<BiskAcdbContext>();
builder.Services.AddScoped<IAuthService, AuthenticationService>(); builder.Services.AddScoped<IAuthService, AuthenticationService>();
builder.Services.AddScoped<ITokenService, TokenService>(); builder.Services.AddScoped<ITokenService, TokenService>();
builder.Services.AddScoped<IConnectionService, ConnectionService>(); builder.Services.AddScoped<IConnectionService, ConnectionService>();
builder.Services.AddScoped<IAnalytics, AnalyticalService>();
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {

12
Server/Services/ConnectionService.cs

@ -10,13 +10,11 @@ namespace Biskilog_Accounting.Server.Services
public class ConnectionService : IConnectionService public class ConnectionService : IConnectionService
{ {
private readonly BiskilogContext m_context; private readonly BiskilogContext m_context;
private readonly ITokenService m_tokenService;
private readonly IConfiguration m_configuration; 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_context = a_context;
m_tokenService = a_tokenService;
m_configuration = configuration; m_configuration = configuration;
} }
/// <summary> /// <summary>
@ -42,11 +40,11 @@ namespace Biskilog_Accounting.Server.Services
/// <returns>A configured BiskAcdbContext</returns> /// <returns>A configured BiskAcdbContext</returns>
public object PrepareDBContext(string a_connectionString) public object PrepareDBContext(string a_connectionString)
{ {
throw new NotImplementedException();
//DbContextOptionsBuilder<BiskAcdbContext> acdbContext = new DbContextOptionsBuilder<BiskAcdbContext>();
//acdbContext.UseMySql(a_connectionString, new MariaDbServerVersion(new Version()));
DbContextOptionsBuilder<BiskAcdbContext> acdbContext = new DbContextOptionsBuilder<BiskAcdbContext>(); //return new BiskAcdbContext(acdbContext.Options);
acdbContext.UseMySql(a_connectionString, new MariaDbServerVersion(new Version()));
return new BiskAcdbContext(acdbContext.Options);
} }
} }
} }

Loading…
Cancel
Save