Files
limanapp/LiMan.DB/Controllers/DbController.cs
T
Samuele Locatelli 07189dc22b REnaming classi DB
2021-10-11 11:22:04 +02:00

229 lines
7.6 KiB
C#

using LiMan.DB.DBModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LiMan.DB.Controllers
{
public class LicManController : IDisposable
{
#region Private Fields
private static IConfiguration _configuration;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Public Constructors
public LicManController(IConfiguration configuration)
{
_configuration = configuration;
}
#endregion Public Constructors
#region Public Methods
public bool DbForceMigrate()
{
bool answ = false;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
try
{
localDbCtx.DbForceMigrate();
answ = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in DbForceMigrate");
}
}
return answ;
}
public void Dispose()
{
// Clear database context
//Log.Info("Dispose di GWMSController");
}
public List<ApplicativoModel> GetApplicazioni()
{
List<ApplicativoModel> dbResult = new List<ApplicativoModel>();
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
dbResult = localDbCtx
.DbSetApp
.ToList();
}
return dbResult;
}
public List<InstallazioneModel> GetInstallazioni()
{
List<InstallazioneModel> dbResult = new List<InstallazioneModel>();
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
dbResult = localDbCtx
.DbSetInst
.ToList();
}
return dbResult;
}
public List<LicenzaModel> GetLicenze()
{
List<LicenzaModel> dbResult = new List<LicenzaModel>();
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
dbResult = localDbCtx
.DbSetLicenze
.ToList();
}
return dbResult;
}
public List<LicenzaModel> GetLicenzeFilt(bool OnlyActive, string CodApp, string CodInst)
{
List<LicenzaModel> dbResult = new List<LicenzaModel>();
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
DateTime oggi = DateTime.Today;
dbResult = localDbCtx
.DbSetLicenze
.Where(x => (x.CodApp == CodApp || string.IsNullOrEmpty(CodApp)) && (x.CodInst == CodInst || string.IsNullOrEmpty(CodInst)) && (!OnlyActive || x.Scadenza > oggi))
.OrderByDescending(o => o.Scadenza)
.ToList();
}
return dbResult;
}
/// <summary>
/// Annulla modifiche su una specifica entity (cancel update)
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool rollBackEntity(object item)
{
bool answ = false;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
try
{
if (localDbCtx.Entry(item).State == EntityState.Deleted || localDbCtx.Entry(item).State == EntityState.Modified)
{
localDbCtx.Entry(item).Reload();
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}");
}
}
return answ;
}
public bool UpdateApplicazioni(ApplicativoModel updItem)
{
bool done = false;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
try
{
ApplicativoModel currData = localDbCtx
.DbSetApp
.Where(x => x.CodApp == updItem.CodApp)
.FirstOrDefault();
if (currData != null)
{
// aggiorno valori
currData.Descrizione = updItem.Descrizione;
localDbCtx.Entry(currData).State = EntityState.Modified;
localDbCtx.SaveChanges();
}
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in UpdateApplicazioni:{Environment.NewLine}{exc}");
}
}
return done;
}
public bool UpdateInstallazioni(InstallazioneModel updItem)
{
bool done = false;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
try
{
InstallazioneModel currData = localDbCtx
.DbSetInst
.Where(x => x.CodInst == updItem.CodInst)
.FirstOrDefault();
if (currData != null)
{
// aggiorno valori
currData.Descrizione = updItem.Descrizione;
currData.Cliente = updItem.Cliente;
currData.Contatto = updItem.Contatto;
currData.Email = updItem.Email;
localDbCtx.Entry(currData).State = EntityState.Modified;
localDbCtx.SaveChanges();
}
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in UpdateApplicazioni:{Environment.NewLine}{exc}");
}
}
return done;
}
public bool UpdateLicenze(LicenzaModel updItem)
{
bool done = false;
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
{
try
{
LicenzaModel currData = localDbCtx
.DbSetLicenze
.Where(x => x.IdxLic == updItem.IdxLic)
.FirstOrDefault();
if (currData != null)
{
// aggiorno valori
currData.CodApp = updItem.CodApp;
currData.CodInst = updItem.CodInst;
currData.NumLicenze = updItem.NumLicenze;
currData.Descrizione = updItem.Descrizione;
currData.Chiave = updItem.Chiave;
currData.Locked = updItem.Locked;
currData.IdxLicParent = updItem.IdxLicParent;
currData.Scadenza = updItem.Scadenza;
localDbCtx.Entry(currData).State = EntityState.Modified;
localDbCtx.SaveChanges();
}
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in UpdateLicenze:{Environment.NewLine}{exc}");
}
}
return done;
}
#endregion Public Methods
}
}