using EgwCoreLib.Lux.Core.RestPayload; using EgwCoreLib.Lux.Data.DbModel; 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; using static System.Runtime.InteropServices.JavaScript.JSType; namespace EgwCoreLib.Lux.Data.Controllers { public class LuxController { // manca costruttore parametrico contoller... #region Public Methods /// /// Elenco completo Customers da DB /// /// public List CustomersGetAll() { List dbResult = new List(); //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { dbResult = dbCtx .DbSetCustomer .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante CustomersGetAll{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elenco completo Dealers da DB /// /// public List DealersGetAll() { List dbResult = new List(); //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { dbResult = dbCtx .DbSetDealer .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante DealersGetAll{Environment.NewLine}{exc}"); } } return dbResult; } public List ItemGetAll() { List dbResult = new List(); //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { dbResult = dbCtx .DbSetItem .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante ItemGetAll{Environment.NewLine}{exc}"); } } return dbResult; } public List ItemGetSearch(string term) { List dbResult = new List(); //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { dbResult = dbCtx .DbSetItem .Where(x => x.Description.Contains(term)) .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante ItemGetSearch{Environment.NewLine}{exc}"); } } return dbResult; } public bool ItemUpsert(ItemModel newRec) { bool answ = false; //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { var currRec = dbCtx .DbSetItem .Where(x => x.ItemID == newRec.ItemID) .FirstOrDefault(); // se trovato --> aggiorno if (currRec != null) { currRec.Description = newRec.Description; currRec.SupplCode = newRec.SupplCode; currRec.ItemCode = newRec.ItemCode; currRec.Cost = newRec.Cost; currRec.Description = newRec.Description; currRec.IsService = newRec.IsService; currRec.Margin = newRec.Margin; dbCtx.Entry(currRec).State = EntityState.Modified; } // se mancasse --> aggiungo else { dbCtx.DbSetItem.Add(newRec); } // salvo... dbCtx.SaveChanges(); } catch (Exception exc) { Log.Error($"Eccezione durante ItemGetSearch{Environment.NewLine}{exc}"); } } return answ; ; } public bool ItemUpsertFromBom(List bomList) { bool answ = false; //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { // ciclo x ogni elemento della BOM, cercando x gruppo e ExtItemCode foreach (var item in bomList) { var currRec = dbCtx .DbSetItem .Where(x => x.CodGroup == item.ClassCode && x.ExtItemCode == item.ItemCode) .FirstOrDefault(); // se nullo --> lo devo inserire!!! if (currRec == null) { ItemModel newRec = new ItemModel() { CodGroup = item.ClassCode, IsService = false, // da calcolare meglio x gruppo ItemCode = 0, ExtItemCode = item.ItemCode, SupplCode = "BOM ITEM", Description = $"BOM | {item.ClassCode} | {item.ItemCode}", Cost = 0, Margin = 0, BomGen = true, QtyMin = 0, QtyMax = 0, UM = "#" }; dbCtx.DbSetItem.Add(newRec); } } // salvo... dbCtx.SaveChanges(); } catch (Exception exc) { Log.Error($"Eccezione durante ItemUpsertFromBom{Environment.NewLine}{exc}"); } } return answ; ; } /// /// Elenco completo offerte da DB /// /// public List OfferGetAll() { List dbResult = new List(); //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { dbResult = dbCtx .DbSetOffer .Include(c => c.CustomerNav) .Include(d => d.DealerNav) .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante OfferGetAll{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elenco righe offerta specificata /// /// /// public List OfferRowGetByOffer(int OfferID) { List dbResult = new List(); //using (DataLayerContext dbCtx = new DataLayerContext(configuration)) using (DataLayerContext dbCtx = new DataLayerContext()) { try { dbResult = dbCtx .DbSetOfferRow .Where(x => x.OfferID == OfferID) .Include(s => s.SellingItemNav) //.Include(d => d.DealerNav) .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante OfferRowGetByOffer{Environment.NewLine}{exc}"); } } return dbResult; } #endregion Public Methods #region Private Fields private static IConfiguration _configuration; private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields } }