250 lines
8.8 KiB
C#
250 lines
8.8 KiB
C#
using ApiPolo.Data;
|
|
using ApiPolo.Interfaces;
|
|
using ApiPolo.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
using static ApiPolo.Controllers.PoloController;
|
|
|
|
namespace ApiPolo
|
|
{
|
|
/// <summary></summary>
|
|
public class Service
|
|
{
|
|
//private readonly Maras_DbContext _dbContext;
|
|
private readonly ITenantDbContextFactory _dbContextFactory;
|
|
|
|
// Constructor injection of DbContext
|
|
/// <summary></summary>
|
|
public Service( ITenantDbContextFactory dbContextFactory)
|
|
{
|
|
//_dbContext = dbContext;
|
|
_dbContextFactory = dbContextFactory;
|
|
}
|
|
|
|
// Example method
|
|
/// <summary></summary>
|
|
//public async Task AddRappNewToDbAsync2(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.");
|
|
// }
|
|
//}
|
|
|
|
/// <summary>Scrive su Rapp_new: prendi in carico, rifiuta, chiudi</summary>
|
|
public async Task AddRappNewToDbAsync(string tenant, Rapp_New model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
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;
|
|
throw new InvalidOperationException("Error in Service - AddRappNewToDbAsync: " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Cancella da Rapp_new: rilascia</summary>
|
|
public async Task RemoveRappNewToDbAsync(string tenant, Rapp_New model)
|
|
{
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
string mex = string.Empty;
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
dbContext.Rapps.Remove(model);
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - RemoveRappNewToDbAsync : " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Scrive su Chiamate: inserimento chiamata da APP</summary>
|
|
public async Task AddChiamateTableToDbAsync(string tenant, ChiamateTable model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
string mex = string.Empty;
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
await dbContext.ChiamateTables.AddAsync(model);
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - AddRappNewToDbAsync: " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Aggiorna Cpwarn: aggiornamento serieli e progressini</summary>
|
|
public async Task UpdCpwarnToDbAsync(string tenant, Prog2 model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
|
|
string mex = string.Empty;
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
dbContext.Entry(model).State = EntityState.Modified;
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
|
|
//using (var transactionLW = _LW_cpwarn.Database.BeginTransaction())
|
|
//{
|
|
// _cpwarns = getCpwarnByTenant(tenant);
|
|
// Prog2 entitasViewModel = _cpwarns.Where(p => p.tablecode.Equals(tablecode) && p.warncode.Equals(warncode)).FirstOrDefault();
|
|
// entitasViewModel.autonum = nuovoProg;
|
|
// entitasViewModel.cpccchk = cpcccchk;
|
|
|
|
// _LW_cpwarn.Entry(entitasViewModel).State = EntityState.Modified;
|
|
// await _LW_cpwarn.SaveChangesAsync();
|
|
// transactionLW.Commit();
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - AddRappNewToDbAsync: " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Scrive su Mag_new: prendi in carico, rifiuta, chiudi</summary>
|
|
public async Task AddMagNewToDbAsync(string tenant, Mag_New model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
string mex = string.Empty;
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
await dbContext.Mag.AddAsync(model);
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - AddRappNewToDbAsync: " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Scrive su BUONI: upload immagine buono in metodo chiudi</summary>
|
|
public async Task AddBuonoToDbAsync(string tenant, BuoniLW model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
string mex = string.Empty;
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
await dbContext.Buoni.AddAsync(model);
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - AddRappNewToDbAsync: " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Scrive su PIPRESE: Manutenzione prendi in carico</summary>
|
|
public async Task AddPreseToDbAsync(string tenant, Prese model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
string mex = string.Empty;
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
await dbContext.Prese.AddAsync(model);
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - AddPreseToDbAsync: " + mex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Scrive su PIPRESE: Manutenzione rilascia</summary>
|
|
public async Task RemovePreseToDbAsync(string tenant, Prese model)
|
|
{
|
|
|
|
var dbContext = _dbContextFactory.GetDbContext(tenant);
|
|
|
|
|
|
try
|
|
{
|
|
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
|
{
|
|
dbContext.Prese.Remove(model);
|
|
await dbContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string mex = string.Empty;
|
|
mex = ex.Message;
|
|
throw new InvalidOperationException("Error in Service - RemovePreseToDbAsync: " + mex);
|
|
}
|
|
}
|
|
}
|
|
}
|