|
|
@ -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<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) |
|
|
|
: 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<Creditpurchase> Creditpurchases { get; set; } |
|
|
|
|
|
|
|