using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MP.AppAuth.Models;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MP.AppAuth.Controllers
{
public class MPController : IDisposable
{
#region Public Fields
public static MPController dbController;
#endregion Public Fields
#region Private Fields
private static IConfiguration _configuration;
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Public Constructors
public MPController(IConfiguration configuration)
{
_configuration = configuration;
Log.Info("Avviata classe MpController");
}
#endregion Public Constructors
#region Public Methods
///
/// Elenco Record x AnagCauSca
///
///
public List AnagCauSca()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagCauSca
.ToList();
}
return dbResult;
}
///
/// Elenco Record x AnagClassiTempo
///
///
public List AnagClassiTempo()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagClassiTempo
.ToList();
}
return dbResult;
}
///
/// Elenco Record x AnagraficaEventi
///
///
public List AnagEventi()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagEventi
.ToList();
}
return dbResult;
}
///
/// Elenco Record x AnagraficaIngressi
///
///
public List AnagIngressi()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagIngressi
.ToList();
}
return dbResult;
}
///
/// Delete Record AnagKeyValue
///
///
public bool AnagKeyValuesDelete(string NomeVar)
{
bool fatto = false;
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
var rec2del = localDbCtx
.DbSetAnagKeyValues
.Where(x => x.NomeVar == NomeVar)
.FirstOrDefault();
if (rec2del != null)
{
localDbCtx.Remove(rec2del);
var res = localDbCtx.SaveChanges();
fatto = res > 0;
}
}
return fatto;
}
///
/// Elenco Record x AnagKeyValue
///
///
public List AnagKeyValuesGetAll()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagKeyValues
.ToList();
}
return dbResult;
}
///
/// Upsert AnagKeyValue
///
///
public bool AnagKeyValuesUpsert(AnagKeyValueModel newRec)
{
bool fatto = false;
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
var currRec = localDbCtx
.DbSetAnagKeyValues
.Where(x => x.NomeVar == newRec.NomeVar)
.FirstOrDefault();
if (currRec != null)
{
currRec.Descrizione = newRec.Descrizione;
currRec.ValInt = newRec.ValInt;
currRec.ValFloat = newRec.ValFloat;
currRec.ValString = newRec.ValString;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else
{
localDbCtx.DbSetAnagKeyValues.Add(newRec);
}
var res = localDbCtx.SaveChanges();
fatto = res > 0;
}
return fatto;
}
///
/// Elenco Record x AnagMicroStati
///
///
public List AnagMicroStati()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagMicroStati
.ToList();
}
return dbResult;
}
///
/// Elenco Record x AnagraficaStati
///
///
public List AnagStati()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetAnagStati
.ToList();
}
return dbResult;
}
///
/// Delete Record Config
///
///
public bool ConfigDelete(string Chiave)
{
bool fatto = false;
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
var rec2del = localDbCtx
.DbSetConfig
.Where(x => x.Chiave == Chiave)
.FirstOrDefault();
if (rec2del != null)
{
localDbCtx.Remove(rec2del);
var res = localDbCtx.SaveChanges();
fatto = res > 0;
}
}
return fatto;
}
///
/// Elenco Record x Config
///
///
public List ConfigGetAll()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetConfig
.ToList();
}
return dbResult;
}
///
/// Upsert Config
///
///
public bool ConfigUpsert(ConfigModel newRec)
{
bool fatto = false;
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
var currRec = localDbCtx
.DbSetConfig
.Where(x => x.Chiave == newRec.Chiave)
.FirstOrDefault();
if (currRec != null)
{
currRec.Valore = newRec.Valore;
currRec.ValoreStd = newRec.ValoreStd;
currRec.Note = newRec.Note;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else
{
localDbCtx.DbSetConfig.Add(newRec);
}
var res = localDbCtx.SaveChanges();
fatto = res > 0;
}
return fatto;
}
public void Dispose()
{
if (dbController != null)
{
// Clear database controller
dbController.Dispose();
}
}
///
/// Elenco Record x ListValues
///
///
public List ListValues()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetListValue
.ToList();
}
return dbResult;
}
///
/// Elenco Record Macchine
///
///
public List MacchineGetAll()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetMacchine
.ToList();
}
return dbResult;
}
///
/// Delete Record Vocabolario
///
///
public bool VocabolarioDelete(VocabolarioModel currRec)
{
bool fatto = false;
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
var rec2del = localDbCtx
.DbSetVocabolario
.Where(x => x.Lingua == currRec.Lingua && x.Lemma == currRec.Lemma)
.FirstOrDefault();
if (rec2del != null)
{
localDbCtx.Remove(rec2del);
var res = localDbCtx.SaveChanges();
fatto = res > 0;
}
}
return fatto;
}
///
/// Elenco Record x VocabolarioModel
///
///
public List VocabolarioGetAll()
{
List dbResult = new List();
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
dbResult = localDbCtx
.DbSetVocabolario
.ToList();
}
return dbResult;
}
///
/// Upsert Vocabolario
///
///
public bool VocabolarioUpsert(VocabolarioModel newRec)
{
bool fatto = false;
using (MoonProContext localDbCtx = new MoonProContext(_configuration))
{
var currRec = localDbCtx
.DbSetVocabolario
.Where(x => x.Lingua == newRec.Lingua && x.Lemma == newRec.Lemma)
.FirstOrDefault();
if (currRec != null)
{
currRec.Traduzione = newRec.Traduzione;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else
{
localDbCtx.DbSetVocabolario.Add(newRec);
}
var res = localDbCtx.SaveChanges();
fatto = res > 0;
}
return fatto;
}
#endregion Public Methods
}
}