prima bozza service

This commit is contained in:
Marco Audiffredi 2025-03-25 13:03:42 +01:00
parent c8489d34dd
commit 48c195e477
5 changed files with 138 additions and 47 deletions

View File

@ -40,6 +40,7 @@ using System.Runtime.CompilerServices;
using System.Diagnostics; using System.Diagnostics;
using ApiPolo.Data; using ApiPolo.Data;
using ApiPolo.Interfaces; using ApiPolo.Interfaces;
using Microsoft.EntityFrameworkCore.Internal;
namespace ApiPolo.Controllers namespace ApiPolo.Controllers
{ {
@ -54,6 +55,7 @@ namespace ApiPolo.Controllers
private readonly ConfigurazioniDbContext? _config; private readonly ConfigurazioniDbContext? _config;
private readonly IConfiguration? _configuration; private readonly IConfiguration? _configuration;
private readonly ITenantDbContextFactory _dbContextFactory; private readonly ITenantDbContextFactory _dbContextFactory;
private readonly Service _service;
private readonly TokenDbContext? _tokens; private readonly TokenDbContext? _tokens;
private readonly Maras_DbContext _Maras; private readonly Maras_DbContext _Maras;
@ -529,6 +531,7 @@ namespace ApiPolo.Controllers
IConfiguration configuration, IConfiguration configuration,
ConfigurazioniDbContext config, ConfigurazioniDbContext config,
ITenantDbContextFactory dbContextFactory, ITenantDbContextFactory dbContextFactory,
Service service,
TokenDbContext tokens, TokenDbContext tokens,
Maras_DbContext maras Maras_DbContext maras
/* /*
@ -899,6 +902,7 @@ namespace ApiPolo.Controllers
{ {
_dbContextFactory = dbContextFactory; _dbContextFactory = dbContextFactory;
_configuration = configuration; _configuration = configuration;
_service = service;
_config = config; _config = config;
_tokens = tokens; _tokens = tokens;
_Maras = maras; _Maras = maras;
@ -1326,11 +1330,11 @@ namespace ApiPolo.Controllers
var dbContext = _dbContextFactory.GetDbContext(tenant); var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Prog; // Use Set<T>() to handle different DbContexts return dbContext.Prog; // Use Set<T>() to handle different DbContexts
} }
private DbSet<Prog> getChiamateProgressiviByTenant(string tenant) //private DbSet<Prog> getChiamateProgressiviByTenant(string tenant)
{ //{
var dbContext = _dbContextFactory.GetDbContext(tenant); // var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Progressivi; // Use Set<T>() to handle different DbContexts // return dbContext.Progressivi; // Use Set<T>() to handle different DbContexts
} //}
/// <summary> /// <summary>
/// CONFLITTO CON Compo_Impia_Table /// CONFLITTO CON Compo_Impia_Table
@ -1387,11 +1391,11 @@ namespace ApiPolo.Controllers
var dbContext = _dbContextFactory.GetDbContext(tenant); var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Sost; // Use Set<T>() to handle different DbContexts return dbContext.Sost; // Use Set<T>() to handle different DbContexts
} }
private DbSet<Compo_Impia> getComponentiByTenant(string tenant) //private DbSet<Compo_Impia> getComponentiByTenant(string tenant)
{ //{
var dbContext = _dbContextFactory.GetDbContext(tenant); // var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Compo; // Use Set<T>() to handle different DbContexts // return dbContext.Compo; // Use Set<T>() to handle different DbContexts
} //}
#endregion #endregion
#region Utility #region Utility
@ -5647,15 +5651,15 @@ namespace ApiPolo.Controllers
newSerial = await getSeriale(token); newSerial = await getSeriale(token);
Rapp_New r = await fillRappNewByInput(model, token, newSerial, _confLette); 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()) //using (var transaction = dbContext.Database.BeginTransaction())
{ //{
await dbContext.Rapps.AddAsync(r); // await dbContext.Rapps.AddAsync(r);
await dbContext.SaveChangesAsync(); // await dbContext.SaveChangesAsync();
transaction.Commit(); // transaction.Commit();
} //}
//scrittura test //scrittura test
@ -6593,8 +6597,40 @@ namespace ApiPolo.Controllers
[Route("chiamate/test")] [Route("chiamate/test")]
public async Task<ActionResult<Chiamate_out>> test([FromBody] Rapp_New model) public async Task<ActionResult<Chiamate_out>> test([FromBody] Rapp_New model)
{ {
Maras_DbContext dbContext = (Maras_DbContext)_dbContextFactory.GetDbContext("MARAS");
string mex = string.Empty; string mex = string.Empty;
/*
using (var dbContext = _dbContextFactory.CreateDbContext())
{
try
{
// Add the entity to the DbSet
await dbContext.Set<T>().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 try
{ {
using (var transaction = dbContext.Database.BeginTransaction()) using (var transaction = dbContext.Database.BeginTransaction())
@ -6608,6 +6644,7 @@ namespace ApiPolo.Controllers
{ {
mex = ex.Message; mex = ex.Message;
} }
*/
return StatusCode(StatusCodes.Status200OK, mex); return StatusCode(StatusCodes.Status200OK, mex);
} }
@ -8450,17 +8487,6 @@ namespace ApiPolo.Controllers
string ten = getClaimValueByToken(token, "tenant"); string ten = getClaimValueByToken(token, "tenant");
List<Compo_Impia> lst = new List<Compo_Impia>(); List<Compo_Impia> lst = new List<Compo_Impia>();
if(!ten.Equals(Clienti.VT))
{
_compo = getComponentiByTenant(ten);
var co = await _compo.Where(
t => t.cocodimp == codimp
).ToListAsync();
lst = co;
}
else
{
Compo_Impia ci = new Compo_Impia(); Compo_Impia ci = new Compo_Impia();
ci.cocodimp = codimp; ci.cocodimp = codimp;
ci.cprownum = 1; ci.cprownum = 1;
@ -8471,7 +8497,7 @@ namespace ApiPolo.Controllers
lst.Add(ci); lst.Add(ci);
}
return StatusCode(StatusCodes.Status200OK, lst); return StatusCode(StatusCodes.Status200OK, lst);
} }
catch (Exception ex) catch (Exception ex)
@ -13486,15 +13512,17 @@ namespace ApiPolo.Controllers
private async Task<Prog> leggiCpwarnSeriale(string tenant, string tablecode) private async Task<Prog> leggiCpwarnSeriale(string tenant, string tablecode)
{ {
//autorun float //autorun float
_chiamateprogr = getChiamateProgressiviByTenant(tenant); //_chiamateprogr = getChiamateProgressiviByTenant(tenant);
var cpwarn = await _chiamateprogr.Where(p => p.tablecode != null && p.tablecode.Contains(tablecode) && p.tablecode.Contains(tenant)).FirstAsync(); //var cpwarn = await _chiamateprogr.Where(p => p.tablecode != null && p.tablecode.Contains(tablecode) && p.tablecode.Contains(tenant)).FirstAsync();
Prog cpwarn = new Prog();
return cpwarn; return cpwarn;
} }
private async Task<Prog> leggiCpwarnProg(string tenant, string tablecode, string codiceEsercizio) private async Task<Prog> leggiCpwarnProg(string tenant, string tablecode, string codiceEsercizio)
{ {
//autorun float //autorun float
_chiamateprogr = getChiamateProgressiviByTenant(tenant); //_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(); //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; return cpwarn;
} }
private async Task<Prog2> leggiCpwarnProgDecimal(string tenant, string tablecode, string codiceEsercizio) private async Task<Prog2> leggiCpwarnProgDecimal(string tenant, string tablecode, string codiceEsercizio)

