Uniti i dbset in uno nuovo unico. aggiunta gestione token

This commit is contained in:
michele 2025-01-30 15:44:35 +01:00
parent 7cd0837a8d
commit 103fcaa068
7 changed files with 49 additions and 74 deletions

View File

@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -11,9 +11,9 @@ namespace ApiAdHoc_Odoo.Controllers
[ApiController]
public class ClientiController : ControllerBase
{
private readonly Pi_ClientiDbContext _clientiContext;
private readonly PiDbContext _clientiContext;
public ClientiController(Pi_ClientiDbContext clientiContext)
public ClientiController(PiDbContext clientiContext)
{
_clientiContext = clientiContext;
}
@ -21,28 +21,8 @@ namespace ApiAdHoc_Odoo.Controllers
[HttpGet("{pIva}")]
public async Task<ActionResult<IEnumerable<Cliente>>> GetApiClienti(string pIva)
{
return await _clientiContext.Clienti.Where(x => x.Partiva == pIva).ToListAsync();
var clientiList = _clientiContext.Clienti.Where(x => x.Partiva == pIva).ToListAsync();
await foreach (var cliente in _clientiContext.Clienti.AsAsyncEnumerable())
{
cliente.Azienda = cliente.Azienda.Trim();
cliente.Codice = cliente.Codice.Trim();
cliente.Descrizione = cliente.Descrizione.Trim();
cliente.Partiva = cliente.Partiva.Trim();
cliente.CodFisc = cliente.CodFisc.Trim();
cliente.Telefono = cliente.Telefono.Trim();
cliente.Cellulare = cliente.Cellulare.Trim();
cliente.Email = cliente.Email.Trim();
cliente.Indirizzo = cliente.Indirizzo.Trim();
cliente.Citta = cliente.Citta.Trim();
cliente.Cap = cliente.Cap.Trim();
cliente.Provincia = cliente.Provincia.Trim();
cliente.CodAgente = cliente.CodAgente.Trim();
cliente.DescriAgente = cliente.DescriAgente.Trim();
}
return await clientiList;
}
// GET: api/ApiClienti/{codice}
@ -51,21 +31,6 @@ namespace ApiAdHoc_Odoo.Controllers
{
var cliente = await _clientiContext.Clienti.FirstOrDefaultAsync(c => c.Codice == codCli && c.Azienda == codAzi);
cliente.Azienda = cliente.Azienda.Trim();
cliente.Codice = cliente.Codice.Trim();
cliente.Descrizione = cliente.Descrizione.Trim();
cliente.Partiva = cliente.Partiva.Trim();
cliente.CodFisc = cliente.CodFisc.Trim();
cliente.Telefono = cliente.Telefono.Trim();
cliente.Cellulare = cliente.Cellulare.Trim();
cliente.Email = cliente.Email.Trim();
cliente.Indirizzo = cliente.Indirizzo.Trim();
cliente.Citta = cliente.Citta.Trim();
cliente.Cap = cliente.Cap.Trim();
cliente.Provincia = cliente.Provincia.Trim();
cliente.CodAgente = cliente.CodAgente.Trim();
cliente.DescriAgente = cliente.DescriAgente.Trim();
if (cliente == null)
{
return NotFound();

View File

@ -10,9 +10,9 @@ namespace ApiAdHoc_Odoo.Controllers
[ApiController]
public class FattureController : Controller
{
private readonly PI_FattureDbContext _fattureContext;
private readonly PiDbContext _fattureContext;
public FattureController(PI_FattureDbContext fattureContext)
public FattureController(PiDbContext fattureContext)
{
_fattureContext = fattureContext;
}
@ -25,15 +25,6 @@ namespace ApiAdHoc_Odoo.Controllers
x.CodCliente == codCli &&
x.DataDocumento >= DateTime.Now.AddYears(-2)).OrderByDescending(x => x.DataDocumento).ToListAsync();
await foreach (var fattura in _fattureContext.Fattura.AsAsyncEnumerable())
{
fattura.Azienda = fattura.Azienda.Trim();
fattura.CodCliente = fattura.CodCliente.Trim();
fattura.Descrizione = fattura.Descrizione.Trim();
fattura.Classe = fattura.Classe.Trim();
fattura.Documento = fattura.Azienda.Trim();
}
return await fattureList;
}

View File

@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Mvc;
namespace ApiAdHoc_Odoo.Controllers
{
public class TokenAuthenticationController : Controller
{
private readonly RequestDelegate _next;
private const string TokenHeaderName = "PoloApiKey"; // Header name
private const string PreSharedToken = "ViRvfsqRmnB1AjAJ"; // Replace with your actual token
public TokenAuthenticationController(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (!context.Request.Headers.TryGetValue(TokenHeaderName, out var extractedToken) ||
extractedToken != PreSharedToken)
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
await context.Response.WriteAsync("Unauthorized");
return;
}
await _next(context);
}
}
}

View File

@ -1,20 +0,0 @@
using ApiAdHoc_Odoo.Models;
using Microsoft.EntityFrameworkCore;
namespace ApiAdHoc_Odoo.Data
{
public class PI_FattureDbContext : DbContext
{
public PI_FattureDbContext(DbContextOptions<PI_FattureDbContext> options) : base(options) { }
public DbSet<Fattura> Fattura { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Map the view to the model
modelBuilder.Entity<Fattura>()
.ToView("PI_APIFATTURE")/*;*/
.HasNoKey(); // Views typically don't have a primary key
}
}
}

View File

@ -4,11 +4,13 @@ using System;
namespace ApiAdHoc_Odoo.Data
{
public class Pi_ClientiDbContext : DbContext
public class PiDbContext : DbContext
{
public Pi_ClientiDbContext(DbContextOptions<Pi_ClientiDbContext> options) : base(options) { }
public PiDbContext(DbContextOptions<PiDbContext> options) : base(options) { }
public DbSet<Cliente> Clienti { get; set; }
public DbSet<Fattura> Fattura { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -16,6 +18,10 @@ namespace ApiAdHoc_Odoo.Data
modelBuilder.Entity<Cliente>()
.ToView("PI_APICLIENTI")/*;*/
.HasNoKey(); // Views typically don't have a primary key
// Map the view to the model
modelBuilder.Entity<Fattura>()
.ToView("PI_APIFATTURE")/*;*/
.HasNoKey(); // Views typically don't have a primary key
}
}
}

View File

@ -1,3 +1,4 @@
using ApiAdHoc_Odoo.Controllers;
using ApiAdHoc_Odoo.Data;
using ApiAdHoc_Odoo.Models;
using Microsoft.EntityFrameworkCore;
@ -14,9 +15,9 @@ builder.Services.AddControllers();
#region istanze dbContext
builder.Services.AddDbContext<Pi_ClientiDbContext>(options =>
builder.Services.AddDbContext<PiDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDbContext<PI_FattureDbContext>(options =>
builder.Services.AddDbContext<PiDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
#endregion
@ -34,6 +35,8 @@ if (app.Environment.IsDevelopment())
{
}
app.UseMiddleware<TokenAuthenticationController>();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();