using System; using System.Collections.Generic; using System.Linq; using System.Text; using EgtBEAMWALL.DataLayer.DatabaseModels; namespace EgtBEAMWALL.DataLayer.Controllers { public class MachGroupController : IDisposable { #region Private Fields private DatabaseContext dbCtx; #endregion Private Fields #region Public Fields public static MachGroupController man = new MachGroupController(); #endregion Public Fields #region Public Constructors public MachGroupController() { // Initialize database context dbCtx = new DatabaseContext(Constants.CONNECTION_STRING); } #endregion Public Constructors #if false /// /// Get paginated data from DB (ASC ordered) /// /// /// /// public List GetPaginatedAsc(int MachGroupDbIdStart, int numRecord) { int numEnd = MachGroupDbIdStart - numRecord; // check numEnd if (numEnd < 0) numEnd = 0; // retrieve return dbCtx .MachGroupList .Where(x => x.MachGroupDbId <= MachGroupDbIdStart) .OrderBy(x => x.MachGroupDbId) .Take(numRecord) .ToList(); } /// /// Get paginated data from DB (DESC ordered) /// /// /// /// public List GetPaginatedDesc(int PartDbIdStart, int numRecord) { int numEnd = PartDbIdStart - numRecord; // check numEnd if (numEnd < 0) numEnd = 0; // retrieve return dbCtx .MachGroupList .Where(x => x.MachGroupDbId <= PartDbIdStart) .OrderByDescending(x => x.MachGroupDbId) .Take(numRecord) .ToList(); } #endif #region Public Methods /// /// Conversion of base class to DB model class /// /// /// /// public static MachGroupModel ConvertFromCore(Core.MyMachGroup coreMachGroup, int currProdDbId) { MachGroupModel answ = new MachGroupModel(); if (coreMachGroup != null) { answ = new MachGroupModel() { Assign = "", Name = coreMachGroup.Name, Locked = true, MachGroupId = coreMachGroup.Id, ProdDbId = currProdDbId, State = Core.ItemState.ND, H = coreMachGroup.dH, L = coreMachGroup.dL, W = coreMachGroup.dW, Material = coreMachGroup.sMATERIAL }; } return answ; } /// /// Create record on DB /// /// /// /// public MachGroupModel Create(int newMachGroupId, string Name) { MachGroupModel newMachGroup = new MachGroupModel() { MachGroupId = newMachGroupId, Name = Name, State = Core.ItemState.ND }; try { // Add to database dbCtx.MachGroupList.Add(newMachGroup); // Commit changes dbCtx.SaveChanges(); } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Create: {exc}"); } return newMachGroup; } public void Dispose() { // Clear database context dbCtx.Dispose(); } /// /// Get record by MachGroupDbId /// /// /// public MachGroupModel FindByMachGroupDbId(int MachGroupDbId) { return dbCtx .MachGroupList .Where(x => x.MachGroupDbId == MachGroupDbId) .SingleOrDefault(); } /// /// Get record by MachGroupId /// /// /// public MachGroupModel FindByMachGroupId(int MachGroupId) { return dbCtx .MachGroupList .Where(x => x.MachGroupId == MachGroupId) .SingleOrDefault(); } /// /// Get filtered data by Assign (ASC ordered) /// /// /// public List GetByAssign(string Assign) { return dbCtx .MachGroupList .Where(x => x.Assign == Assign) .OrderBy(x => x.Order) .ThenBy(x => x.MachGroupId) .ToList(); } /// /// Get filtered data by ProdId (ASC ordered) /// /// /// public List GetByProdAsc(int ProdId) { // recupero int ProdDbId = 0; try { var currProd = dbCtx .ProdList .Where(x => x.ProdId == ProdId) .FirstOrDefault(); if (currProd != null) { ProdDbId = currProd.ProdDbId; } } catch (Exception exc) { Console.WriteLine($"EXCEPTION on GetByProdAsc: {exc}"); } return dbCtx .MachGroupList .Where(x => x.ProdDbId == ProdDbId) .OrderBy(x => x.Order) .ThenBy(x => x.MachGroupId) .ToList(); } /// /// Get filtered data by MachGroupectId (DESC ordered) /// /// /// public List GetByProdDesc(int ProdId) { // recupero int ProdDbId = 0; try { var currProd = dbCtx .ProdList .Where(x => x.ProdId == ProdId) .FirstOrDefault(); if (currProd != null) { ProdDbId = currProd.ProdDbId; } } catch (Exception exc) { Console.WriteLine($"EXCEPTION on GetByProdDesc: {exc}"); } return dbCtx .MachGroupList .Where(x => x.ProdDbId == ProdDbId) .OrderByDescending(x => x.Order) .ThenByDescending(x => x.MachGroupId) .ToList(); } /// /// Update single MachGroup /// /// Item to update (with updated values) /// public bool Update(MachGroupModel updItem) { bool done = false; var item2update = dbCtx .MachGroupList .Where(x => x.MachGroupDbId == updItem.MachGroupDbId) .SingleOrDefault(); try { // update, vers 1... dbCtx.Entry(item2update).CurrentValues.SetValues(updItem); //// update, vers 2 //dbCtx.BTLPartList.Remove(item2del); //dbCtx.BTLPartList.Add(updItem); // Commit changes dbCtx.SaveChanges(); done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Update: {exc}"); } return done; } /// /// Set Status for MachGroup /// /// /// /// public bool UpdateAssign(int MachGroupId, string newAssign) { bool done = false; try { // aggiorno var currRecord = dbCtx .MachGroupList .Where(x => x.MachGroupId == MachGroupId) .FirstOrDefault(); currRecord.Assign = newAssign; currRecord.State = Core.ItemState.Assigned; // Commit changes dbCtx.SaveChanges(); done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on UpdateAssign: {exc}"); } return done; } /// /// Set Status for MachGroup /// /// /// /// public bool UpdateStatus(int MachGroupId, Core.ItemState newState) { bool done = false; try { // aggiorno dbCtx .MachGroupList .Where(x => x.MachGroupId == MachGroupId) .FirstOrDefault() .State = newState; // Commit changes dbCtx.SaveChanges(); done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on UpdateStatus: {exc}"); } return done; } #endregion Public Methods } }