diff --git a/ApiPolo/Controllers/PoloController.cs b/ApiPolo/Controllers/PoloController.cs index 51a49eb..3b1a17b 100644 --- a/ApiPolo/Controllers/PoloController.cs +++ b/ApiPolo/Controllers/PoloController.cs @@ -6649,6 +6649,31 @@ namespace ApiPolo.Controllers return StatusCode(StatusCodes.Status200OK, mex); } + [HttpPost] + [Route("chiamate/test2")] + public async Task> Test2([FromBody] Rapp_New model) + { + var dbContext = _dbContextFactory.GetDbContext("MARAS"); + + string mex = string.Empty; + try + { + using (var transaction = await dbContext.Database.BeginTransactionAsync()) + { + await dbContext.Rapps.AddAsync(model); + await dbContext.SaveChangesAsync(); + await transaction.CommitAsync(); + } + } + catch (Exception ex) + { + mex = ex.Message; + } + + return StatusCode(StatusCodes.Status200OK, mex); + } + + #endregion #region MANUTENZIONI diff --git a/ApiPolo/Data/TenantDbContext.cs b/ApiPolo/Data/TenantDbContext.cs new file mode 100644 index 0000000..2e5abb8 --- /dev/null +++ b/ApiPolo/Data/TenantDbContext.cs @@ -0,0 +1,199 @@ +using ApiPolo.Interfaces; +using ApiPolo.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +namespace ApiPolo.Data +{ + public class TenantDbContext : DbContext,ITenantDbContext + { + private readonly string _connectionString; + + #region PROPERTIES + public DbSet? Causali { get; set; } + + public DbSet? Chiusure { get; set; } + + public DbSet? Azi { get; set; } + + public DbSet? ccci { get; set; } + + public DbSet? ccciWiev { get; set; } + + public DbSet? Chiamate { get; set; } + + public DbSet? Clienti { get; set; } + + public DbSet? Componen { get; set; } // IN CONFLITTO CON Compo_Impia + + //public DbSet? Componen { get; set; } //CAMBIARE NOME!!! + + public DbSet? Impia { get; set; } + + public DbSet? Mag { get; set; } + + public DbSet? Magaz { get; set; } + + public DbSet? Manutenzioni { get; set; } + + public DbSet? Pagamenti { get; set; } + + public DbSet? Prese { get; set; } + + public DbSet? Rapps { get; set; } + + public DbSet? rapp { get; set; } + + public DbSet? Sost { get; set; } + + public DbSet? Saldi { get; set; } + + public DbSet? StoRapp { get; set; } + + public DbSet? Tecnici { get; set; } + public DbSet? StoImp { get; set; } + + public DbSet? Timbr { get; set; } + + public DbSet? Commesse { get; set; } + + public DbSet? Prog { get; set; } + //public DbSet? Progressivi { get; set; } + //public DbSet? Compo { get; set; } + #endregion + + public TenantDbContext(DbContextOptions options, IConfiguration configuration, string tenant) + : base(options) + { + // Fetch the connection string dynamically based on tenant + _connectionString = configuration.GetConnectionString(tenant) + ?? throw new KeyNotFoundException($"No connection string found for tenant: {tenant}"); + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlServer(_connectionString); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + void TryConfigureEntity(Action configure, string entityName) + { + try + { + configure(modelBuilder); + } + catch (Exception ex) + { + Console.WriteLine($"Warning: Skipping entity '{entityName}' due to error: {ex.Message}"); + } + } + + TryConfigureEntity(mb => mb.Entity().ToView("API_CAUS_RAPP"), "API_CAUS_RAPP"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_CHIUSURE"), "API_CHIUSURE"); + + TryConfigureEntity(mb => + { + mb.Entity().ToTable("PIAZIRIF"); + mb.Entity().HasKey(table => new + { + table.piazihoc, + table.picodtec, + table.pirifazi + }); + }, "PIAZIRIF"); + + TryConfigureEntity(mb => + { + mb.Entity().ToTable("PIASSCHIU"); + mb.Entity().HasKey(table => new + { + table.picodazi, + table.picodint, + table.picodchi + }); + }, "PIASSCHIU"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_ASSCHIU"), "API_ASSCHIU"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_CHIAMATE"), "API_CHIAMATE"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_CLIENTI"), "API_CLIENTI"); + + TryConfigureEntity(mb => + { + mb.Entity().ToTable("DISCOMICOMIMP"); + mb.Entity().HasKey(table => new + { + table.cocodimp, + table.cprownum, + table.cocodazi + }); + }, "DISCOMICOMIMP"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_MICOMIMP"), "API_MICOMIMP"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_IMPIANTI"), "API_IMPIANTI"); + + TryConfigureEntity(mb => + { + mb.Entity().ToTable("DISCOMAG_NEW"); + mb.Entity().HasKey(table => new + { + table.seriale_rapportino, + table.riga + }); + }, "DISCOMAG_NEW"); + + TryConfigureEntity(mb => mb.Entity().ToTable("DISCOMAGAZZIN"), "DISCOMAGAZZIN"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_MANPROG"), "API_MANPROG"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_PAGAMENTI"), "API_PAGAMENTI"); + + TryConfigureEntity(mb => + { + mb.Entity().ToTable("PIPRESA"); + mb.Entity().HasKey(table => new + { + table.picodazi, + table.pimpianto, + table.picodint, + table.pidatman + }); + }, "PIPRESA"); + + TryConfigureEntity(mb => mb.Entity().ToTable("RAPP_NEW"), "RAPP_NEW"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_RAPPORTINI"), "API_RAPPORTINI"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_SALDIART_PREZZI"), "API_SALDIART_PREZZI"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_SOSTITUZIONI"), "API_SOSTITUZIONI"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_STO_RAPP"), "API_STO_RAPP"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_TECNICI"), "API_TECNICI"); + + TryConfigureEntity(mb => mb.Entity().ToTable("TIMBRATURE"), "TIMBRATURE"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_COMMESSE"), "API_COMMESSE"); + + TryConfigureEntity(mb => mb.Entity().ToView("API_STO_IMP"), "API_STO_IMP"); + + TryConfigureEntity(mb => + { + mb.Entity().ToTable("CPWARN"); + mb.Entity().HasKey(table => new + { + table.tablecode, + table.warncode + }); + }, "CPWARN"); + } + } +} diff --git a/ApiPolo/Interfaces/ITenantDbContextFactory.cs b/ApiPolo/Interfaces/ITenantDbContextFactory.cs index f5054e1..586ca24 100644 --- a/ApiPolo/Interfaces/ITenantDbContextFactory.cs +++ b/ApiPolo/Interfaces/ITenantDbContextFactory.cs @@ -1,9 +1,9 @@ -using Microsoft.EntityFrameworkCore; +using ApiPolo.Data; namespace ApiPolo.Interfaces { public interface ITenantDbContextFactory { - ITenantDbContext GetDbContext(string tenant); + TenantDbContext GetDbContext(string tenant); } } diff --git a/ApiPolo/Interfaces/TenantDbContextFactory.cs b/ApiPolo/Interfaces/TenantDbContextFactory.cs index 2ea5bd5..4a52b49 100644 --- a/ApiPolo/Interfaces/TenantDbContextFactory.cs +++ b/ApiPolo/Interfaces/TenantDbContextFactory.cs @@ -1,31 +1,29 @@ using ApiPolo.Data; using ApiPolo.Interfaces; using Microsoft.EntityFrameworkCore; -using static ApiPolo.Controllers.PoloController; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; -/// public class TenantDbContextFactory : ITenantDbContextFactory { private readonly IServiceProvider _serviceProvider; + private readonly IConfiguration _configuration; - /// - public TenantDbContextFactory(IServiceProvider serviceProvider) + public TenantDbContextFactory(IServiceProvider serviceProvider, IConfiguration configuration) { _serviceProvider = serviceProvider; + _configuration = configuration; } - public ITenantDbContext GetDbContext(string tenant) + public TenantDbContext GetDbContext(string tenant) { - // Create a scope to resolve DbContext - var scope = _serviceProvider.CreateScope(); + var optionsBuilder = new DbContextOptionsBuilder(); - return tenant switch - { - Clienti.Maras => scope.ServiceProvider.GetRequiredService(), + string connectionString = _configuration.GetConnectionString(tenant) + ?? throw new KeyNotFoundException($"No connection string found for tenant: {tenant}"); - // Add other tenants as needed - // Clienti.AnotherTenant => scope.ServiceProvider.GetRequiredService(), - _ => throw new KeyNotFoundException($"No DbContext found for tenant: {tenant}") - }; + optionsBuilder.UseSqlServer(connectionString); + + return new TenantDbContext(optionsBuilder.Options, _configuration, tenant); } }