From 48c195e4773caf8daadef73d2eddc8aa14bbdb27 Mon Sep 17 00:00:00 2001 From: Marco Audiffredi Date: Tue, 25 Mar 2025 13:03:42 +0100 Subject: [PATCH] prima bozza service --- ApiPolo/Controllers/PoloController.cs | 112 +++++++++++++++---------- ApiPolo/Data/Maras_DbContext.cs | 20 ++++- ApiPolo/Interfaces/ITenantDbContext.cs | 4 +- ApiPolo/Service.cs | 47 +++++++++++ ApiPolo/Startup.cs | 2 + 5 files changed, 138 insertions(+), 47 deletions(-) create mode 100644 ApiPolo/Service.cs diff --git a/ApiPolo/Controllers/PoloController.cs b/ApiPolo/Controllers/PoloController.cs index ea89669..51a49eb 100644 --- a/ApiPolo/Controllers/PoloController.cs +++ b/ApiPolo/Controllers/PoloController.cs @@ -40,6 +40,7 @@ using System.Runtime.CompilerServices; using System.Diagnostics; using ApiPolo.Data; using ApiPolo.Interfaces; +using Microsoft.EntityFrameworkCore.Internal; namespace ApiPolo.Controllers { @@ -54,6 +55,7 @@ namespace ApiPolo.Controllers private readonly ConfigurazioniDbContext? _config; private readonly IConfiguration? _configuration; private readonly ITenantDbContextFactory _dbContextFactory; + private readonly Service _service; private readonly TokenDbContext? _tokens; private readonly Maras_DbContext _Maras; @@ -529,6 +531,7 @@ namespace ApiPolo.Controllers IConfiguration configuration, ConfigurazioniDbContext config, ITenantDbContextFactory dbContextFactory, + Service service, TokenDbContext tokens, Maras_DbContext maras /* @@ -899,6 +902,7 @@ namespace ApiPolo.Controllers { _dbContextFactory = dbContextFactory; _configuration = configuration; + _service = service; _config = config; _tokens = tokens; _Maras = maras; @@ -1326,11 +1330,11 @@ namespace ApiPolo.Controllers var dbContext = _dbContextFactory.GetDbContext(tenant); return dbContext.Prog; // Use Set() to handle different DbContexts } - private DbSet getChiamateProgressiviByTenant(string tenant) - { - var dbContext = _dbContextFactory.GetDbContext(tenant); - return dbContext.Progressivi; // Use Set() to handle different DbContexts - } + //private DbSet getChiamateProgressiviByTenant(string tenant) + //{ + // var dbContext = _dbContextFactory.GetDbContext(tenant); + // return dbContext.Progressivi; // Use Set() to handle different DbContexts + //} /// /// CONFLITTO CON Compo_Impia_Table @@ -1387,11 +1391,11 @@ namespace ApiPolo.Controllers var dbContext = _dbContextFactory.GetDbContext(tenant); return dbContext.Sost; // Use Set() to handle different DbContexts } - private DbSet getComponentiByTenant(string tenant) - { - var dbContext = _dbContextFactory.GetDbContext(tenant); - return dbContext.Compo; // Use Set() to handle different DbContexts - } + //private DbSet getComponentiByTenant(string tenant) + //{ + // var dbContext = _dbContextFactory.GetDbContext(tenant); + // return dbContext.Compo; // Use Set() to handle different DbContexts + //} #endregion #region Utility @@ -5647,15 +5651,15 @@ namespace ApiPolo.Controllers newSerial = await getSeriale(token); Rapp_New r = await fillRappNewByInput(model, token, newSerial, _confLette); - Maras_DbContext dbContext = (Maras_DbContext)_dbContextFactory.GetDbContext("MARAS"); + //Maras_DbContext dbContext = (Maras_DbContext)_dbContextFactory.GetDbContext("MARAS"); - using (var transaction = dbContext.Database.BeginTransaction()) - { - await dbContext.Rapps.AddAsync(r); - await dbContext.SaveChangesAsync(); - transaction.Commit(); - } + //using (var transaction = dbContext.Database.BeginTransaction()) + //{ + // await dbContext.Rapps.AddAsync(r); + // await dbContext.SaveChangesAsync(); + // transaction.Commit(); + //} //scrittura test @@ -6593,8 +6597,40 @@ namespace ApiPolo.Controllers [Route("chiamate/test")] public async Task> test([FromBody] Rapp_New model) { - Maras_DbContext dbContext = (Maras_DbContext)_dbContextFactory.GetDbContext("MARAS"); string mex = string.Empty; + /* + using (var dbContext = _dbContextFactory.CreateDbContext()) + { + try + { + // Add the entity to the DbSet + await dbContext.Set().AddAsync(entity); + + // Save the changes to the database + await dbContext.SaveChangesAsync(); + } + catch (Exception ex) + { + // Handle exceptions as necessary + Console.WriteLine($"Error inserting entity: {ex.Message}"); + // Optionally rethrow or log the errorMaras + throw; + } + }*/ + try + { + _service.AddRappNewToDbAsync("AA",model); + } + catch(Exception ex) + { + mex = ex.Message; + } + + + + /* + Maras_DbContext dbContext = (Maras_DbContext)_dbContextFactory.GetDbContext("MARAS"); + try { using (var transaction = dbContext.Database.BeginTransaction()) @@ -6604,10 +6640,11 @@ namespace ApiPolo.Controllers transaction.Commit(); } } - catch(Exception ex) + catch (Exception ex) { mex = ex.Message; } + */ return StatusCode(StatusCodes.Status200OK, mex); } @@ -8450,28 +8487,17 @@ namespace ApiPolo.Controllers string ten = getClaimValueByToken(token, "tenant"); List lst = new List(); - if(!ten.Equals(Clienti.VT)) - { - _compo = getComponentiByTenant(ten); + Compo_Impia ci = new Compo_Impia(); + ci.cocodimp = codimp; + ci.cprownum = 1; + ci.cocodart = "00001"; + ci.cocodazi = "AZI02"; + ci.ardesart = "Tubo in metallo"; - var co = await _compo.Where( - t => t.cocodimp == codimp - ).ToListAsync(); - lst = co; - } - else - { - Compo_Impia ci = new Compo_Impia(); - ci.cocodimp = codimp; - ci.cprownum = 1; - ci.cocodart = "00001"; - ci.cocodazi = "AZI02"; - ci.ardesart = "Tubo in metallo"; - - lst.Add(ci); + lst.Add(ci); + - } return StatusCode(StatusCodes.Status200OK, lst); } catch (Exception ex) @@ -13486,15 +13512,17 @@ namespace ApiPolo.Controllers private async Task leggiCpwarnSeriale(string tenant, string tablecode) { //autorun float - _chiamateprogr = getChiamateProgressiviByTenant(tenant); - var cpwarn = await _chiamateprogr.Where(p => p.tablecode != null && p.tablecode.Contains(tablecode) && p.tablecode.Contains(tenant)).FirstAsync(); + //_chiamateprogr = getChiamateProgressiviByTenant(tenant); + //var cpwarn = await _chiamateprogr.Where(p => p.tablecode != null && p.tablecode.Contains(tablecode) && p.tablecode.Contains(tenant)).FirstAsync(); + Prog cpwarn = new Prog(); return cpwarn; } private async Task leggiCpwarnProg(string tenant, string tablecode, string codiceEsercizio) { //autorun float - _chiamateprogr = getChiamateProgressiviByTenant(tenant); - var cpwarn = await _chiamateprogr.Where(p => p.tablecode != null && p.tablecode.Contains(tablecode) && p.tablecode.Contains(codiceEsercizio) && p.tablecode.Contains(tenant)).FirstAsync(); + //_chiamateprogr = getChiamateProgressiviByTenant(tenant); + //var cpwarn = await _chiamateprogr.Where(p => p.tablecode != null && p.tablecode.Contains(tablecode) && p.tablecode.Contains(codiceEsercizio) && p.tablecode.Contains(tenant)).FirstAsync(); + Prog cpwarn = new Prog(); return cpwarn; } private async Task leggiCpwarnProgDecimal(string tenant, string tablecode, string codiceEsercizio) diff --git a/ApiPolo/Data/Maras_DbContext.cs b/ApiPolo/Data/Maras_DbContext.cs index d8d0089..3fc45a4 100644 --- a/ApiPolo/Data/Maras_DbContext.cs +++ b/ApiPolo/Data/Maras_DbContext.cs @@ -57,13 +57,17 @@ namespace ApiPolo.Data public DbSet? Commesse { get; set; } public DbSet? Prog { get; set; } - public DbSet? Progressivi { get; set; } - public DbSet? Compo { get; set; } + //public DbSet? Progressivi { get; set; } + //public DbSet? Compo { get; set; } #endregion + + + private readonly DbContextOptions _options; /// public Maras_DbContext(DbContextOptions options) : base(options) { + _options = options; } /// @@ -174,7 +178,17 @@ namespace ApiPolo.Data 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/ITenantDbContext.cs b/ApiPolo/Interfaces/ITenantDbContext.cs index e968f44..41cabc1 100644 --- a/ApiPolo/Interfaces/ITenantDbContext.cs +++ b/ApiPolo/Interfaces/ITenantDbContext.cs @@ -31,8 +31,8 @@ namespace ApiPolo.Interfaces DbSet? StoImp { get; set; } DbSet? Prog { get; set; } - DbSet? Progressivi { get; set; } - DbSet? Compo { get; set; } + //DbSet? Progressivi { get; set; } + //DbSet? Compo { get; set; } } diff --git a/ApiPolo/Service.cs b/ApiPolo/Service.cs new file mode 100644 index 0000000..ffaec70 --- /dev/null +++ b/ApiPolo/Service.cs @@ -0,0 +1,47 @@ +using ApiPolo.Data; +using ApiPolo.Models; +using Microsoft.EntityFrameworkCore; +using static ApiPolo.Controllers.PoloController; + +namespace ApiPolo +{ + public class Service + { + private readonly Maras_DbContext _dbContext; + + // Constructor injection of DbContext + public Service(Maras_DbContext dbContext) + { + _dbContext = dbContext; + } + + // Example method + public async Task AddRappNewToDbAsync(string tenant, Rapp_New model) + { + // Some logic to add entity to DbContext + //await _dbContext.Rapps.AddAsync(model); + //await _dbContext.SaveChangesAsync(); + if (tenant.Equals(Clienti.Maras)) + { + using (var transaction = _dbContext.Database.BeginTransaction()) + { + await _dbContext.Rapps.AddAsync(model); + await _dbContext.SaveChangesAsync(); + transaction.Commit(); + } + } + //else if (dbContext is OtherDbContext otherDbContext) + //{ + // await otherDbContext.AddAsync(entity); + // await otherDbContext.SaveChangesAsync(); + //} + else + { + throw new InvalidOperationException("Unsupported DbContext type."); + } + + + + } + } +} diff --git a/ApiPolo/Startup.cs b/ApiPolo/Startup.cs index 4b97e1f..d35fcd6 100644 --- a/ApiPolo/Startup.cs +++ b/ApiPolo/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.OpenApi.Models; using ApiPolo.Services; using ApiPolo.Interfaces; using ApiPolo.Data; +using Microsoft.EntityFrameworkCore.Internal; namespace ApiPolo { @@ -1011,6 +1012,7 @@ namespace ApiPolo // FACTORY MUTI-TENANT services.AddScoped(); + services.AddScoped(); services.AddSwaggerGen(gen =>