View File

@ -57,13 +57,17 @@ namespace ApiPolo.Data
public DbSet<Commessa>? Commesse { get; set; } public DbSet<Commessa>? Commesse { get; set; }
public DbSet<Prog2>? Prog { get; set; } public DbSet<Prog2>? Prog { get; set; }
public DbSet<Prog>? Progressivi { get; set; } //public DbSet<Prog>? Progressivi { get; set; }
public DbSet<Compo_Impia>? Compo { get; set; } //public DbSet<Compo_Impia>? Compo { get; set; }
#endregion #endregion
private readonly DbContextOptions<Maras_DbContext> _options;
/// <summary></summary> /// <summary></summary>
public Maras_DbContext(DbContextOptions<Maras_DbContext> options) : base(options) public Maras_DbContext(DbContextOptions<Maras_DbContext> options) : base(options)
{ {
_options = options;
} }
/// <summary></summary> /// <summary></summary>
@ -174,7 +178,17 @@ namespace ApiPolo.Data
TryConfigureEntity(mb => mb.Entity<Sto_Imp>().ToView("API_STO_IMP"), "API_STO_IMP"); TryConfigureEntity(mb => mb.Entity<Sto_Imp>().ToView("API_STO_IMP"), "API_STO_IMP");
TryConfigureEntity(mb =>
{
mb.Entity<Prog2>().ToTable("CPWARN");
mb.Entity<Prog2>().HasKey(table => new
{
table.tablecode,
table.warncode
});
}, "CPWARN");
}
} }
} }
}

View File

@ -31,8 +31,8 @@ namespace ApiPolo.Interfaces
DbSet<Sto_Imp>? StoImp { get; set; } DbSet<Sto_Imp>? StoImp { get; set; }
DbSet<Prog2>? Prog { get; set; } DbSet<Prog2>? Prog { get; set; }
DbSet<Prog>? Progressivi { get; set; } //DbSet<Prog>? Progressivi { get; set; }
DbSet<Compo_Impia>? Compo { get; set; } //DbSet<Compo_Impia>? Compo { get; set; }
} }

47
ApiPolo/Service.cs Normal file
View File

@ -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.");
}
}
}
}

View File

@ -4,6 +4,7 @@ using Microsoft.OpenApi.Models;
using ApiPolo.Services; using ApiPolo.Services;
using ApiPolo.Interfaces; using ApiPolo.Interfaces;
using ApiPolo.Data; using ApiPolo.Data;
using Microsoft.EntityFrameworkCore.Internal;
namespace ApiPolo namespace ApiPolo
{ {
@ -1011,6 +1012,7 @@ namespace ApiPolo
// FACTORY MUTI-TENANT // FACTORY MUTI-TENANT
services.AddScoped<ITenantDbContextFactory, TenantDbContextFactory>(); services.AddScoped<ITenantDbContextFactory, TenantDbContextFactory>();
services.AddScoped<Service>();
services.AddSwaggerGen(gen => services.AddSwaggerGen(gen =>