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 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<T>() to handle different DbContexts
}
private DbSet<Prog> getChiamateProgressiviByTenant(string tenant)
{
var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Progressivi; // Use Set<T>() to handle different DbContexts
}
//private DbSet<Prog> getChiamateProgressiviByTenant(string tenant)
//{
// var dbContext = _dbContextFactory.GetDbContext(tenant);
// return dbContext.Progressivi; // Use Set<T>() to handle different DbContexts
//}
/// <summary>
/// CONFLITTO CON Compo_Impia_Table
@ -1387,11 +1391,11 @@ namespace ApiPolo.Controllers
var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Sost; // Use Set<T>() to handle different DbContexts
}
private DbSet<Compo_Impia> getComponentiByTenant(string tenant)
{
var dbContext = _dbContextFactory.GetDbContext(tenant);
return dbContext.Compo; // Use Set<T>() to handle different DbContexts
}
//private DbSet<Compo_Impia> getComponentiByTenant(string tenant)
//{
// var dbContext = _dbContextFactory.GetDbContext(tenant);
// return dbContext.Compo; // Use Set<T>() 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<ActionResult<Chiamate_out>> 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<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
{
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<Compo_Impia> lst = new List<Compo_Impia>();
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<Prog> 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<Prog> 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<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<Prog2>? Prog { get; set; }
public DbSet<Prog>? Progressivi { get; set; }
public DbSet<Compo_Impia>? Compo { get; set; }
//public DbSet<Prog>? Progressivi { get; set; }
//public DbSet<Compo_Impia>? Compo { get; set; }
#endregion
private readonly DbContextOptions<Maras_DbContext> _options;
/// <summary></summary>
public Maras_DbContext(DbContextOptions<Maras_DbContext> options) : base(options)
{
_options = options;
}
/// <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<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<Prog2>? Prog { get; set; }
DbSet<Prog>? Progressivi { get; set; }
DbSet<Compo_Impia>? Compo { get; set; }
//DbSet<Prog>? Progressivi { 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.Interfaces;
using ApiPolo.Data;
using Microsoft.EntityFrameworkCore.Internal;
namespace ApiPolo
{
@ -1011,6 +1012,7 @@ namespace ApiPolo
// FACTORY MUTI-TENANT
services.AddScoped<ITenantDbContextFactory, TenantDbContextFactory>();
services.AddScoped<Service>();
services.AddSwaggerGen(gen =>