98 lines
3.2 KiB
C#
98 lines
3.2 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></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></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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|