using Biskilog_Accounting.Server.POSModels; using Biskilog_Accounting.Shared.ClientContractModels; using Biskilog_Accounting.Shared.Enums; using Biskilog_Accounting.Shared.Interfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; 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) { m_context = a_context; m_tokenService = a_tokenService; m_configuration = configuration; } /// /// Prepares and returns the connection string for a client using the specified database id /// /// Specified database id to use /// public string GetClientConnectionString(int a_databaseId) { Databasemap? dbMap = m_context.Databasemaps.Find(a_databaseId); if (dbMap != null) { string rawConString = m_configuration.GetConnectionString("PrivateConnection")!.ToString(); return String.Format(rawConString, dbMap.Domain, dbMap.DbName); } return ConnectionEnums.ConnectionNotEstablished.ToString(); } /// /// Prepare the DB context from the specified connection string /// /// /// /// A configured BiskAcdbContext public object PrepareDBContext(string a_connectionString) { DbContextOptionsBuilder acdbContext = new DbContextOptionsBuilder(); acdbContext.UseMySql(a_connectionString, new MariaDbServerVersion(new Version())); return new BiskAcdbContext(acdbContext.Options); } } }