using EgtBEAMWALL.DataLayer.DatabaseModels; using EgwProxy.MagMan.DTO; using NLog; using System; using System.Collections.Generic; using System.Linq; namespace EgtBEAMWALL.DataLayer.Controllers { /// /// Gestione Alias su DB (es: materiali da BTL e su DB) /// public class AliasController : IDisposable { #region Public Constructors public AliasController() { } #endregion Public Constructors #if false /// /// Conversion of DB model class to base class /// /// /// public static Core.AliasM ConvToCore(AliasModel dbRec) { Core.AliasM answ = new Core.AliasM(dbRec.ValueOriginal, dbRec.ValueAlias); return answ; } #endif #region Public Methods /// /// Helper conversione a DTO /// /// /// public static AliasDTO ConvToDto(AliasModel currRec) { AliasDTO answ = new AliasDTO() { ValOrig = currRec.ValueOriginal, ValAlias = currRec.ValueAlias, IsActive = true }; return answ; } /// /// Helper conversione da DTO /// /// /// /// public static AliasModel ConvToModel(AliasDTO currRec, string family) { AliasModel answ = new AliasModel() { Family = family, ValueOriginal = currRec.ValOrig, ValueAlias = currRec.ValAlias }; return answ; } /// /// Delete by key /// /// /// /// public bool DeleteByKey(string Family, string ValueOriginal) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { var items2del = localDbCtx .AliasList .Where(x => x.Family == Family && x.ValueOriginal == ValueOriginal); try { // Add to database localDbCtx.AliasList.RemoveRange(items2del); // Commit changes localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Log.Error($"EXCEPTION on Alias.DeleteByKey: {Environment.NewLine}{exc}"); } } return done; } public void Dispose() { } /// /// Ricerca record da chiave (=valore ALIAS) /// /// Valore ALIAS cercato /// Famiglia alias cercato (MatCode se non specificato) /// public AliasModel FindByDbId(string ValueOriginal, string Family = "MatCode") { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .AliasList .Where(x => x.Family == Family && x.ValueOriginal.ToLower() == ValueOriginal.ToLower()) .SingleOrDefault(); } } /// /// Elenco Alias x famiglia /// /// se "" restituisce tutti /// public List GetFilt(string Family) { // retrieve using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .AliasList .Where(x => x.Family == Family) .OrderBy(x => x.ValueOriginal) .ToList(); } } /// /// Update or insert Alias record /// /// /// public bool Upsert(AliasModel updItem) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { var item2update = localDbCtx .AliasList .Where(x => x.Family == updItem.Family && x.ValueOriginal == updItem.ValueOriginal) .SingleOrDefault(); if (item2update != null) { // update, vers 1... localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem); } else { localDbCtx.AliasList.Add(updItem); } // Commit changes localDbCtx.SaveChanges(); fatto = true; } catch (Exception exc) { Log.Error($"EXCEPTION on Alias.Upsert: {Environment.NewLine}{exc}"); } } return fatto; } /// /// Update or insert Alias record list /// /// /// public bool UpsertList(List updList) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { foreach (var updItem in updList) { var item2update = localDbCtx .AliasList .Where(x => x.Family == updItem.Family && x.ValueOriginal.ToLower() == updItem.ValueOriginal.ToLower()) .SingleOrDefault(); if (item2update != null) { if (item2update.ValueAlias.ToLower() != updItem.ValueAlias.ToLower()) { localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem); } } else { localDbCtx.AliasList.Add(updItem); } } // Commit changes localDbCtx.SaveChanges(); fatto = true; } catch (Exception exc) { Log.Error($"EXCEPTION on Alias.UpsertList: {Environment.NewLine}{exc}"); } } return fatto; } /// /// Eliminazione eventuali record Alias nella lista (se presenti) /// /// /// public bool DeleteList(List updList) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { foreach (var updItem in updList) { var item2del = localDbCtx .AliasList .Where(x => x.Family == updItem.Family && x.ValueOriginal.ToLower() == updItem.ValueOriginal.ToLower()) .SingleOrDefault(); if (item2del != null) { localDbCtx.AliasList.Remove(item2del); } } // Commit changes localDbCtx.SaveChanges(); fatto = true; } catch (Exception exc) { Log.Error($"EXCEPTION on Alias.DeleteList: {Environment.NewLine}{exc}"); } } return fatto; } #endregion Public Methods #region Private Fields /// /// Istanza logger /// private Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields } }